I am developing an google places API application where my target to get all available details of a specific query. But as I know google places API only returns 20 results at a time.But I need all available place details.
If I search with a search keyword more than once its always gives the same results. So I want to know that is there any way to get different results each time I search with the same keyword.
<?php
$placeSearchURL = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' . $query . '&sensor=true' . '&key=' . API_KEY;
$placeSearchJSON = file_get_contents($placeSearchURL);
$dataArray = json_decode($placeSearchJSON);
$references = array();
$detailsOfPlaces = array();
if (isset($dataArray->status)) {
switch ($dataArray->status) {
case 'OK' :
foreach( $dataArray->results as $details) {
$references[] = $details->reference;
}
break;
default:
echo "Something wrong";
}
}
foreach ($references as $reference) {
$placeDetailsURL = 'https://maps.googleapis.com/maps/api/place/details/json?reference=' . $reference . '&sensor=true&key=' . API_KEY;
$placeDetails =file_get_contents($placeDetailsURL);
$placeDetailsAsArray = json_decode($placeDetails);
switch ($dataArray->status) {
case 'OK' :
$detailsOfPlace['business_name'] = isset($placeDetailsAsArray->result->name) ? $placeDetailsAsArray->result->name : '';
$detailsOfPlace['address'] = isset($placeDetailsAsArray->result->formatted_address) ? $placeDetailsAsArray->result->formatted_address : '';
$detailsOfPlaces[] = $detailsOfPlace;
break;
default:
echo "Something went wrong";
}
}
This is my code. It returns 20 results for each query. Can anyone please tell me what adjustment I should made to get 60 results for each query.
Thanks
As Chris Green says in his answer you will have to use pagination to get more than 20 results.
Also as Chris Green also states to take the address you dont really need to make another call to the reference of each place.I am assuming that you will need more information so I just created 2 function implementing what Chris says in his response.
Not tested so might be some errors but you can get the feeling.