When making a large number of concurrent HTTP requests, the curl_multi_* series of functions provided by PHP is a very practical tool that allows you to initiate multiple requests at the same time in a script, thereby improving execution efficiency. Among them, curl_multi_close() is the last step in the entire process. It is responsible for cleaning up the resources initialized by curl_multi_init() , ensuring that all CURL handles are properly closed, leaving no hidden dangers of resource leakage. This article will use a complete example to explain in detail how to gracefully close all CURL sessions after all concurrent requests are completed.
When using curl_multi_* for concurrent requests, the general process is as follows:
Initialize a curl_multi container.
Create multiple curl requests and add them to the curl_multi container.
Use curl_multi_exec to drive the execution of all requests.
The loop checks whether the request is completed.
Process the results of each curl request separately.
Removes each individual curl handle.
Use curl_multi_close() to release the resource.
Here is an example that demonstrates how to request multiple URLs concurrently and close resources gracefully after all requests are finished:
<?php
// To send concurrent requests URL List
$urls = [
"https://gitbox.net/api/data1",
"https://gitbox.net/api/data2",
"https://gitbox.net/api/data3"
];
// initialization curl_multi Handle
$multiHandle = curl_multi_init();
$curlHandles = [];
// For each URL Create a separate curl Handle,And join multi Handle
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10
]);
curl_multi_add_handle($multiHandle, $ch);
$curlHandles[] = $ch;
}
// Perform all requests
$active = null;
do {
$mrc = curl_multi_exec($multiHandle, $active);
// curl_multi_select Block until there is an active connection
if ($active) {
curl_multi_select($multiHandle);
}
} while ($active && $mrc == CURLM_OK);
// Get the result of each request
foreach ($curlHandles as $ch) {
$response = curl_multi_getcontent($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);
echo "URL: " . $info['url'] . PHP_EOL;
echo "HTTP Code: " . $info['http_code'] . PHP_EOL;
echo "Response: " . $response . PHP_EOL;
echo "Error: " . $error . PHP_EOL;
echo str_repeat("-", 40) . PHP_EOL;
// 移除并closureHandle
curl_multi_remove_handle($multiHandle, $ch);
curl_close($ch);
}
// at last,closure multi Handle
curl_multi_close($multiHandle);
?>
Call only after all handles are removed : Before calling curl_multi_close() , you should first remove all the clone handles from the multi container through curl_multi_remove_handle() , and then call curl_close() one by one. Otherwise, resources may not be released or behavioral uncertain.
The clone handle will not be closed automatically : curl_multi_close() will only close the multi container itself, and will not automatically close the internal curl handle. You need to manually close each handle created by curl_init() .
Call time : curl_multi_close() should be the last step you execute in the entire process. Its call marks the official end of the entire concurrent request lifecycle.
By using curl_multi_init() and curl_multi_close() properly, you can efficiently make concurrent network requests in PHP, while ensuring that resources are properly released after use, avoiding memory leaks or connection exceptions. Be sure to remember: each curl_init() corresponds to a curl_close() , and each curl_multi_init() corresponds to a curl_multi_close() , so that your code can be more robust and reliable.