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

get_headers()有可能返回与通过CURL获取它们不同的结果吗?这是我的代码:
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] => 
)
在配置cURL以执行HEAD请求时,get_headers将默认执行GET请求.首先,通过输入不同的 HTTP stream context using HEAD for the request method.,使请求与cURL发送的请求相同

此外,服务器似乎期望用户代理,因此请确保provide user_agent in php.ini或将其添加到流上下文.

以下应该有效:

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

http://codepad.viper-7.com/cOO9XS

请注意,stream_context_set_default修改了全局默认的Stream Context,因此在调用上述内容后,使用此流包装器对其他方法的任何调用现在都会执行HEAD请求.与例如file_get_contents不同,get_headers不允许通过函数的参数提供自定义流上下文.换句话说,确保在获得标题后将方法更改回GET.

相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...