我使用以下代码执行发布请求:
<?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谢谢!
http://php.net/manual/en/function.getallheaders.php
获取所有标头应该可以为您解决问题 - 请注意,尽管它取决于某些 php 版本和从您的 Web 服务器运行 php 的方法。
例如
PHP 5.4.0 This function became available under FastCGI. Previously, it was supported only when PHP was installed as an Apache module.
更多详细信息包含在上面的链接中。