Detailed curl Debugging in PHP Using the Output Buffer

//start the output buffer
ob_start();
$out = fopen('php://output', 'w');

$ch = curl_init();

//setting the curl options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//increase verbosity of curl output
curl_setopt($ch, CURLOPT_VERBOSE, 1);

//store output in the stream we've initalized
curl_setopt($ch, CURLOPT_STDERR, $out);

//CURLINFO_HEADER_OUT silently causes CURLOPT_VERBOSE to fail
//curl_setopt($ch, CURLINFO_HEADER_OUT, 1); // capture the header info

$output = [];
$output['result'] = curl_exec($ch);
$output['info'] = curl_getinfo($ch);

curl_close($ch);

fclose($out);
$output['debug'] = ob_get_clean();

curl Verbosity Settings Warning

CURLINFO_HEADER_OUT and CURLOPT_VERBOSE options conflict; when CURLINFO_HEADER_OUT is set, CURLOPT_VERBOSE does not work, apparently because CURLINFO_HEADER_OUR uses CURLOPT_VERBOSE internally to obtain the header, and discards the rest of the verbose output according to https://bugs.php.net/bug.php?id=65348.

Since the output of CURLOPT_VERSBOSE contains the header anyway, it's easier to just use that setting alone and you'll get additional request output as well.

Tags

 curl  PHP  PHP output buffer  snippets