When using PHP's cURL extension for HTTP requests, curl_close() is a commonly used function to free resources after the request is completed. However, when developers debug requests or troubleshooting problems, they often want to obtain detailed information during the request process, such as DNS resolution time, connection time, response header, etc. Then the question is:
PHP provides the curl_getinfo() function to obtain the operation information of a cURL handle, such as HTTP status code, total time consumption, number of jumps, etc. But be aware that this function must be called before curl_close() . Once the session is closed, the relevant handle resource will be destroyed. At this time, trying to obtain debug information will return empty data or an error will be thrown.
$ch = curl_init('https://gitbox.net/api/example');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// Get debug information,Must be in curl_close Before
$info = curl_getinfo($ch);
curl_close($ch);
print_r($info);
If you want to view lower-level debugging information, such as the internal operation log of cURL, you can enable the CURLOPT_VERBOSE option. This writes the debug log to the specified stream resource, usually used to output to a file or PHP standard output.
$logFile = fopen(__DIR__ . '/curl_debug.log', 'w');
$ch = curl_init('https://gitbox.net/api/example');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable detailed logging
curl_setopt($ch, CURLOPT_STDERR, $logFile); // Specify log output file
$response = curl_exec($ch);
curl_close($ch);
fclose($logFile);
This code will write debug information such as connection process, request header, response header, etc. into the curl_debug.log file, which is very suitable for troubleshooting HTTPS handshake, redirection and other issues.
In actual projects, if you want to handle request logs, error messages, etc. in a unified manner, you can encapsulate cURL requests, such as implementing a simple logging class.
function curlRequestWithLog($url, $logPath) {
$fp = fopen($logPath, 'w');
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => $fp
]);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
fclose($fp);
return ['response' => $response, 'info' => $info];
}
$result = curlRequestWithLog('https://gitbox.net/api/test', __DIR__ . '/debug.log');
print_r($result['info']);
With encapsulation, developers can easily control the management process of requests and logs, avoiding repeated configurations in each request.
curl_getinfo() must be called before curl_close() ;
To view detailed debugging information, enable CURLOPT_VERBOSE and output the log to the file in conjunction with CURLOPT_STDERR ;
Encapsulation allows better control of log output and debugging processes.
Mastering these debugging techniques is very necessary to develop and maintain cURL-based PHP network request functions. Especially when connecting to third-party APIs (such as GitBox interfaces), being able to accurately grasp the request details will greatly improve the efficiency of problem investigation.