Google 地图地理编码 API 只能通过浏览器使用

Google Maps Geocode API only works via browser

我尝试通过postalcode/zipcode检索纬度和经度数据。

这是我正在使用的URL:

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

url returns JSON 数据(包括 latitude/longitude 值)一旦我通过浏览器打开它。

但是当我通过 PHP file_get_contents 方法调用完全相同的 URL 时,它 returns 'ZERO_RESULTS'.

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

echo $url; 
echo $geocode;

这将打印以下结果:

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

地理编码:

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

我做错了什么?

谢谢!

在邮政编码数字后加上 space,在我的例子中是 9462 和 AA 之间,适用于 file_get_contents

结论:通过浏览器,您可以在数字和字符之间使用或不使用 space 或 '+' 进行 API 调用,而在 PHP 中,您需要将它们分开带有 space 或“+”的那些。

最后的URL变成了:

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