PHP, Log curl requests

From Wiki de Caballero
Revision as of 22:03, 18 December 2020 by Felipe (talk | contribs) (Created page with "<source lang="php"> →‎Option 1: Log to file Next, adapted from https://stackoverflow.com/a/35363599/1071459 Do this before curl_exec to log to file: $file_location = ''; //...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
/*
Option 1: Log to file
Next, adapted from https://stackoverflow.com/a/35363599/1071459
Do this before curl_exec to log to file
*/
$file_location = ''; // CHANGE THIS
curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
curl_setopt($curl_handle, CURLOPT_STDERR, fopen($file_location, 'a+'));

/*
Option 2: Get output in in buffer and into a variable.
*/
# init (part 1 of 2), from https://blog.kettle.io/debugging-curl-requests-in-php/
ob_start();
$output_buffer = fopen('php://output', 'w');
curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
curl_setopt($curl_handle, CURLOPT_STDERR, $output_buffer);
# end (part 1 of 2)

$response = curl_exec($curl_handle);

# init (part 2 of 2), from https://blog.kettle.io/debugging-curl-requests-in-php/
fclose($output_buffer);
$curl_verbose_debug = ob_get_clean();
# end (part 2 of 2)