php - 谷歌地图地理编码从浏览器工作,而不是从 PHP 服务器工作

标签 php google-maps google-geocoding-api

当我从我的浏览器获取 Google Maps Geocoding API 时,它可以工作,但不能从我的 PHP 脚本中获取。

工作:https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

返回:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "La Pérelle",
               "short_name" : "La Pérelle",
               "types" : [ "colloquial_area", "political" ]
            },
         ],
         "formatted_address" : "La Pérelle, 38380, France",
         // other stuffs
         "partial_match" : true,
         "place_id" : "ChIJZzDRRAX4ikcRnZ-vb1tnTgY",
         "types" : [ "colloquial_area", "political" ]
      }
   ],
   "status" : "OK"
}

不工作:

<?php 
$url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France';
echo file_get_contents($url);

返回:

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

有什么想法吗?

更新:有些人也没有从他们的浏览器中看到任何结果。 Google 可以利用我的职位来显示不错的结果吗?

最佳答案

(缺乏)语言偏好是导致此问题最常见的原因。

当您从浏览器发送 API 请求时:

https://maps.google.com/maps/api/geocode/json?address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

您的浏览器语言首选项可能会设置为首选法语作为第一语言,这会使您的浏览器包含与所有请求一起发送 Accept-Language header 。

Google Geocoding API 将检查 language GET 参数,如果没有,它将检查 Accept-Language header 作为备份。如果您的浏览器设置为更喜欢法语内容,那应该使上述请求等同于

https://maps.google.com/maps/api/geocode/json?address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France&language=fr

对我来说,因为我的浏览器设置为更喜欢英语,所以第一个请求返回“La Perelle, 73130 Saint-François-Longchamp, France”,而第二个请求返回“La Pérelle, 38380 Saint-Pierre-de-Chartreuse, France” ".

自从 2016 年底引入新的地理编码器以来,region 参数不再是唯一影响结果的参数,language 参数现在确实会影响结果:

The preferred language has a small influence on the set of results that the API chooses to return, and the order in which they are returned. The geocoder interprets abbreviations differently depending on language, such as the abbreviations for street types, or synonyms that may be valid in one language but not in another. For example, utca and tér are synonyms for street in Hungarian.

也就是说,如果“La Perelle, 73130 Saint-François-Longchamp, France”只是上述查询的错误结果(La Pérelle, Saint-Pierre-de-Chartreuse, France),请考虑 filing a bug report .

关于php - 谷歌地图地理编码从浏览器工作,而不是从 PHP 服务器工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693280/

相关文章:

javascript - 尝试在backbone.js应用程序中重构谷歌地图的监听器回调

curl - Google Geocoding API 突然很慢

php - 谷歌地图 : is a lat/lng within a polygon?

php - 如何使用 SQL 按降序排列整数的总和

php - codeigniter 分页错误

android - 使用 Google Maps Android API 实用程序库的自定义标记

javascript - 标记在谷歌地图中不可见

python - 使用Python的geopy + Google Maps Geocoding API的HTTP错误500

javascript - JSON.parse 中不允许使用哪些字符?

php - 检查用户输入错误的方法。 Jquery 还是 PHP?