下面发出一个子请求并输出它的 body HTTP 响应内容:
<?php
if (condition()) {
virtual('/vh/test.php');
}
?>
有没有办法获取它的响应头?
我的目标是将我的请求(带有请求 header )转发到其他主机上的其他位置,这是通过 Apache ProxyPass 指令完成的,并将其响应( header 和内容)设置为对我的请求的响应。
所以我的服务器将充当反向代理。但它会在转发请求之前测试一些需要完成 php 上下文的条件。
最佳答案
比方说,当前页面有自己的 original
header 。通过使用 virtual()
您正在强制 apache 执行子请求,这会生成额外的 virtual
header 。您可能会通过 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,它不会帮助您更改当前请求 header 。 :
To run the sub-request, all buffers are terminated and flushed to the browser, pending headers are sent too.
这意味着,您不能再发送 header 了。您应该考虑使用 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);
?>
关于php - 获取 php virtual() 响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19200953/