卷曲头请求返回404但是主体返回200

时间:2014-06-13 18:52:36

标签: php curl http-headers content-type

我正在使用以下代码发送带有curl的标头请求

function getContentType($u)
{
    $ch = curl_init();
    $url = $u;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.12011-10-16 20:23:00");

    $results = split("\n", trim(curl_exec($ch)));
    print_r($results);
    foreach($results as $line) {
        if (strtok($line, ':') == 'Content-Type') {
            $parts = explode(":", $line);
            return trim($parts[1]);
        }
    }
}

对于大多数网站而言,它正确返回,但对于某些服务器,当页面实际可用时,它会返回404错误。我假设这是因为服务器已配置为拒绝标头请求。

我正在寻找一种绕过此服务器标头请求拒绝的方法,或者一种方法来判断标头请求是否已被拒绝且实际上不是404.

1 个答案:

答案 0 :(得分:2)

Setting CURLOPT_NOBODY to "true" with curl_setopt sets the request 
method to HEAD for HTTP(s) requests, and furthermore, cURL does not read 
any content even if a Content-Length header is found in the headers. 
However, setting CURLOPT_NOBODY back to "false" does *not* reset the 
request method back to GET. But because it is now "false", cURL will 
wait for content if the response contains a content-length header. 

我的猜测是你正在使用HEAD请求而不是GET,因此被拒绝了。

相关问题