Fernando Terra
Fernando Terra

Reputation: 81

get php virtual() response headers

The following makes a sub-request and outputs its body HTTP response content:

<?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.

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.

Upvotes: 4

Views: 477

Answers (2)

BlitZ
BlitZ

Reputation: 12168

Lets say, that current page has own original headers. By using virtual() you are forcing apache to perform sub-request, which generates additional virtual headers. You might get difference of those two header groups (by saving each with apache_response_headers()) by array_diff():

<?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:

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

Upvotes: 3

Ivo
Ivo

Reputation: 5420

You could use the HTTP Library - https://www.php.net/manual/en/httprequest.send.php

Upvotes: 0

Related Questions