简体   繁体   English

Google Maps Geocode API仅可通过浏览器使用

[英]Google Maps Geocode API only works via browser

I try to retrieve the latitude and longitude data by postalcode/zipcode. 我尝试通过邮政编码/邮政编码检索纬度和经度数据。

This is the URL that I'm using: 这是我正在使用的URL:

http://maps.google.com/maps/api/geocode/json?address=9462AA 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. 当我通过浏览器打开url时,该URL返回JSON数据(包括纬度/经度值)。

But when I call the exact same URL via the PHP file_get_contents method, it returns 'ZERO_RESULTS'. 但是,当我通过PHP file_get_contents方法调用完全相同的URL时,它将返回“ 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 网址: http : //maps.google.com/maps/api/geocode/json?address=9462AA

Geocode: 地理编码:

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

What am I doing wrong? 我究竟做错了什么?

Thank you! 谢谢!

Putting a space after the postalcode digits, in my case between 9462 and AA, works with file_get_contents . 在邮编数字后面放置一个空格(在我的情况下是9462和AA之间)与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 '+'. 结论:通过浏览器,您可以进行API调用,数字和字符之间可以有空格或无空格或'+',在PHP中,您需要用空格或'+'分隔。

The final URL became: 最终网址变为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM