Guzzle ~6 和 Google Place API - 使用分页时出错(pagetoken)

Guzzle ~6 and Google Place API - error when using pagination (pagetoken)

我正在尝试在几个地点 (lat/lng) 附近获得一组 universities

为此,我将 Google Maps Places API Web ServicesPHP 中的这个小脚本与 Guzzle ~6.0.

一起使用

我正在使用此代码获取 Google API:

$positions = [
    [51.388667, 2.557513], // <- no next_page_token
    [51.388667, 2.750000], // <- next_page_token
];

foreach($geo in $positions) {
    getJsonPlacesResponse($geo);
}

function getJsonPlacesResponse($geo)
{
    $lat = $geo[0];
    $lng = $geo[1];
    if ($lat == 'Latitude') return;
    $r1 = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?key='.getenv('GMAPS_API_KEY').'&radius=50000'.'&type=university'.'&location='.$lat.','.$lng;

    return getAndParse($r1);
}


function getAndParse($url)
{
    $client = new GuzzleHttp\Client();
    $body = $client->get($url, ['debug' => true])->getBody();

    $obj = json_decode($body, true);
    if ($obj['status'] !== 'OK') {
        return [];
    }

    if (isset($obj['next_page_token'])) {
        echo "--Get additionals results\n";
        return array_merge($obj['results'], getAndParse($url."&pagetoken=".$obj['next_page_token']));
    } else {
        return $obj['results'];
    }
}

此脚本工作正常,除非响应包含 "next_page_token",在这种情况下 api return a HTTP/1.1 200 OK:

{
   "html_attributions" : [],
   "results" : [],
   "status" : "INVALID_REQUEST"
}

通过启用 debug mode 我可以在控制台中看到 url:

*   Trying 216.58.204.234...
* TCP_NODELAY set
* Connected to maps.googleapis.com (216.58.204.234) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: C:\cacert.pem
  CApath: none
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googleapis.com
*  start date: Dec 13 13:18:44 2017 GMT
*  expire date: Mar  7 13:01:00 2018 GMT
*  subjectAltName: host "maps.googleapis.com" matched cert's "*.googleapis.com"
*  issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*  SSL certificate verify ok.
> GET /maps/api/place/nearbysearch/json?key=AIzaSyAjKv57vnxrBpHQHFjUj3kzjJtBHn3PwgA&radius=50000&type=university&location=51.388667,2.750000&pagetoken=CqQCGwEAAMzahTCyfdinn6yQR9l0qp3oTPeETilcXXNQoYf3BKN-Nolz7nPJmWDx4ydLl0ZG42jo52uYIDG1kmHTj3cvFK4boSfSWEuJHfeDVSkPjAgjyd9I
oqbu-TPvaioozcQwmfY7q7XA3_lhoiuvcZbHkUDm9ntx1ujBE1z4PjFIyMyuljY0E36-usj3f3Nq0VMBHGayLwnx9AraPyg-iMaSbDLjz5gKs2wYTz3mnKw-fhl064oI3MFWHi7u7d3PLDEyJ3m-YPZQ_tuqeIudpuc8GeItOzYMvnVD8nXYP0a12PyFx-2EeKutZlhZHtnRmRmHr74_Zzmx0dasuZMTI3Ot5y8j6EvCQhmo2CmLlj5mpedBIgnr_LDZBYqQn8YAdtk
izxIQ0G7SnNzr-chGUqQO6kwCQRoUmNgPs4xMYbYsQ0rdVsG-veFh31E HTTP/1.1
Host: maps.googleapis.com
User-Agent: GuzzleHttp/6.2.1 curl/7.55.0 PHP/7.1.10

< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Date: Mon, 15 Jan 2018 20:46:56 GMT
< Expires: Mon, 15 Jan 2018 20:51:56 GMT
< Cache-Control: public, max-age=300
< Server: scaffolding on HTTPServer2
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=":443"; ma=2592000; v="41,39,38,37,35"
< Accept-Ranges: none
< Vary: Accept-Language,Accept-Encoding
< Transfer-Encoding: chunked
<
* Connection #0 to host maps.googleapis.com left intact
{
   "html_attributions" : [],
   "results" : [],
   "status" : "INVALID_REQUEST"
}

但是当我测试 url /maps/api/place/nearbysearch/json?key=AIza...31E directly in Google Chrome, and it's just works fine: Screenshot of the Google Chrome Dev Tools.

而且我不知道问题出在哪里!

我在考虑一些 url 编码问题,但我读到 guzzle 已经对 url 进行了编码,所以是的,我不知道。

注意:我已经限制了密钥访问(基于 IP),所以它不应该为你使用这个特定的密钥。

感谢您的帮助。

Google Places API : next_page_token error

的副本

看到这条评论:

是的,添加一些延迟解决它。