Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9315323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:20:06+00:00 2026-06-19T02:20:06+00:00

I was wondering if anyone could help me fine tune my script. I have

  • 0

I was wondering if anyone could help me fine tune my script.

I have what I need but I’m just trying to figure out how to make it recursive.

e.g. I currently have:

$key    = 'XXXXXXXXXXXX';
$sensor = 'false';
$query  = 'Place 1';

$url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?key='.$key.'&query='.urlencode($query).'&sensor='.$sensor;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$places = curl_exec($ch);
curl_close($ch);

// echo $url;

$output = json_decode($places);


$i = 0;


while ($output->results[$i]->geometry->location->lat != '') {
    echo '<strong>' . $query . '</strong><br />';
    echo $output->results[$i]->geometry->location->lat . ', '. $output->results[$i]->geometry->location->lng;
    echo '<br />' . $output->results[$i]->formatted_address;
    echo '<hr />';

    $i++;
}

// there is a delay between when the next page token is given and when it is ready to be accessed
sleep(5);

if ($output->next_page_token != '') {

    $url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken='.$output->next_page_token.'&key='.$key.'&sensor='.$sensor;

    // repeating myself now!

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $places = curl_exec($ch);
    curl_close($ch);

    $output = json_decode($places);

    $i = 0;

    while ($output->results[$i]->geometry->location->lat != '') {
        echo '<strong>' . $query . '</strong><br />';
        echo $output->results[$i]->geometry->location->lat . ', '. $output->results[$i]->geometry->location->lng;
        echo '<br />' . $output->results[$i]->formatted_address;
        echo '<hr />';

        $i++;
    }


}

So, ideally, I am looking at how to restructure so that the above will run for as long as there is a next page token.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:20:07+00:00Added an answer on June 19, 2026 at 2:20 am

    At the very least you could replace the if with a while. And maybe re-factor the body of the while into a function.

    But you don’t need to call it recursively, just iteratively until you’re done (i.e. no more next page token)

    function doQuery($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $places = curl_exec($ch);
        curl_close($ch);
    
        $output = json_decode($places);
    
        $i = 0;
    
        while ($output->results[$i]->geometry->location->lat != '') {
            echo '<strong>' . $query . '</strong><br />';
            echo $output->results[$i]->geometry->location->lat . ', '. $output->results[$i]->geometry->location->lng;
            echo '<br />' . $output->results[$i]->formatted_address;
            echo '<hr />';
    
            $i++;
        }
    
        sleep(5);
    
        return $output->next_page_token;
    }
    
    $next_page_token = doQuery($url);
    
    while ($next_page_token != '') 
    {
       $url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken='.$next_page_token.'&key='.$key.'&sensor='.$sensor;
    
       $next_page_token = doQuery($url);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just wondering if anyone could help me. I have a file containing a line:
I was just wondering if anyone could help out with how to do the
was wondering if anyone could help me out. I have been looking all over
I was just wondering if anyone could help me out with this query. In
I was wondering if any one could help me out; I have a table
I was wondering if anyone could help me. I'm trying to use the PHP
I was wondering if anyone could help me. I have made an fiddle so
Just wondering if anyone could help me with this. I'm new to actionscript, and
I was wondering if anyone could help me with this. I have to create
was wondering if anyone could help me improve my code. Its just some basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.