PHP Stream - 获取所有HTTP请求标头

时间:2013-10-25 11:54:57

标签: php http http-headers httprequest httpresponse

我使用以下代码发布帖子请求:

<?php
$url = 'http://www.example.com/';
$data = array('first-post-data' => 'data', 'second-post-data' => 'another-data');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
        'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0',

        // corporate proxy thing ...
        'proxy' => 'proxy-ip:proxy-port',
        'request_fulluri' => true,
    ),
);
$context  = stream_context_create($options);
$file = fopen($url, 'r', null, $context);
$content = stream_get_contents($file);

// Response headers:
echo '<pre>' . print_r(stream_get_meta_data($file), true) . '</pre>';

fclose($file);
echo $content;

我可以使用函数stream_get_meta_data从我手工制作的HTTP请求中轻松获取响应标头。是否可以获取所有请求标头?有内置功能吗?

修改

我使用tcpflow得到了原始响应标头:https://superuser.com/a/23187 谢谢!

1 个答案:

答案 0 :(得分:0)

http://php.net/manual/en/function.getallheaders.php

获取所有标题应该为您做的 - 请注意,虽然它取决于某些PHP版本和从您的Web服务器运行PHP的方法。

e.g。

PHP 5.4.0    This function became available under FastCGI. Previously, it was supported only when PHP was installed as an Apache module.

更多详细信息包含在上面的链接中。