我想检查域的状态码,以免给远程服务器带来太多负担,我希望确保两次请求之间有足够的时间。
为此,我需要取消对关联数组(id:url)和
的排序生成这样的队列:域a,域b,c,a,b,c,a,b,c,a ...
我尝试了以下操作,循环遍历各个域,并检查在最近3次迭代中是否已请求该域,然后继续下一个域并将其移至循环结束。
$urls_last_checked = array();
foreach ($check as $id => $url) {
// let's only monitor the last x urls with processed
// save domain to array in order to avoid to many requests to this URL in a short period of time
$domain = parse_url($url, PHP_URL_HOST);
// only continue with this domain if not checked before
if (in_array($domain, $urls_last_checked)){ // we processed it before continue with next one
echoerror ($domain." passed \n", $logfile);
unset($check[$id]);
$check[$id] = $url;
continue;
}
else{
// add this domain to array
$urls_last_checked[] = $domain;
// remove oldest domain we checked
if (count($urls_last_checked) > 3) array_shift($urls_last_checked);
echoerror (print_r($urls_last_checked).$domain." added \n", $logfile);
}
}
不幸的是,这不起作用,因为for循环将结束并且不考虑我添加的新数组元素。
如何确保前循环继续进行或以其他方式实现我的目标?