2013-10-05 66 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); 
?>