41

When using the PHP curl functions, is there anyway to see the exact raw headers that curl is sending to the server?

4 Answers 4

47

You can use curl_getinfo:

Before the call

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

After

$headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, this doesn't show the body of the cURL request. Does anyone know how to show that? Obviously we pass the body to cURL, but for debugging I would like to see EXACTLY what cURL is sending.
@ChadwickMeyer I'm looking for the same thing: how to show both the headers and the request body. Doesn't seem to be possible. :-(
9
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_exec($ch);
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
?>

Only available in php 5.1.3 http://php.net/manual/en/function.curl-getinfo.php


You can verify that they are the same by using your console and hitting

curl http://example.com/ -I

or

curl --trace-ascii /file.txt http://example.com/

Comments

5

AFAIK, the PHP/CURL binding still lacks proper support for CURLOPT_DEBUGFUNCTION which is a callback from libcurl that can provide all those details.

That's the primary reason why I recommend people to work out HTTP scripting things with the curl command line tool and its --trace-ascii option FIRST, then translate that into a PHP function.

1 Comment

It looks like you're right, I poked around the latest stable PHP source a bit, and it looks like they use CURLOPT_DEBUGFUNCTION to implement their CURLINFO_HEADER_OUT options, but they don't expose a fully featured CURLOPT_DEBUGFUNCTION of their own. Side note: I remember your emails from the php curl mailing list. I'm amazed you still have the patience to do any kind of PHP related libcurl support :)
2

be sure to set the CURLINFO_HEADER_OUT option before making the curl_getinfo call

curl_setopt($c, CURLINFO_HEADER_OUT, true);

2 Comments

@Janek It should be read as follows: curl_setopt($c, CURLOPT_HEADER, true);
Janek is correct, CURLINFO_HEADER_OUT is the option to set if you want to view the header using curl_getinfo() it is documented here php.net/manual/en/function.curl-setopt.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.