tags:

views:

68

answers:

3

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)
  • Printing all superglobals ($_POST, $_GET, $_REQUEST, $_FILES etc) still doesn't show the raw HTTP headers.
  • http_get_request_headers(), apache_request_headers(), $http_response_header, $HTTP_RAW_POST_DATA aren't outputting everything.

Help?

A: 

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);
Tom Haigh
It's still not showing all the headers, specifically the proxy headers. Adding`curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);``curl_setopt($ch, CURLOPT_PROXY, 'http://' . '60.194.14.98:8080');``curl_setopt($ch, CURLOPT_PROXYTYPE ,CURLPROXY_HTTP);``$result = curl_exec($ch);``echo curl_getinfo($ch, CURLINFO_HEADER_OUT);`Returns the same output.
php
@php: ah, so you actually want to know what the proxy is sending, not what curl is sending. you probably need to log that on the proxy itself or the remote server. curl doesn't know what headers your proxy is adding
Tom Haigh
A: 

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 webserver config.

C.

symcbean
A: 

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 **/
}
wimg