簡體   English   中英

PHP流-獲取所有HTTP請求標頭

[英]PHP Stream - Get all HTTP request headers

我用以下代碼發出請求:

<?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/zh/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.

更多詳細信息包含在上面的鏈接中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM