Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places API : next_page_token error

I'm gathering information on the location of stores. The search is:

<?php
...
$url='https://maps.googleapis.com/maps/api/place/search/json?key=[my_key]&location=40.420989,-3.706812&radius=1000030&=&sensor=false';
$body=file_get_contents($url);
...
?>

I return a Json without problems, and indicates that there is another page of results. I'll be back to make another call as follows

<?php
...
$url2='https://maps.googleapis.com/maps/api/place/search/json?key=[my_key]&pagetoken=ClREAAAAQXKNHPVGCkTC_MdjSqi2T0KBDMWjEu4KF1Ylw1761Po-67AnNSp4zw0wXD4oocGpx4olSl4k2LyklJBl3mBF4VPmxp3IoOCHDRlXVmivaDsSEBuG_V1GvCH1gS5s0LCuy3EaFNXxUzzHhZ5ZRYNfeGHuqewR6Zk7&sensor=false';
$body=file_get_contents($url2);
...
?>

If I run it with the second call I get an error

'status' -> INVALID_REQUEST

But when I paste the ulr2 browser in the result is correct.

How I can fix it?

Thanks

like image 333
user1932300 Avatar asked Dec 27 '12 14:12

user1932300


Video Answer


1 Answers

It has something to do with the timing between the requests, if you run them immediately one after the other, the pagetoken isn't valid yet, you have to wait a few seconds between consecutive requests.

This is because google's license terms don't allow you to fetch all the results at once and return them all at once to the user. You should have a user action asking for more results, which adds a delay of a couple of seconds.

like image 88
dirkgroten Avatar answered Oct 07 '22 01:10

dirkgroten