Closed markjanenriquez closed 4 years ago
@markjanenriquez you can pass it as a param for next search request.
$responses = $googlePlaces->nearbySearch($location, $radius = '30000', $params);
$params['next_page_token'] = $response['next_page_token'];
// This will get you 2nd page as next_page_token is in params
$responses = $googlePlaces->nearbySearch($location, $radius = '30000', $params);
Hello, thank you for your response, and I tried your code/suggestion but the response from the API result was the same here's my code.
$googlePlaces = new PlacesApi($apiKey); $params = [ 'keyword' => $keyword, 'name' => null, 'types' => null ];
$responses = $googlePlaces->nearbySearch($location, $radius = '30000', $params); $params['next_page_token'] = $responses['next_page_token']; $second_responses = $googlePlaces->nearbySearch($location, $radius = '30000', $params); dd($responses['results'], $second_responses ['results']);
Is there any other recommendation?
Thanks.
@markjanenriquez Sorry, the key should be pagetoken
$params['pagetoken'] = $response['next_page_token'];
Hello, I tried again I used this code
$params['pagetoken'] = $response['next_page_token']; But I got the error: "Response returned with status: INVALID_REQUEST"
@/vendor/skagarwal/google-places-api/src/PlacesApi.php if ($response['status'] !== 'OK' AND $response['status'] !== 'ZERO_RESULTS') { throw new GooglePlacesApiException( "Response returned with status: " . $response['status'] . "\n" . array_key_exists('error_message', $response) ?: "Error Message: {$response['error_message']}" ); }
And test again, I changed the $params['pagetoken'] = $response['next_page_token']; to (Capital T) $params['pageToken'] = $response['next_page_token'];
but the response from Google is the same here's the code I used. $googlePlaces = new PlacesApi($apiKey); $params = [ 'keyword' => $keyword, 'name' => null, 'types' => null ]; $responses = $googlePlaces->nearbySearch($location, $radius = '30000', $params); $params['pagetoken'] = $responses['next_page_token']; $second_responses = $googlePlaces->nearbySearch($location, $radius = '30000', $params); dd($responses['results'], $second_responses ['results']);
Thanks.
As per Google's documentation:
There is a short delay between when a next_page_token is issued, and when it will become valid.
Try doing this:
$google_place_api = [];
$resolved = false;
while(!$resolved){
try{
$google_place_api = GooglePlaces::nearbySearch($location, $radius, [
// your params
...
'pagetoken' => $google_place_api['next_page_token'] ?? null;
...
])
$resolved = true;
}catch(\Exception $e){
$resolved = false;
}
}
Or Alternatively use the sleep($seconds)
method
As per Google's documentation:
There is a short delay between when a next_page_token is issued, and when it will become valid.
Try doing this:
$google_place_api = []; $resolved = false; while(!$resolved){ try{ $google_place_api = GooglePlaces::nearbySearch($location, $radius, [ // your params ... 'pagetoken' => $google_place_api['next_page_token'] ?? null; ... ]) $resolved = true; }catch(\Exception $e){ $resolved = false; } }
Or Alternatively use the
sleep($seconds)
method
This helped me, it was exactly the issue! thanks.
Hi, Is there a way to get the second page using the "next_page_token" from the backend? Here's my code.
$googlePlaces = new PlacesApi($this->getGoogleApi()); $params = [ 'keyword' => $keyword, 'name' => null, 'types' => null ];
$responses = $googlePlaces->nearbySearch($location, $radius = '30000', $params);
But I want to get the second page using the "$responses['next_page_token']"
Thank You.