0

我想知道是否有人可以帮助我微调我的脚本。

我有我需要的东西,但我只是想弄清楚如何让它递归。

例如,我目前有:

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


}

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

4

2 回答 2

2

至少你可以ifwhile. 并且也许将 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);
}
于 2013-02-20T16:18:22.733 回答
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 != '');
于 2013-02-20T16:23:29.840 回答