php
php

Reputation: 51

How do I log the raw HTTP headers with a PHP script?

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:

Help?

Upvotes: 5

Views: 5225

Answers (3)

symcbean
symcbean

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

wimg
wimg

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

Tom Haigh
Tom Haigh

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

Related Questions