Needs to get location details for the latitude and longitude, It just work in other server - functioning correctly, but trying with client dev server getting error, i have tried with this about 4 hours - till didn't get the result. assist would be helpful. Here is my code:
public function index() {
echo $this->getLocation('39.2323', '-97.3828');
}
function getLocation($lat, $long) {
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" . trim($lat) . "," . trim($long) . "&sensor=false";
$json = @file_get_contents($url);
$data = json_decode($json);
$status = $data->status;
$address = '';
if ($status == "OK") {
$address = $data->results[0]->formatted_address;
}
return $address;
}
Am getting the error like below:
A PHP Error was encountered Severity: Warning Message: file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=39.2323,-97.3828&sensor=false) [function.file-get-contents]: failed to open stream: Connection timed out
Filename: webservice/Test.php Line Number: 17
Backtrace:
File: /home/colanful/public_html/hoa/application/controllers/webservice/Test.php Line: 17 Function: file_get_contents
File: /home/colanful/public_html/hoa/application/controllers/webservice/Test.php Line: 12 Function: getaddress
File: /home/colanful/public_html/hoa/index.php Line: 292 Function: require_once
Looks like you need to change an ini setting on the clients dev server.
From @Aillyn's answer:
The setting you are looking for is allow_url_fopen.
You have two ways of getting around it without changing php.ini, one of them is to use fsockopen(), and the other is to use cURL.
I recommend using cURL over file_get_contents() anyways, since it was built for this.