我使用以下代码进行发布请求:
<?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 谢谢!