0

我正在测试同一域上的大量链接,以查看它们是否存在。我正在使用以下代码:

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 错误。为什么会有这样的差异?我应该设置超时吗?任何帮助将不胜感激。

4

1 回答 1

0

正如我从您的代码中了解到的那样,问题出在变量错误$url 尝试这个。

...
foreach($allResults as $result)
{
    $tempURL = 'http://www.doma.in/foo/'.$result['url'].'/bar';
...

$url变成$result

于 2013-03-04T23:45:31.377 回答