Difference between revisions of "PHP, Log curl requests"

From Wiki de Caballero
Jump to navigation Jump to search
 
 
Line 1: Line 1:
<source lang="php">
<source lang="php">
/*
/*
Option 1: Log to file
Option 1: Log to file.
Next, adapted from https://stackoverflow.com/a/35363599/1071459
 
Adapted from https://stackoverflow.com/a/35363599/1071459
 
Do this before curl_exec to log to file
Do this before curl_exec to log to file
*/
*/
Line 8: Line 10:
curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
curl_setopt($curl_handle, CURLOPT_STDERR, fopen($file_location, 'a+'));
curl_setopt($curl_handle, CURLOPT_STDERR, fopen($file_location, 'a+'));
</source>


<source lang="php">
/*
/*
Option 2: Get output in in buffer and into a variable.
Option 2: Get output in in buffer and into a variable.
From https://blog.kettle.io/debugging-curl-requests-in-php/
*/
*/
# init (part 1 of 2), from https://blog.kettle.io/debugging-curl-requests-in-php/
# init (part 1 of 2)
ob_start();
ob_start();
$output_buffer = fopen('php://output', 'w');
$output_buffer = fopen('php://output', 'w');
Line 21: Line 27:
$response = curl_exec($curl_handle);
$response = curl_exec($curl_handle);


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

Latest revision as of 22:05, 18 December 2020

/*
Option 1: Log to file.

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.

From https://blog.kettle.io/debugging-curl-requests-in-php/
*/
# init (part 1 of 2)
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)
fclose($output_buffer);
$curl_verbose_debug = ob_get_clean();
# end (part 2 of 2)