Php 请求数之间的延迟
我使用simplexml通过while循环从另一个提供者加载数据。为了避免被防火墙阻止或淹没他们的服务器,我想在500个请求后通过睡眠增加一点延迟。这是我现有的代码Php 请求数之间的延迟,php,simplexml,Php,Simplexml,我使用simplexml通过while循环从另一个提供者加载数据。为了避免被防火墙阻止或淹没他们的服务器,我想在500个请求后通过睡眠增加一点延迟。这是我现有的代码 while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); // Load the XML $url = "http://www.domain.com/models.asp?txtYear=" . $line_of_
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
// Load the XML
$url = "http://www.domain.com/models.asp?txtYear=" . $line_of_text[0] . "&txtMake=" . $line_of_text[1];
$xmlinfo = simplexml_load_file($url);
$i = 0;
$value = (string) $xmlinfo->table[$i]->txtmodel;
while ($value != '') {
$value = (string) $xmlinfo->table[$i]->txtmodel;
if ($value != '') {
fputcsv($handle, array(
$line_of_text[0],
$line_of_text[1],
$value
));
} else {
// Do nothing
}
$i++;
}
}
我希望这很容易做到--我期待得到帮助!:-) 计算循环的每个实例,如果达到500,将其置于睡眠状态并重新启动:
$meter = 0;
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
// Load the XML
$url = "http://www.domain.com/models.asp?txtYear=" . $line_of_text[0] . "&txtMake=" . $line_of_text[1];
$xmlinfo = simplexml_load_file($url);
$i = 0;
$value = (string) $xmlinfo->table[$i]->txtmodel;
while ($value != '') {
$value = (string) $xmlinfo->table[$i]->txtmodel;
if ($value != '') {
fputcsv($handle, array(
$line_of_text[0],
$line_of_text[1],
$value
));
} else {
// Do nothing
}
$i++;
}
//Increase the meter by 1
$meter++;
//Check if the limit is reached and if yes, sleep for 60 seconds
if($meter == 500){
sleep(60);
$meter = 0;
}
}