When using PHP's cURL extension for network requests, developers often call curl_close() to close a cURL session. But many people encounter this problem when debugging:. Because once curl_close() is called, resources related to the session will be released, including error messages.
This article will explain how to get and debug possible errors in cURL before closing a session, and provide some practical suggestions and sample code.
curl_close() releases the cURL handle created by curl_init() . Once released, you will no longer be able to call curl_error() or curl_errno() to view the error message. This requires us to obtain error information in time for debugging or recording before closing the handle.
The following is a standard request process, pay attention to the timing of obtaining error information:
<?php
$url = "https://gitbox.net/api/data";
// initialization cURL Session
$ch = curl_init();
// set up cURL Options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute a request
$response = curl_exec($ch);
// 在closureSession前获取mistake信息
if (curl_errno($ch)) {
$errorCode = curl_errno($ch);
$errorMessage = curl_error($ch);
error_log("cURL mistake [$errorCode]: $errorMessage");
} else {
// Processing response
echo "Response content: " . $response;
}
// closure cURL Session
curl_close($ch);
In addition to curl_error() and curl_errno() , you can also use the following methods to further debug:
You can set CURLOPT_VERBOSE to true , so that PHP will output the detailed information of the request process to the standard error output, which is suitable for debugging in CLI mode:
curl_setopt($ch, CURLOPT_VERBOSE, true);
If you want to write verbose information to the log file:
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $verbose);
After the request is completed, the log content can be read:
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
error_log("cURL Debugging information: \n" . $verboseLog);
Sometimes the request itself does not have a network error, but the server returns the wrong HTTP status code. You can check it by:
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
error_log("HTTP Status code: $httpCode");
}
To facilitate multiplexing and error capture, cURL requests can be encapsulated into a function:
function fetchData($url) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("cURL Request failed: " . $error);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
throw new Exception("HTTP Status codemistake: " . $httpCode);
}
return $response;
}
try {
$data = fetchData("https://gitbox.net/api/data");
echo "Get successful: $data";
} catch (Exception $e) {
error_log("Exception capture: " . $e->getMessage());
}
When using PHP's cURL for network requests, the key to debugging is to obtain error information in time before closing the session . By rationally using tools such as curl_errno() , curl_error() , curl_getinfo() and CURLOPT_VERBOSE , developers can effectively locate problems and improve program stability. Remember not to try to get any errors or debug information after curl_close() , because it was too late by then.