获取php virtual()响应头

时间:2013-10-05 17:59:37

标签: php reverse-proxy response-headers

以下内容生成子请求并输出其正文HTTP响应内容:

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

有没有办法获取其回复标题?

我的目标是将我的请求(带有请求标头)转发到其他主机上的其他位置,这是通过Apache ProxyPass指令完成的,并将其响应(标头和内容)设置为对我的请求的响应。

所以我的服务器将充当反向代理。但它会在转发请求之前测试一些需要完成php上下文的条件。

2 个答案:

答案 0 :(得分:3)

可以说,当前页面有自己的original标题。通过使用virtual(),您强制apache执行子请求,从而生成额外的virtual标头。您可以通过apache_response_headers()将这两个标题组(通过array_diff()保存每个标题组)区别开来:

<?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);
?>

答案 1 :(得分:0)

相关问题