Google Maps Geocode API only works via browser

南笙酒味 提交于 2020-01-06 01:50:46

问题


I try to retrieve the latitude and longitude data by postalcode/zipcode.

This is the URL that I'm using:

http://maps.google.com/maps/api/geocode/json?address=9462AA

The url returns JSON data (including latitude/longitude values) once I open it via the browser.

But when I call the exact same URL via the PHP file_get_contents method, it returns 'ZERO_RESULTS'.

$url = 'http://maps.google.com/maps/api/geocode/json?address=9462AA';
$geocode = file_get_contents($url);

echo $url; 
echo $geocode;

This prints the following result:

URL: http://maps.google.com/maps/api/geocode/json?address=9462AA

Geocode:

{
   "results" : [],
   "status" : "ZERO_RESULTS"
}

What am I doing wrong?

Thank you!


回答1:


Putting a space after the postalcode digits, in my case between 9462 and AA, works with file_get_contents.

Conclusion: via the browser you can do an API call with or without a space or '+' between the digits and characters, and in PHP you need to seperate those with a space or '+'.

The final URL became:

http://maps.google.com/maps/api/geocode/json?address=9462+AA



来源:https://stackoverflow.com/questions/31909037/google-maps-geocode-api-only-works-via-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!