2013-10-25 200 views
0

我用下面的代碼的POST請求:PHP流 - 獲取所有HTTP請求頭

<?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 謝謝!

+0

在你的情況下,沒有請求頭。上下文中的所有內容都在您的選項數組中。 – Spell

+0

那麼,你將它們設置在'$ options'中 - 那麼有什麼問題呢? –

+0

對不起,我的意思是,RAW請求標題...我想要捕獲整個請求流到達服務器的方式。 –

回答

0

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. 

更多細節包含在上面的鏈接。

+1

它不能解決我的問題。我需要從fopen調用中獲取原始請求標頭,而不是從當前頁面的請求中獲取。 –