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

时间:2018-01-15 21:16:02

标签: php google-places-api guzzle

我正试图在几个地点(纬度/经度)附近获得一组universities

为此,我使用PHP~6.0中的小脚本与Guzzle $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']; } }

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

HTTP/1.1 200 OK

当响应包含“next_page_token”时,此脚本工作正常,在这种情况下,api返回{ "html_attributions" : [], "results" : [], "status" : "INVALID_REQUEST" }

debug mode

启用* 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" } 我可以在控制台中看到网址:

#define PRODUCT "MY PRODUCT B"

但是,当我直接在Google Chrome中测试网址Google Maps Places API Web Services时,它运行正常:/maps/api/place/nearbysearch/json?key=AIza...31E

我不知道问题出在哪里!

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

注意:我限制了密钥访问(基于IP),因此它不适合您使用此特定密钥。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

重复Google Places API : next_page_token error

看到此评论: https://stackoverflow.com/a/20499757/8738918

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