How to get info on sent PHP curl request How to get info on sent PHP curl request curl curl

How to get info on sent PHP curl request


If you set CURLINFO_HEADER_OUT to true, outgoing headers are available in the array returned by curl_getinfo(), under request_header key:

$ch = curl_init();curl_setopt($ch, CURLOPT_URL, "http://foo.com/bar");curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);curl_setopt($ch, CURLOPT_USERPWD, "someusername:secretpassword");curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLINFO_HEADER_OUT, true);curl_exec($ch);$info = curl_getinfo($ch);print_r($info['request_header']);

This will print:

GET /bar HTTP/1.1Authorization: Basic c29tZXVzZXJuYW1lOnNlY3JldHBhc3N3b3JkHost: foo.comAccept: */*

Note the auth details are base64-encoded:

echo base64_decode('c29tZXVzZXJuYW1lOnNlY3JldHBhc3N3b3Jk');// prints: someusername:secretpassword

Also note that username and password need to be percent-encoded to escape any URL reserved characters (/, ?, &, : and so on) they might contain:

curl_setopt($ch, CURLOPT_USERPWD, urlencode($username).':'.urlencode($password));


You can also use a proxy tool like Charles to capture the outgoing request headers, data, etc. by passing the proxy details through CURLOPT_PROXY to your curl_setopt_array method.

For example:

$proxy = '127.0.0.1:8888';$opt = array (    CURLOPT_URL => "http://www.example.com",    CURLOPT_PROXY => $proxy,    CURLOPT_POST => true,    CURLOPT_VERBOSE => true,    );$ch = curl_init();curl_setopt_array($ch, $opt);curl_exec($ch);curl_close($ch);


curl_getinfo() must be added before closing the curl handler

$ch = curl_init();curl_setopt($ch, CURLOPT_URL, "http://example.com/bar");curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);curl_setopt($ch, CURLOPT_USERPWD, "someusername:secretpassword");curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLINFO_HEADER_OUT, true);curl_exec($ch);$info = curl_getinfo($ch);print_r($info['request_header']);curl_close($ch);