Warning when using file_get_contents for google api

I use this code to find lat lon location

$api='http://maps.googleapis.com/maps/api/geocode/json?address=United States Neversink&sensor=false';

$result = file_get_contents($api);

$data = json_decode($result);

But this gives a warning Warning: file_get_contents (http://maps.googleapis.com/maps/api/geocode/json?address=United States Neversink & sensor = false) [function.file-get-contents]: could not open the stream : The HTTP request failed! HTTP / 1.0 400 Bad Request in C: \ wamp \ www \ new_yellowpages \ modules \ mod_yellowpages_item_map \ helper.php on line 139

if anyone knows about this problem please help me.

+3
source share
3 answers

Use curl instead file_get_contents:

$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;
+16
source

urlencode . ( .)

$address = urlencode("United States Neversink");
$api='http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false';

$result = file_get_contents($api);
+12

Try changing HTTP to HTTPS and replace '' with '+'

https://maps.googleapis.com/maps/api/geocode/json?address=United+States+Neversink&sensor=false
0
source

Source: https://habr.com/ru/post/1672946/


All Articles