我正在尝试为Google Places API制作一个迭代式抓取工具。正如使用API的任何人所知道的那样,该响应每页仅包含20个结果,并且我尝试获取结果的完整列表,因此我创建了一个循环,如果next_page_token字段存在,它将执行一个函数。最奇特的事情发生是因为它成功检索并回显了前20个结果,但未能执行第二个搜索,但是在Laravel中没有引发任何错误。用于此的代码如下所示(在Laravel中)
public function getAllPlaces(){
if (Auth::user()->id != '5') {
return redirect('/userlanding')->with('error', 'Denied access');
} else {
$client = new Client();
$types = 'park';
$radius = '50000';
$location = [
'latitude' => '36.187740',
'longitude' => '-92.731422'
];
$keyword = '';
$requestQuery = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$location['latitude']},{$location['longitude']}&radius={$radius}&type={$types}{$keyword}&key=MYSUPERSECRETKEY";
$response = $client->request('GET', $requestQuery);
$statusCode = $response->getStatusCode();
$body = $response->getBody()->getContents();
$body = json_decode($body);
$numofrecordspresent = 0;
$data = $body->results;
$results = collect();
foreach($data as $result) {
$results->push($result;
$numofrecordspresent = $numofrecordspresent + 1;
}
}
if(isset($body->next_page_token)) {
$loopData = [
'nexttoken' => $body->next_page_token,
'locationData' => $location,
'numofrecordspresent' => $numofrecordspresent,
'radius' => $radius,
'types' => $types,
'keyword' => $keyword,
'client' => $client,
];
$this->loopedPlaceSearch($loopData);
} else {
return view('apireturnpage')->with('numofrecordspresent', $numofrecordspresent);
}
}
}
public function loopedPlaceSearch($loopData) {
$client = new Client();
$location = $loopData['locationData'];
$numofrecordspresent = $loopData['numofrecordspresent'];
$pagetoken = $loopData['nexttoken'];
$radius = $loopData['radius'];
$types = $loopData['types'];
$keyword = $loopData['keyword'];
$requestQuery = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$location['latitude']},{$location['longitude']}&radius={$radius}&type={$types}{$keyword}&pagetoken={$pagetoken}&key=MYSUPERSECRETKEY";
$response = $client->request('GET', $requestQuery);
$statusCode = $response->getStatusCode();
$body = $response->getBody()->getContents();
$body = json_decode($body);
$data = $body->results;
$results = collect();
foreach($data as $result) {
$results->push($result;
$numofrecordspresent = $numofrecordspresent + 1;
}
}
if (isset($body->next_page_token)) {
$loopData = [
'nexttoken' => $body->next_page_token,
'locationData' => $location,
'numofrecordspresent' => $numofrecordspresent,
];
$this->loopedPlaceSearch($loopData);
} else {
return view('apireturnpage')->with('numofrecordspresent', $numofrecordspresent);
}
}
目前,要详细说明我之前所说的内容,第一个函数是从web.php调用的那个函数,它可以正常工作,将所有20个结果推入结果集合,但是当有更多结果时(即next_page_token为当前),第二个函数被调用,但在主体中返回“ INVALID_REQUEST”。这是非常奇怪的,因为从已使用但失败的第二个函数中获取确切的实际查询字符串,然后将其放入我的URL栏中会返回正确的响应,从而排除了API调用中的任何错字。
任何帮助将不胜感激,似乎是一个错误,我不知道为什么它不起作用。
[您似乎在两次结果推送时都缺少右括号: