2013-10-05 65 views
4

下使一個子請求,並輸出其身體HTTP響應內容:讓PHP虛擬()響應頭

<?php 
if (condition()) { 
    virtual('/vh/test.php'); 
} 
?> 

是否有一種方式來獲得它的響應頭?

我的目標是轉發我的請求(請求頭)到其他主機,這是與Apache ProxyPass指令來實現對其他位置,並設置其響應(標題和內容)爲迴應我的請求。

所以我的服務器將充當反向代理。但是它會在轉發請求之前測試一些需要php上下文的條件。

+3

如果你真的堅持用PHP做它,嘗試捲曲:http://www.php.net/manual /en/intro.curl.php –

回答

3

可以說,當前頁面有自己的original標題。通過使用virtual(),您迫使apache執行子請求,該請求會生成額外的virtual標題。你可能會array_diff()得到這兩個首標組的差異(通過保存每個apache_response_headers()):

<?php 
$original = apache_response_headers(); 

virtual('somepage.php'); 

$virtual = apache_response_headers(); 
$difference = array_diff($virtual, $original); 

print_r($difference); 
?> 

但是它不會幫助你改變,因爲this當前請求頭:

要運行子請求,所有緩衝區終止並刷新到 瀏覽器,等待標題也被髮送。

這意味着,你不能再發送標題。你應該考慮的cURL使用來代替:

<?php 
header('Content-Type: text/plain; charset=utf-8'); 

$cUrl = curl_init(); 

curl_setopt($cUrl, CURLOPT_URL, "http://somewhere/somepage.php"); 
curl_setopt($cUrl, CURLOPT_HEADER, true); 
curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true); 

$response = curl_exec($cUrl); 
curl_close($cUrl); 

print_r($response); 
?>