When you need to execute multiple cURL requests concurrently, you can use the curl_multi_exec() function. This function allows multiple cURL sessions to be run simultaneously and manage them with a multiple handle.
Here is a basic example showing how to use curl_multi_exec() to handle multiple cURL requests:
<?php
// Create multiple cURL Handle
$ch1 = curl_init("https://gitbox.net/api/data1");
$ch2 = curl_init("https://gitbox.net/api/data2");
$ch3 = curl_init("https://gitbox.net/api/data3");
// Create a cURL 多Handle
$multi_handle = curl_multi_init();
// Put a single cURL Handle加入到多Handle中
curl_multi_add_handle($multi_handle, $ch1);
curl_multi_add_handle($multi_handle, $ch2);
curl_multi_add_handle($multi_handle, $ch3);
// Execute multiple cURL ask
$running = null;
do {
curl_multi_exec($multi_handle, $running);
} while ($running > 0);
// Close each individual cURL Handle
curl_multi_remove_handle($multi_handle, $ch1);
curl_multi_remove_handle($multi_handle, $ch2);
curl_multi_remove_handle($multi_handle, $ch3);
// closure多Handle
curl_multi_close($multi_handle);
In this example, we create three cURL request handles and execute them simultaneously via curl_multi_exec() . Note that although curl_multi_exec() can perform multiple requests at the same time, we still need to use curl_multi_remove_handle() and curl_multi_close() to ensure that resources are properly released.
After each cURL request is executed, the curl_close() function should be used to release the corresponding resource. Although curl_multi_close() closes the entire multi-handle connection, it does not automatically close a separate cURL handle. Therefore, after completing the multitasking operation, we must manually close each cURL handle. If these resources are not released correctly, memory leaks may occur.
Here is the improved code that correctly uses curl_close() to free resources for each cURL handle:
<?php
// Create multiple cURL Handle
$ch1 = curl_init("https://gitbox.net/api/data1");
$ch2 = curl_init("https://gitbox.net/api/data2");
$ch3 = curl_init("https://gitbox.net/api/data3");
// Create a cURL 多Handle
$multi_handle = curl_multi_init();
// Put a single cURL Handle加入到多Handle中
curl_multi_add_handle($multi_handle, $ch1);
curl_multi_add_handle($multi_handle, $ch2);
curl_multi_add_handle($multi_handle, $ch3);
// Execute multiple cURL ask
$running = null;
do {
curl_multi_exec($multi_handle, $running);
} while ($running > 0);
// Close each individual cURL Handle
curl_close($ch1);
curl_close($ch2);
curl_close($ch3);
// closure多Handle
curl_multi_close($multi_handle);
In this way, we ensure that after each cURL request is completed, the relevant resources can be released in time.
Release resources in time : After all cURL requests are completed, be sure to call curl_close() to release each individual cURL resource and close multiple handles at the end.
Proper error handling : In actual projects, network requests may fail, resulting in some cURL handles not being closed properly. You can add an error handling mechanism to ensure that the resource can be released regardless of whether the request is successful or not. For example:
if ($ch1) {
// 如果ask成功,closure cURL resource
curl_close($ch1);
}
Monitor memory usage : When handling multiple concurrent cURL requests, you can monitor PHP's memory usage to ensure that no memory leaks occur. In some complex applications, using gc_collect_cycles() in time to force the recycling of PHP memory garbage can also help reduce memory usage.
Limit the number of concurrent requests : Although curl_multi_exec() can execute multiple requests at the same time, excessive concurrent requests may lead to excessive consumption of system resources. In practical applications, the number of concurrent requests can be appropriately limited to avoid excessive cURL requests from occupying too much memory.