PHP get_headers()报告不同于CURL的头

问题描述:

get_headers()可能会返回不同于通过CURL获取结果的结果吗?这是我的代码:

How is it possible that get_headers() could possibly return a different result than getting them by CURL? Here is my code:

header("Content-type: text/plain");
$url = 'http://www.foxbusiness.com/index.html';

echo "get_headers() headers:\n\n";
$headers = get_headers($url);
print_r($headers);

echo "\n\nCURL headers\n\n";
$curl = curl_init();
curl_setopt_array( $curl, array(
    CURLOPT_HEADER => true,
    CURLOPT_NOBODY => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $url ) );
$headers = explode( "\n", curl_exec( $curl ) );
curl_close( $curl );
print_r($headers);

这是结果:

get_headers() headers:

Array
(
    [0] => HTTP/1.0 403 Forbidden
    [1] => Server: AkamaiGHost
    [2] => Mime-Version: 1.0
    [3] => Content-Type: text/html
    [4] => Content-Length: 283
    [5] => Expires: Fri, 31 Aug 2012 07:29:14 GMT
    [6] => Date: Fri, 31 Aug 2012 07:29:14 GMT
    [7] => Connection: close
)


CURL headers

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Server: Apache
    [2] => X-FoxNews-EdgeTTL: 2m
    [3] => Content-Type: text/html;charset=UTF-8
    [4] => Cache-Control: max-age=64
    [5] => Date: Fri, 31 Aug 2012 07:29:14 GMT
    [6] => Connection: keep-alive
    [7] => 
    [8] => 
)


get_headers GET请求默认情况下,您配置cURL做一个HEAD请求。首先,请求请求与cURL发送的内容完全相同,方法是将其他 HTTP流上下文 将HEAD用于请求方法

get_headers will do a GET request by default while you configured cURL to do a HEAD request. Start by making the request identical to what cURL sends by putting a different HTTP stream context using HEAD for the request method.

此外,服务器似乎需要一个用户代理,因此请确保您在php.ini中提供 user_agent 或将其添加到流上下文。

Also, the server seems to expect a User Agent, so make sure you either provide user_agent in php.ini or add it to the stream context.

以下应适用:

stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD',
            'user_agent' => "PHP"
        )
    )
);

请参阅 http://codepad.viper-7.com/cOO9XS

请注意, stream_context_set_default 修改全局默认的流上下文,所以任何调用其他方法使用此流封装器现在将做HEAD请求一旦你调用上述。不同于例如, file_get_contents get_headers 不允许通过函数的参数提供自定义流上下文。换句话说,确保您在获得标题后将方法重新更改为GET。

Note that stream_context_set_default modifies the global default Stream Context, so any calls to other methods using this stream wrapper will now do HEAD requests once you called the above. Unlike for example, file_get_contents, get_headers does not allow supplying a custom stream context via arguments to the function. In other words, make sure you change the method back to GET after you got the headers.