In PHP, the curl_multi_* function family is used to execute multiple cURL requests simultaneously. curl_multi_close() is an important function in this series to close multiple cURL handles initialized by curl_multi_init() .
The curl_multi_* function family is a tool in PHP for processing multiple cURL requests in parallel. They allow you to send multiple HTTP requests simultaneously without waiting for each request to complete before sending the next one. In this way, multiple requests can be processed significantly, especially when multiple APIs or websites need to be requested.
Common curl_multi_* functions are:
curl_multi_init() : Initialize multiple cURL handles.
curl_multi_add_handle() : Adds a single cURL handle to the multi handle.
curl_multi_exec() : executes multiple cURL requests.
curl_multi_getcontent() : Gets the contents of a cURL handle.
curl_multi_close() : Closes the multi handle created by curl_multi_init() and frees all resources.
When multiple cURL requests are executed using the curl_multi_* function, each request is independent and each request has its own resource consumption. In order to ensure that these resources can be released in time after the request is completed, curl_multi_close() must be called to close the connection.
If you don't use curl_multi_close() , these cURL handles may continue to take up memory and other resources, causing performance issues and even memory leaks. Therefore, it is very important to properly close these connections after the request is complete.
Here is an example showing how to use curl_multi_close() to close multiple cURL connections.
<?php
// Initialize multiple cURL Handle
$mh = curl_multi_init();
// Define what to request URL List
$urls = [
"https://gitbox.net/api/endpoint1",
"https://gitbox.net/api/endpoint2",
"https://gitbox.net/api/endpoint3"
];
// storage cURL Handle
$ch_handles = [];
// Create each cURL Handle并添加到 multi Handle中
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch);
$ch_handles[] = $ch;
}
// Perform multiple requests
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
// Get the content of each request and close it cURL Handle
foreach ($ch_handles as $ch) {
$content = curl_multi_getcontent($ch);
echo $content . "\n"; // Output the returned content
curl_multi_remove_handle($mh, $ch);
curl_close($ch); // Close each cURL Handle
}
// closure multi Handle
curl_multi_close($mh);
?>
Initialize multiple cURL handles :
Use curl_multi_init() to create a new multi handle that manages multiple cURL requests.
Add multiple cURL handles :
Create a cURL handle for each URL, set relevant options, and then add these handles to the multi handle via curl_multi_add_handle() .
Execute the request :
Use curl_multi_exec() to execute all requests until all requests are completed.
Get the content and close the handle :
Use curl_multi_getcontent() to get the content returned by each cURL handle. Once done, remove the handle from the multi handle via curl_multi_remove_handle() and close each individual cURL handle using curl_close() .
Close the multi handle :
Finally, call curl_multi_close() to close the multi handle and free all resources.
Correct use of curl_multi_close() is a key step to ensure that multiple cURL request resources are released in a timely manner. When processing multiple requests in parallel, remember to call curl_multi_close() after each request is executed to close all relevant cURL connections. This not only prevents memory leaks, but also improves the efficiency of the code.