Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

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

I'm trying to get a set of universities near a couple of locations (lat/lng).

To do so I'm using the Google Maps Places API Web Services and this small script in PHP with Guzzle ~6.0.

I'm using this code to fetch Google APIs:

$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 GuzzleHttpClient();
    $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
";
        return array_merge($obj['results'], getAndParse($url."&pagetoken=".$obj['next_page_token']));
    } else {
        return $obj['results'];
    }
}

This script works just fine exept when the response contain a "next_page_token", in that case the api return a HTTP/1.1 200 OK:

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

By enabling the debug mode I can see the url in the console:

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

But when I test the 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.

And I don't know where is the problem!

I'm thinking about some url encoding issue, but I've read that guzzle already encode the url, so yep, I don't know.

Note: I have restricted the key access (IP based) so it shouldn't work with this specific key for you.

Thanks for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Duplicate of Google Places API : next_page_token error

See this comment: https://stackoverflow.com/a/20499757/8738918

And yes, adding a few delay resolve it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...