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.
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);
?>
You could use the HTTP Library - http://au1.php.net/manual/en/httprequest.send.php
Related
I need to get the location header. From what I've read, this should be as simple as
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, false);
If I don't include those two options, the curl request works fine, but I'm not able to get the location header.
If I do include those two options, then I get a 401 error.
The Location that is being returned should be a URL that does require an additional login. What am I doing wrong?
If it makes a difference, I'm doing a PUT.
Update:
Turns out I was looking too much at the trees and not enough at the forest. When generated the information needed for a response to this question I realized the issue was actually with the call prior to this call.
Before the PUT, I need to get a session token. Since I wasn't parsing out the headers when getting the session token, I was getting a blank session token which was resulting in the 401 for the PUT.
HTTP response headers are included to the result of curl_exec when you set CURLOPT_HEADER option. So if you want to get response headers you have to extract it from the result using substr() and curl_getinfo(CURLINFO_HEADER_SIZE).
Here is a sample:
$c = curl_init();
// setting options including CURLOPT_HEADER
// ...
$result = curl_exec($c);
$info = curl_getinfo($c);
curl_close($c);
// extracting headers from result:
$n = $info['header_size'];
$headers = rtrim(substr($result, 0, $n));
$content = substr($result, $n);
I have, of course, read several questions with exactly this asked, but I have to say it didn't work for me at all. What I am about to accomplish is
sending 'X-Requested-With: XMLHttpRequest' header via PHP and curl
sending other http request headers via PHP and curl
provided solutions didn't work for me.
How do I know I'm not sending right http request headers?
Simply by
(1)comparing real headers generated by XMLHttpRequest(triggering JQuery click) and those simulated by PHP and curl in Firefox add-on Live HTTP headers
(2)Print_r() -ing $_SERVER variable in target script
What do I get that is incorrect/below my expectations?
First and most important:
Firefox Live HTTP headers does not capture my headers (just like they don't exists).
Second, by print_r($_SERVER):
if I get anything of simulated headers at all, I get [HTTP_X_REQUESTED_WITH] => XMLHttpRequest - not the: [X_REQUESTED_WITH] => XMLHttpRequest.
That problem persists almost for any header I send via curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header) - any of these is being prefixed with 'HTTP' ('Header1: value1' - I get 'HTTP_HEADER1').
I'm using XAMPP with PHP version 5.4.7, CURL 7.24.0 .
Before I ask if what I'm trying to accomplish is possible or maybe not and say thanks in advance for responses, it's not bad idea to provide my code - one of many code solutions that I've tried.
$curl_header = array('X-Requested-With: XMLHttpRequest');
$data = "name=miloshio"; // just to be sure I'm doing the POST request
$ch = curl_init('http://example.com/test.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
echo $result;
Sum of my questions:
Is it possible to send exactly 'X-Requested-With: XMLHttpRequest'
header via PHP and curl?
Is it possible to avoid attaching 'HTTP_' prefix to custom headers
send by PHP and curl?
Are there well-known limitations in matter of using PHP and curl?
Firefox Live HTTP headers won't show your headers as they're sent by the server to another server and not to the client(browser).
Curl send the headers correctly, using CURLOPT_PROXY You can try to put curl traffic through a debuging proxy like Fiddler if You're using windows for development, I'm sure there are linux alternatives
If you try to get the headers from $SERVER variable, they will be prefixed with HTTP, you can use apache_request_headers to get the headers without HTTP_ prefix.
I want to set a request header for a url xyz.com
is it the right way to set it in php?
header('Authorization: AuthSub token="xxxxxx"');
header('location:https://www.google.com/accounts/AuthSubRevokeToken');
I am trying to set the header for this URL for a call.But the Authorization: AuthSub header doesnt shows up in the request headers section of the FireFox NET panel.Which is used to show the requests.
Any idea about it?
Thanx.
I was using curl previously,But it didnt seemed to issue any request as i cant see it in the NET panel of FireFox.
Code is as follows:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"https://www.google.com/accounts/AuthSubRevokeToken");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="1/xxx"'
));
$result = curl_exec($curl);
curl_close($curl);
echo 'hererer'.$result;exit;
header sets response headers, not request headers. (If you were trying to send a HTTP request elsewhere, it would have no effect.)
Please also note what the manual says about Remember that header() must be called before any actual output is sent, ....
And turn on error_reporting(E_ALL); before using header() to see if that is the issue for you.
Header names and values need to be separated by one colon plus a space, so the location "header" is just wrong, it should be:
header('Location: https://www.google.com/accounts/AuthSubRevokeToken');
(It's common to write the case this way, too, but not a need)
Next to that the header function is setting response headers, not request headers. So you're basically using the wrong tool.
In PHP you can not set request headers, that's part of the client (e.g. browser), not the server. So header just looks wrong here. Which HTTP client are you using?
A call, as in using CURL to request another page? The header() function applies only for web-browser<->server communications. It cannot affect any requests your server-side script does to other webservers. For that, you need to modify the particular method you're using, e.g. curl or streams.
For curl, see CURLOPT_HTTPHEADER here: http://php.net/curl_setopt
I want send some data to a remote webpage from my site. Actually it can be achieved through form hidden variables. but for security reason, i want set as post variables in header and then send to that webpage. i use this code
$post_data = 'var1=123&var2=456';
$content_length = strlen($post_data);
header('POST http://localhost/testing/test.php HTTP/1.1');
header('Host: localhost');
header('Connection: close');
header('Content-type: application/x-www-form-urlencoded');
header('Content-length: ' . $content_length);
header($post_data);
but my code doesn't work properly.
help me...
POST data forms the body of an HTTP request, it isn't a header, and the header method is used in making HTTP responses.
askapache has an example of making a POST request from PHP using the curl library.
You're trying to put request headers into answer. Client just don't know what to do with it.
What you are trying to achieve is impossible.
What is the task you're trying to accomplish? There can be another way.
If you're trying to post a request to remote server, you'll need to use a tool like cUrl. Here's an exmple:
// Create a curl handle to a non-existing location
$ch = curl_init('http://localhost/testing/test.php');
// Set options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$page = curl_exec($ch);
Alternatively, if you really want to put data in the headers, you can set custom headers using something like this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('field: value', 'field2: value') );
The only way to redirect POST Header is an HTTP 1.1 Redirect with 307/308
307 Temporary Redirect
header('HTTP/1.1 307 Redirect');
header('Location: target.php', false, 307);
308 Permanent Redirect
header('HTTP/1.1 308 Redirect');
header('Location: target.php', false, 308);
!!! Only works in PHP if the Redirect Code is setted 2 times in this way !!!
If you only do the 2nd line. PHP made a normal 302 redirect.
Client get a message from browser if to accept this redirect, because of security reasons.
I receive HTTP PUT requests on a server and I would like to redirect / forward these requests to an other server.
I handle the PUT request on both server with PHP.
The PUT request is using basic HTTP authentication.
Here is an example :
www.myserver.com/service/put/myfile.xml
redirect to
www.myotherserver.com/service/put/myfile.xml
How can I do this without saving the file on my first server and resending a PUT request using CURL?
Thanks!
HTTP/1.1 defines status code 307 for such redirect. However, PUT is normally used by client software and you can pretty much assume no one honors 307.
The most efficient way to do this is to setup a proxy on Apache to redirect the request to the new URL.
This is how you can proxy it in PHP,
$data = file_get_contents('php://input');
$mem = fopen('php://memory');
fwrite($mem, $data);
rewind($mem);
$ch = curl_init($new_url);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $mem);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
fclose($meme);
Not possible. A redirect is implicitly a GET request. You'll need to have to play for a proxy using curl.
Saving on disk is technically also not necessary, you could just pipe the reponse body directly to the request body of Curl. But since I've never done this in PHP (in Java it's a piece of cake), I can't give a more detailed answer about that.