In PHP, using cURL for HTTP requests is very common, especially when requests are required to be sent to multiple external services, cURL provides a very powerful way to implement this functionality. However, by default, cURL only supports synchronous requests, that is, every time a request is sent, PHP will wait for the request to complete before continuing to process the next request.
However, when we need to send requests to multiple services at the same time, this synchronized approach can lead to significant performance bottlenecks, especially when the number of requests is large. In order to improve the concurrent execution efficiency of requests, we can use the curl_multi_* function group, one of the most critical functions is curl_multi_close .
curl_multi_close is part of the cURL multi-request library in PHP, which closes the cURL handle and frees resources. It is usually used after multiple concurrent requests are performed using curl_multi_exec . In this way, the life cycle of multiple concurrent requests can be more efficiently managed, thereby improving performance, especially when processing multiple network requests.
Assuming we have multiple URLs that need to request, we can execute these requests concurrently through the curl_multi_* function. The following is a PHP sample code that uses curl_multi_close to improve the efficiency of request concurrency:
<?php
// Initialize multiple requests cURL Handle
$urls = [
"http://gitbox.net/api/endpoint1",
"http://gitbox.net/api/endpoint2",
"http://gitbox.net/api/endpoint3",
"http://gitbox.net/api/endpoint4"
];
$multiHandle = curl_multi_init();
$curlHandles = [];
// For each URL Create a separate cURL Handle
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_multi_add_handle($multiHandle, $ch);
$curlHandles[] = $ch;
}
// Perform all requests
$running = null;
do {
curl_multi_exec($multiHandle, $running);
curl_multi_select($multiHandle); // Wait for a request to complete
} while ($running);
// Get the response content
foreach ($curlHandles as $ch) {
$response = curl_multi_getcontent($ch);
echo $response . "\n"; // Output response content
}
// closure cURL Handle
foreach ($curlHandles as $ch) {
curl_multi_remove_handle($multiHandle, $ch);
curl_close($ch);
}
// Free up resources
curl_multi_close($multiHandle);
?>
Initialize cURL handles : We first create a separate cURL handle for each URL to request and add these handles to the multiple cURL session.
Execute concurrent requests : All requests are executed asynchronously through curl_multi_exec . It will be executed continuously until all requests are completed. curl_multi_select will make the program wait for a request to complete, thus not blocking the entire process.
Get response data : After all requests are completed, get the response content of each request through curl_multi_getcontent .
Close and Clean : Finally, remove and close each individual cURL handle, and then use curl_multi_close to release the resources of the multiple cURL session handles.
curl_multi_close does not just close the handle to multiple sessions, it also ensures that all cURL resources are released correctly. In long-running scripts, the rational use of curl_multi_close can effectively prevent memory leakage and improve program stability.
Concurrent execution : Traditional synchronous requests are executed sequentially, while the curl_multi_* function allows multiple requests to be executed in parallel, greatly improving execution efficiency, especially when requests need to be sent to multiple different URLs.
Non-blocking I/O : Through the cooperation of curl_multi_select and curl_multi_exec , PHP can continue to process other requests while waiting for a request to respond, avoiding the problem of blocking and waiting.
Resource release : By using curl_multi_close reasonably, resources can be released in time after the request is completed, avoiding memory leaks and other problems.
curl_multi_close plays a crucial role in the concurrent execution of multiple HTTP requests. By using curl_multi_exec and curl_multi_close s rationally, PHP developers can significantly improve the efficiency of applications when processing multiple requests and ensure that resources are effectively managed. For scenarios where high concurrent requests are required, the use of multiple processing capabilities of cURL is a very effective optimization method.