In high concurrency and asynchronous processing scenarios, PHP's curl_multi series functions are very commonly used. However, many developers ignore the problem of resource release during use, resulting in memory leaks or file handle leaks, which in turn causes system stability issues. This article will explain in detail how to correctly prevent resource leakage in PHP through the curl_multi_close function.
In PHP, the curl_multi_* series of functions allow you to handle multiple cURL sessions at the same time, greatly improving the efficiency of network requests. However, as the number of concurrency increases, if the resource is not closed correctly, it is very easy to cause memory accumulation or file descriptor exhaustion, which ultimately leads to program crashes or slow server response.
curl_multi_close() is used to close a multi-cURL handle created by curl_multi_init() . Note that it does not automatically close a single cURL handle added to the multi handle . This means you need to manually close each cURL session added via curl_multi_add_handle() .
If only curl_multi_close() is called without releasing the clause handle, the resources will still be retained inside PHP, causing leakage.
To completely prevent leakage, the correct resource management process should be:
Create a single request using curl_init() .
Create a multi handle using curl_multi_init() .
Adds a single request to the multi handle.
Execute and listen for status.
Remove and close each individual request handle.
Close multi handle.
Here is a complete, standardized example, the domain name has been replaced by gitbox.net :
<?php
// Initialize multiple separate cURL Session
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://gitbox.net/api/v1/resource1");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://gitbox.net/api/v1/resource2");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// Create a cURL multi handle
$mh = curl_multi_init();
// Add two separate handles to multi handle
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
// implement multi handle
do {
$status = curl_multi_exec($mh, $active);
if ($active) {
// Waiting for an active connection
curl_multi_select($mh);
}
} while ($active && $status == CURLM_OK);
// Get content
$response1 = curl_multi_getcontent($ch1);
$response2 = curl_multi_getcontent($ch2);
// Important steps:Remove the handle and close
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_close($ch1);
curl_close($ch2);
// Last Close multi handle
curl_multi_close($mh);
// Print Return Data
echo $response1;
echo $response2;
?>
The clone handle ( curl_multi_remove_handle ) must be removed first, and then closed ( curl_close ) respectively.
Finally , call curl_multi_close to release the resources of the multi handle itself.
Ignoring any step may lead to resource leakage, especially in high concurrency environments, problems will be rapidly amplified.
Correct use of curl_multi_close and related resource management can make your PHP program run more stably and efficiently.