我正在测试同一域上的大量链接,以查看它们是否存在。我正在使用以下代码:
function get_http_response_code($url)
{
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
function getURLs()
{
foreach($allResults as $result)
{
$tempURL = 'http://www.doma.in/foo/'.$result.'/bar';
if(get_http_response_code($tempURL) != "404" && get_http_response_code($tempURL) != "500")
{
$URLs[] = $tempURL;
}
else
{
echo $tempURL.' could not be reached<br />';
}
return $URLs;
}
$URLs = getURLs();
问题是,在确实存在的数百个中,该$URLs
数组包含不存在的 URL (404);有时是两个,有时是四个,但每次都会产生 HTTP/1.0 404 Not Found 错误。为什么会有这样的差异?我应该设置超时吗?任何帮助将不胜感激。