【问题标题】:php: inconsistent behaviour with HTTP headersphp:与 HTTP 标头的行为不一致
【发布时间】:2013-02-19 05:30:55
【问题描述】:

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

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

【问题讨论】:

  • 您的 getURLs 函数范围内没有 $url 变量。
  • 谢谢,错字,已编辑。这段代码经过大量编辑,但问题仍然是我如何描述它。
  • 你能看到服务器日志中的错误吗?
  • 那会是哪个日志?我正在使用 Apache。

标签: php url http-headers http-status-code-404 httpresponse


【解决方案1】:

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

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

$url改为$result

【讨论】:

  • 代码本身有效。我遇到了 HTTP 标头问题。
猜你喜欢
  • 2023-01-02
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 2010-12-28
相关资源
最近更新 更多