【问题标题】:Google Places API : next_page_token errorGoogle Places API:next_page_token 错误
【发布时间】:2012-12-27 14:45:26
【问题描述】:

我正在收集有关商店位置的信息。 搜索是:

<?php
...
$url='https://maps.googleapis.com/maps/api/place/search/json?key=[my_key]&location=40.420989,-3.706812&radius=1000030&=&sensor=false';
$body=file_get_contents($url);
...
?>

我返回一个没有问题的Json,表示还有另一页结果。 我会回来打另一个电话,如下所示

<?php
...
$url2='https://maps.googleapis.com/maps/api/place/search/json?key=[my_key]&pagetoken=ClREAAAAQXKNHPVGCkTC_MdjSqi2T0KBDMWjEu4KF1Ylw1761Po-67AnNSp4zw0wXD4oocGpx4olSl4k2LyklJBl3mBF4VPmxp3IoOCHDRlXVmivaDsSEBuG_V1GvCH1gS5s0LCuy3EaFNXxUzzHhZ5ZRYNfeGHuqewR6Zk7&sensor=false';
$body=file_get_contents($url2);
...
?>

如果我在第二次调用时运行它,我会收到错误

'状态' -> INVALID_REQUEST

但是当我在浏览器中粘贴ulr2时结果是正确的。

我该如何解决?

谢谢

【问题讨论】:

  • 你发现问题了吗,它还返回我请求被拒绝的错误

标签: google-places-api


【解决方案1】:

这与请求之间的时间有关,如果您一个接一个地立即运行它们,则pagetoken还无效,您必须在连续请求之间等待几秒钟。

这是因为 google 的许可条款不允许您一次获取所有结果并将它们一次全部返回给用户。您应该有一个用户操作要求更多结果,这会增加几秒钟的延迟。

【讨论】:

  • @allemattio 你是对的,这个答案应该被标记为正确,它也拯救了我的一天。
  • 这是正确的答案 - 在请求前增加了 5 秒的睡眠时间,一切都很好!
  • 我认为这是一种非常糟糕的行为......我没想到谷歌会这样做。
  • (编辑队列已满,否则我会插入答案)来自docs (archive):在发出 next_page_token 和发出它将变得有效。在下一页可用之前请求下一页将返回 INVALID_REQUEST 响应。
【解决方案2】:

请求之间的sleep(2)会解决问题

【讨论】:

  • 很棒的解决方案!!
【解决方案3】:

请尝试下面的代码,我使用 sleep(2) 函数来延迟请求之间的时间,因为下一个 pagetoken 需要在 google 服务器上进行验证。 您甚至可以使用循环来消除代码重复。

//您的查询在这里

$query = "";

//这里有api键

$api_key = ""; 

// api调用代码

    try {
        echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key;
        echo "<br>";
        $result = file_get_contents($url);
        $query_results = json_decode($result, true);
        echo "First set" . "<br>";
        print_r($query_results);
        $next_page_token = $query_results['next_page_token'];
        unset($query_results);
        $query_results = array();

        sleep(2);
        echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key . "&pagetoken=" . $next_page_token;
        echo "<br>";
        $result = file_get_contents($url);
        $query_results = json_decode($result, true);
        echo "Second set" . "<br>";
        print_r($query_results);
        $next_page_token = $query_results['next_page_token'];
        unset($query_results);
        $query_results = array();

        sleep(2);
        echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key . "&pagetoken=" . $next_page_token;
        echo "<br>";
        $result = file_get_contents($url);
        $query_results = json_decode($result, true);    
        echo "Third set" . "<br>";
        print_r($query_results);
        unset($query_results);
        $query_results = array();
    } catch (Exception $e) {
        $e->getCode();
        $e->getLine();
    }

【讨论】:

    【解决方案4】:

    Sleep(1.3) 是似乎有效的最短时间。换句话说,下一页令牌在上一个 API 请求中返回后大约 1.3 秒变为活动状态。

    【讨论】:

      【解决方案5】:

      第一个查询将生成第二个页面令牌。 您只需在 uri 中添加“&pagetoken=tokenvalue”。

      当然可以。没有其他选择。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2012-05-10
      • 1970-01-01
      相关资源
      最近更新 更多