Guzzle〜6和Google Place API-使用分页时出错(pagetoken)
我正在尝试在几个位置(纬度/经度)附近获取一组universities
.
I'm trying to get a set of universities
near a couple of locations (lat/lng).
为此,我正在使用 Google Maps Places API网络服务以及PHP
中带有Guzzle ~6.0
的小脚本.
To do so I'm using the Google Maps Places API Web Services and this small script in PHP
with Guzzle ~6.0
.
我正在使用以下代码来获取Google API:
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 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返回HTTP/1.1 200 OK
:
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"
}
通过启用debug mode
,我可以在控制台中看到该URL:
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"
}
但是当我测试网址/映射/API/地点/nearbysearch/JSON键= AIza ... 31E 直接在Google Chrome中运行,效果很好: Google Chrome开发人员工具.
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!
我正在考虑一些url编码问题,但是我已经读过,已经在对url进行编码,所以,是的,我不知道.
I'm thinking about some url encoding issue, but I've read that guzzle already encode the url, so yep, I don't know.
注意:我已经限制了密钥访问(基于IP),因此它不适合您使用此特定密钥.
Note: I have restricted the key access (IP based) so it shouldn't work with this specific key for you.
感谢您的帮助.
重复的 Google Places API:next_page_token错误
查看此评论: https://stackoverflow.com/a/20499757/8738918
是的,添加一些延迟可以解决该问题.
And yes, adding a few delay resolve it.