如何使脚本递归

时间:2013-02-20 16:14:30

标签: php google-places-api

我想知道是否有人可以帮我调整我的剧本。

我有我需要的东西,但我只想弄清楚如何使其递归。

e.g。我目前有:

$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++;
    }


}

所以,理想情况下,我正在研究如何进行重组,以便只要有下一页令牌,上述内容就会运行。

2 个答案:

答案 0 :(得分:2)

至少你可以用if替换while。并且可能将while的主体重新分解为函数。

但你不需要递归地调用它,只需迭代地调用它,直到你完成(即不再有下一页令牌)

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);
}

答案 1 :(得分:1)

在一个函数中封装你的逻辑,然后在你有下一个页面标记时循环:

function doWorkAndPrint( $url, $query) {
    $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++;
    }

    return $output->next_page_token;
}

现在,你需要循环,而该函数返回一些有用的东西。既然你想要至少做一次,我会使用do ... while循环:

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

do {
    $url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken='.$query.'&key='.$key.'&sensor='.$sensor;
    $query = doWorkAndPrint( $url, $query);
} while( $query != '');