Curl头请求返回404,但主体返回200


Curl header request returning 404 but body returning 200

我正在使用以下代码发送带有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。

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,因此它被拒绝了。