Google Places API : next_page_token error

后端 未结 5 1931
[愿得一人]
[愿得一人] 2021-02-19 13:39

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



        
相关标签:
5条回答
  • 2021-02-19 13:47

    sleep(2) between requests will solve the problem

    0 讨论(0)
  • 2021-02-19 13:47

    Please try below code, I have used sleep(2) function for delay between requests, because next pagetoken needs to be validated on google server. You can even use looping to remove code repetation.

    // your query here

    $query = "";
    

    // api key here

    $api_key = ""; 
    

    // api call code

        try {
            echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key;
            echo "<br>";
            $result = file_get_contents($url);
            $query_results = json_decode($result, true);
            echo "First set" . "<br>";
            print_r($query_results);
            $next_page_token = $query_results['next_page_token'];
            unset($query_results);
            $query_results = array();
    
            sleep(2);
            echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key . "&pagetoken=" . $next_page_token;
            echo "<br>";
            $result = file_get_contents($url);
            $query_results = json_decode($result, true);
            echo "Second set" . "<br>";
            print_r($query_results);
            $next_page_token = $query_results['next_page_token'];
            unset($query_results);
            $query_results = array();
    
            sleep(2);
            echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key . "&pagetoken=" . $next_page_token;
            echo "<br>";
            $result = file_get_contents($url);
            $query_results = json_decode($result, true);    
            echo "Third set" . "<br>";
            print_r($query_results);
            unset($query_results);
            $query_results = array();
        } catch (Exception $e) {
            $e->getCode();
            $e->getLine();
        }
    
    0 讨论(0)
  • 2021-02-19 13:51

    Sleep(1.3) is the shortest amount of time that seemed to work. In other words the next page token becomes active about 1.3 seconds after it is returned in the previous API request.

    0 讨论(0)
  • 2021-02-19 13:52

    The first query will generate 2nd page token. You just add "&pagetoken=tokenvalue" in your uri.

    Sure it work. No alternative option.

    0 讨论(0)
  • 2021-02-19 13:54

    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.

    0 讨论(0)
提交回复
热议问题