【问题标题】:ZERO_RESULTS from Google Search API "findplacefromtext"来自 Google 搜索 API“findplacefromtext”的 ZERO_RESULTS
【发布时间】:2020-04-06 19:57:54
【问题描述】:

在使用 Google Place API“findplacefromtext”时遇到奇怪的行为。 https://maps.googleapis.com/maps/api/place/findplacefromtext/json

当通过浏览器使用 API 来检查响应时。显示有效响应。附上截图。

在 PHP 中通过 CURL on Linux TerminalCURL in PHPfile_get_contents 使用 API 时,响应为:

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

这是 Google API。出于安全原因,不共享“API KEY”: 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

API 密钥有效且经过适当授权。

反应变化背后的原因是什么?

【问题讨论】:

    标签: php curl google-api google-places-api file-get-contents


    【解决方案1】:

    对我来说很好。

    $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"
    }
    

    【讨论】:

      【解决方案2】:

      您面临的问题是由于执行请求的原始计算机的 IP 地址不同。

      为了证明这一点,从 IP 偏向更改为区域,两个机器/浏览器/php 脚本应该返回相同的数据集。

      查看更多:

      locationbias — 首选指定区域的结果,通过指定 一个半径加上 lat/lng,或者两个 lat/lng 对代表 一个矩形的点。如果未指定此参数,则 API 默认使用 IP 地址偏置。

      IP 偏差:指示 API 使用 IP 地址偏差。传递字符串 ipbias(此选项没有附加参数)。

      来源:https://developers.google.com/places/web-service/search

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-07
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多