25

In one of my php applications I have to find out the latitude and longitude of the place from address.

I tried this code:

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

But it is showing the following Error :

Warning: file_get_contents(http://maps.google.com/maps/api/geocode/json?address=technopark, Trivandrun, kerala,India&sensor=false&region=IND) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in D:\Projects\lon.php on line 4

Please help me to solve this issue.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
ramesh
  • 4,008
  • 13
  • 72
  • 117

7 Answers7

40

Use curl instead of 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;
Druid
  • 6,423
  • 4
  • 41
  • 56
priyank saini
  • 424
  • 5
  • 2
  • 1
    Does anyone know the purpose of specifying CURLOPT_PROXYPORT 3128?? – sootsnoot May 27 '15 at 15:30
  • Oops - went past the 5-minute limit on comment-editing... I'm not the least bit familiar with using proxies, but from my reading on the subject, I would expect that since there is no proxy *server* specified, then the proxy *port* would be ignored. Yet most every post I see on using curl to access google maps specifies this port with no server. Is this just copy-paste pollution, or is there a purpose to it? – sootsnoot May 27 '15 at 15:45
  • Hi, I want to get current location according to ip address. Is there any way to get this ? – Mr. Tomar Aug 02 '16 at 10:58
  • Most of the time it gives null response.What might be the issue. – Anjali Patil Jul 24 '18 at 10:12
37
$address = str_replace(" ", "+", $address);

Use the above code before the file_get_content. means, use the following code

$address = str_replace(" ", "+", $address);

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

and it will work surely. As address does not support spaces it supports only + sign in place of space.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
8
//add urlencode to your address
$address = urlencode("technopark, Trivandrun, kerala,India");
$region = "IND";
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");

echo $json;

$decoded = json_decode($json);

print_r($decoded);
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
4

Two ideas:

  • Are Address and Region URL Encoded?
  • Perhaps your computer running the code doesn't allow http access. Try loading another page (like 'http://www.google.com') and see if that works. If that also doesn't work, then there's something wrong with PHP settings.
Max
  • 8,671
  • 4
  • 33
  • 46
1
$address = str_replace(" ", "+", $address);

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

...and don't forget "$region" for the code to work:

$address = "Salzburg";
$address = str_replace(" ", "+", $address);
$region = "Austria";

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
echo $lat."</br>".$long;
Pingui
  • 1,312
  • 4
  • 15
  • 28
0

I think allow_url_fopen on your apache server is disabled. you need to trun it on.

kindly change allow_url_fopen = 0 to allow_url_fopen = 1

Don't forget to restart your Apache server after changing it.

Deepak
  • 185
  • 2
  • 9