I retrieved this function for making requests to Google Maps Geolocation API using PHP.
Make a request for The Google Maps Geolocation API
The issue I am having is the request fails around 50% of the time, and it takes forever to actually time out. ( Example: I'll make the request, and it will fail after about 20 seconds. I will refresh the page thus making the request again without changing any information, and it works ) I am trying to write this function into a class, but the service has been far too unreliable.
The Error:
Warning: file_get_contents(http://maps.google.com/maps/api/geocode/json?sensor=false&address=+chicago+IL): failed to open stream: Connection timed out in filename.php on line 2132
Is there any reason why?
Can I force this to timeout sooner?
Note: HTACCESS is currently blocking all but my IP, but changing this doesn't seem to help
public function getGeoPosition($address){
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false" .
"&address=" . urlencode($address);
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
if($data['status']=="OK"){
return $data['results'];
}
}
print_r(getGeoPosition('chicago,il'));