在使用GooglePlaceAPI“findplacefromtext”时面临一种奇怪的行为。https://maps.googleapis.com/maps/api/place/findplacefromtext/json
当通过Linux终端上的CURL
或PHP中的CURL
或PHP中的file\u get\u contents
使用API时,响应为:
{
"candidates" : [],
"status" : "ZERO_RESULTS"
}
这里是谷歌API。出于安全原因,"API KEY"不共享:https://maps.googleapis.com/maps/api/place/findplacefromtext/json?key=APIKEY
API密钥是有效的,并已正确授权。
改变反应的原因是什么?
对我来说很好。
$url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?key=APIKEY&inputtype=textquery&fields=photos,formatted_address,name,rating,opening_hours,geometry,reference,place_id&input=Lollapalooza+Argentina+Mart%C3%ADnez+Argentina";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
print '<pre>';
print $response;
print '</pre>';
其中打印:
{
"candidates" : [
{
"formatted_address" : "Martínez, Buenos Aires Province, Argentina",
"geometry" : {
"location" : {
"lat" : -34.4811677,
"lng" : -58.5165812
},
"viewport" : {
"northeast" : {
"lat" : -34.47967187010728,
"lng" : -58.5156313
},
"southwest" : {
"lat" : -34.48237152989272,
"lng" : -58.51943089999999
}
}
},
"name" : "Lollapalooza Argentina",
"photos" : [
{
"height" : 1536,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/115749286027841945795\"\u003eA Google User\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAiIyTEnkgKjKYFyQZKesopF4_dwAP6h0ynKq0nO5e9AelAo9D0IPqASptGAbZ999ZcOfPIQnlQYie5KtO45bIkSAMOowBkROYcJLdntbS0WhsNhhQJaG5lxU8aCUTuyjGEhBnHL_HnTImzwkwS4OHVSN9GhRO_IGFKamDj_8OEIL3i9MGaCWsXQ",
"width" : 2048
}
],
"place_id" : "ChIJg-5cj9SxvJURkm6B2DlkKL0",
"rating" : 5,
"reference" : "ChIJg-5cj9SxvJURkm6B2DlkKL0"
},
{
"formatted_address" : "Av. Sta Fe 1, B1642 Martínez, Buenos Aires, Argentina",
"geometry" : {
"location" : {
"lat" : -34.4742948,
"lng" : -58.51265829999999
},
"viewport" : {
"northeast" : {
"lat" : -34.47292497010727,
"lng" : -58.51127897010728
},
"southwest" : {
"lat" : -34.47562462989271,
"lng" : -58.51397862989273
}
}
},
"name" : "Lollapalooza Argentina",
"place_id" : "ChIJUQmrWaqxvJURScOzSiIIk9o",
"rating" : 0,
"reference" : "ChIJUQmrWaqxvJURScOzSiIIk9o"
}
],
"status" : "OK"
}
您面临的问题是由于执行请求的原始计算机的IP地址不同。
为了证明这一点,将IP偏差更改为区域,两台机器/browser/php脚本应返回相同的数据集。
查看更多:
locationbias-通过指定一个半径加上lat/lng或两个表示矩形点的lat/lng对,可以在指定区域内获得更优结果。如果未指定此参数,则默认情况下API使用IP地址偏移。
IP偏置:指示API使用IP地址偏置。传递字符串ipbias(此选项没有其他参数)。
来源:https://developers.google.com/places/web-service/search