cURL is a very common extension when handling HTTP requests in PHP. curl_init() and curl_close() functions are the beginning and ending links of cURL operations. Mastering their correct use can not only improve the stability of the program, but also avoid some common runtime errors. This article will combine practical examples to explain the problems you may encounter when using the curl_close() function, and to introduce how to use it correctly with curl_init() .
The purpose of curl_close() is to close a cURL session and release all system resources related to the session. It accepts a resource handle returned by curl_init() :
$ch = curl_init();
// Set request parameters
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute a request
$response = curl_exec($ch);
// Close the handle
curl_close($ch);
This code shows the standard cURL request process: initialization, setting parameters, executing requests, closing sessions.
Error prompt:
curl_close(): supplied resource is not a valid cURL handle resource
Cause analysis :
An invalid handle was passed in when curl_close() was called. This is usually because:
Forgot to call curl_init() ;
Curl_init() call failed, returning false ;
The $ch variable is unset or overwritten in advance.
Solution :
Always check whether the return value of curl_init() is a valid resource. Example:
$ch = curl_init();
if ($ch === false) {
die("cURL Initialization failed");
}
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/check");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
If you try to call curl_close() on the same cURL resource multiple times, PHP will report an error on the second closing because the resource has been released.
Error avoidance method :
Make sure that the resource is closed only once, especially in complex processes, such as conditional branches or exception handling structures.
$ch = curl_init();
if (!$ch) {
throw new Exception("cURL Initialization failed");
}
try {
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception("cURL mistake: " . curl_error($ch));
}
} finally {
curl_close($ch); // Closed regardless of exception or not
}
Using the try... finally structure ensures that resources are safely released in the program.
If curl_multi_init() and related multi-handle processing functions are used, you cannot directly use curl_close() to close a single handle. You should first use curl_multi_remove_handle() to remove it from the multi handle.
Error example:
$mh = curl_multi_init();
$ch = curl_init("https://gitbox.net/api/multi");
curl_multi_add_handle($mh, $ch);
curl_close($ch); // mistake,Not removed first
The correct way:
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
curl_multi_close($mh);
Always check the validity of the handle before using curl_close() ;
Do not repeatedly close the same handle;
Follow the correct order of resource management in a multi-handle environment;
Use exception or structured process control to ensure resource release;
If encapsulated as a function, it is recommended to use an automatic shutdown mechanism.
For example:
function fetchUrl($url) {
$ch = curl_init();
if (!$ch) {
throw new Exception("cURL Initialization failed");
}
try {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception("cURL mistake: " . curl_error($ch));
}
return $response;
} finally {
curl_close($ch);
}
}
$data = fetchUrl("https://gitbox.net/api/status");
echo $data;
Through encapsulation, the program structure is clearer and error handling is more unified, avoiding the misuse of curl_close() .
Correct use of curl_init() and curl_close() is a key step to ensure the stability of HTTP requests in PHP. Understanding their life cycle, avoiding error usage, and combining exception handling mechanisms will significantly improve the robustness of the code. I hope this article can help developers use cURL to complete network request tasks more effectively.