我想知道是否有人可以帮助我微调我的脚本。
我有我需要的东西,但我只是想弄清楚如何让它递归。
例如,我目前有:
$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++;
}
}
所以,理想情况下,我正在研究如何重组,以便只要有下一页令牌,上述内容就会运行。