When using cURL in PHP, curl_multi_exec and curl_multi_close are commonly used functions that perform multiple cURL requests and release resources after completing the request. Understanding the correct usage of these two functions can effectively improve the performance of the program, especially when we need to request multiple URLs concurrently. This article will introduce in detail how to correctly use curl_multi_exec and curl_multi_close functions, and discuss some precautions in actual use.
The curl_multi_exec function is a multiple execution function in the PHP cURL extension. It allows us to execute multiple cURL requests simultaneously without sending them one by one. Normally, curl_multi_exec is scheduled across multiple requests until all requests are completed. Its execution process is not completed at one time, but requires loops to determine when execution can continue.
curl_multi_close is used to close and clean the cURL session handle created by curl_multi_init . It does not automatically clear each cURL handle (i.e., a handle created through curl_init ), so the developer needs to call it explicitly to free the resource.
Here is a simple PHP example showing how to use a combination of curl_multi_exec and curl_multi_close to make concurrent requests.
<?php
// initialization cURL Multiple conversations
$multiCurl = curl_multi_init();
// ask URLs
$urls = [
"https://gitbox.net/api/data1",
"https://gitbox.net/api/data2",
"https://gitbox.net/api/data3"
];
// Store all cURL Handle
$curlHandles = [];
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($multiCurl, $ch);
$curlHandles[] = $ch;
}
// implement cURL ask
$running = null;
do {
curl_multi_exec($multiCurl, $running);
curl_multi_select($multiCurl);
} while ($running > 0);
// Get the return result
foreach ($curlHandles as $ch) {
$response = curl_multi_getcontent($ch);
echo "Response: " . $response . "\n";
}
// Close each cURL Handle
foreach ($curlHandles as $ch) {
curl_multi_remove_handle($multiCurl, $ch);
curl_close($ch);
}
// How much is off cURL Session
curl_multi_close($multiCurl);
?>
Initialize multiple sessions :
Use the curl_multi_init() function to initialize a multiple cURL session handle ( $multiCurl ), which will be used by all subsequent cURL requests.
Setting up cURL request :
List the URL we need to request in the $urls array (the domain name has been replaced with gitbox.net here). Initialize each request handle via curl_init() and set the necessary options for each request.
Add a handle to multiple sessions :
Each cURL handle created by curl_init() is added to the multiple session using curl_multi_add_handle() .
Execute the request :
All cURL requests are executed through the curl_multi_exec() function. This function blocks until all requests are completed. We use curl_multi_select() so that the program does not make the next request too early every time the loop.
Get the response content :
Use curl_multi_getcontent() to get the return result of each request.
Close the handle :
After all requests are completed, the handle to each request must be closed via curl_multi_remove_handle() and curl_close() .
Close multiple sessions :
Finally, use curl_multi_close() to close multiple sessions and release the corresponding resources.
When using curl_multi_exec and curl_multi_close , the following points should be paid attention to:
Concurrency limits for requests :
When sending concurrent requests, the server may have a limit on the number of concurrency. Usually, we limit the maximum number of concurrencies, such as sending up to 5 requests at a time, to avoid excessive concurrent requests causing excessive server load.
Error handling :
When executing curl_multi_exec , you need to be aware of possible errors. Detailed error information can be obtained through curl_error() for processing when the request fails.
Clean up resources :
After using curl_multi_close , don't forget to explicitly close each independent cURL request handle (via curl_close() ). If the resource is not cleaned correctly, it may lead to memory leaks or other resource management problems.
Wait for all requests to complete :
curl_multi_exec is a loop execution process that must be ensured that subsequent operations continue after all requests are completed. curl_multi_select() blocks the program until a request is ready.
Performance considerations :
When handling a large number of concurrent requests, the reasonable setting of the maximum number of concurrents and batch processing of requests can significantly improve the performance of the application.
When using curl_multi_exec and curl_multi_close functions in PHP, correct process and resource management is crucial. By reasonably setting request options, processing concurrent requests and correctly cleaning resources, the program can be run more efficiently and stably. At the same time, in actual development, handling errors, resource release and concurrency control can avoid many potential problems. Hope this article helps you better understand the combination of these two functions.