How do I log the raw HTTP headers with a PHP script?
我正在使用cURL脚本通过代理将POST数据发送到脚本,并且我想查看cURL脚本正在发送哪些原始HTTP标头。我尝试过的事情清单:
-
echo curl_getinfo($ch, CURLINFO_HEADER_OUT) 不提供任何输出。 -
file_get_contents('php://input') 获取一些HTTP标头,但不是全部。 -
print_r($_SERVER) 还获得了一些HTTP标头,但不是全部(我知道这一点,因为应该有一个X-Forwarded-For标头,而没有) - 打印所有超全局变量($ _POST,$ _ GET,$ _ REQUEST,$ _ FILES等)仍不会显示原始HTTP标头。
-
http_get_request_headers() ,apache_request_headers() ,$http_response_header ,$HTTP_RAW_POST_DATA 不会输出所有内容。
帮助?
打开CURLOPT_HEADER,而不是CURLINFO_HEADER_OUT,然后在\\\\上拆分
\\\\
\\\\
\\\\
(标头结束的位置),最大拆分计数为2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php $ch = curl_init('http://www.yahoo.com/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); $result = curl_exec($ch); if ($result !== false) { $split_result = split("\ \ \ \ ", $result, 2); $header = $split_result[0]; $body = $split_result[1]; /** Process here **/ } else { /** Error handling here **/ } |
您还需要设置
CURLINFO_HEADER_OUT
TRUE to track the
handle's request string.
Available
since PHP 5.1.3. The CURLINFO_ prefix
is intentional.
http://www.php.net/manual/zh/function.curl-setopt.php
以下作品:
1 2 3 4 5 6 7 8 9 | <?php $ch = curl_init('http://www.google.com'); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); echo curl_getinfo($ch, CURLINFO_HEADER_OUT); |
如果您在Apache上作为模块运行,则apache_request_headers()可以满足您的需求。
对于几乎任何其他体系结构,您只能选择$ _SERVER中记录的内容,或者您??需要找到某种方法来使用Web服务器配置记录信息。