简体   繁体   English

获取php virtual()响应头

[英]get php virtual() response headers

The following makes a sub-request and outputs its body HTTP response content: 以下是一个子请求并输出其正文HTTP响应内容:

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

Is there a way to get its response headers? 有没有办法获得其响应标头?

My goal is to forward my request (with request headers) to other location on other host, which is accomplished with Apache ProxyPass directive, and set its response (headers and content) as the response to my request. 我的目标是将我的请求(带有请求标头)转发到其他主机上的其他位置,这是通过Apache ProxyPass指令完成的,并将其响应(标头和内容)设置为对我的请求的响应。

So my server would act as a reverse proxy. 所以我的服务器将充当反向代理。 But it will test some condition which requires php context to be done, before forwarding the request. 但它会在转发请求之前测试一些需要完成php上下文的条件。

Lets say, that current page has own original headers. 可以说,当前页面有自己的original标题。 By using virtual() you are forcing apache to perform sub-request, which generates additional virtual headers. 通过使用virtual()您强制apache执行子请求,从而生成其他virtual标头。 You might get difference of those two header groups (by saving each with apache_response_headers() ) by array_diff() : 您可能会通过array_diff()获得这两个标头组(通过使用apache_response_headers()保存每个标头组)的apache_response_headers()

<?php
$original   = apache_response_headers();

virtual('somepage.php');

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

print_r($difference);
?>

However it will not help you to change current request headers because of this : 然而,它不会帮你去改变,因为目前的请求头这个

To run the sub-request, all buffers are terminated and flushed to the browser, pending headers are sent too. 要运行子请求,所有缓冲区都会终止并刷新到浏览器,也会发送待处理的标头。

Which means, that you can not send headers anymore. 这意味着,您不能再发送标头了。 You should consider usage of cURL instead: 您应该考虑使用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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM