Reputation: 51
I'm using a cURL script to send POST data through a proxy to a script and I want to see what raw HTTP headers the cURL script is sending. List of things I've tried:
echo curl_getinfo($ch, CURLINFO_HEADER_OUT)
gives no output.file_get_contents('php://input')
gets some HTTP headers but not all.print_r($_SERVER)
also gets some HTTP headers but not all (I know this because there should be a X-Forwarded-For header and there isn't)http_get_request_headers()
, apache_request_headers()
, $http_response_header
, $HTTP_RAW_POST_DATA
aren't outputting everything.Help?
Upvotes: 5
Views: 5225
Reputation: 48357
If you running as a module on Apache then apache_request_headers() does what you need.
For just about any other architecture you'll only be able to pick out the stuff which is recorded in $_SERVER or you'll need to find some way to record the information using the web server config.
Upvotes: 0
Reputation: 347
Turn on CURLOPT_HEADER, not CURLINFO_HEADER_OUT, then split on \r\n\r\n (which is where the header ends) with a maximum split count of 2 :
<?php
$ch = curl_init('http://www.yahoo.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
if ($result !== false) {
$split_result = split("\r\n\r\n", $result, 2);
$header = $split_result[0];
$body = $split_result[1];
/** Process here **/
} else {
/** Error handling here **/
}
Upvotes: 2
Reputation: 57805
You need to also set the CURLINFO_HEADER_OUT
option:
CURLINFO_HEADER_OUT
TRUE to track the handle's request string.
Available since PHP 5.1.3. The CURLINFO_ prefix is intentional.
http://www.php.net/manual/en/function.curl-setopt.php
The following works:
<?php
$ch = curl_init('http://www.google.com');
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
Upvotes: 1