简体   繁体   English

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

[英]PHP Stream - Get all HTTP request headers

I do a post request with the following code: 我用以下代码发出请求:

<?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;

I can easily get response headers from my handmade HTTP request with the function stream_get_meta_data. 我可以使用stream_get_meta_data函数从手工HTTP请求中轻松获取响应标头 Is it possible to get all request headers ? 是否可以获取所有请求标头 Are there any built-in function? 有内置功能吗?

EDIT 编辑

I got the raw response headers with tcpflow: https://superuser.com/a/23187 Thanks! 我用tcpflow得到了原始响应头: https ://superuser.com/a/23187谢谢!

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

Get all headers should do the trick for you - note though it is dependent on how certain php versions and methods of running php from your web server. 获取所有标头应为您解决问题-注意,尽管它取决于某些php版本和从Web服务器运行php的方式。

eg 例如

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

More Details are included on the link above. 更多详细信息包含在上面的链接中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM