Current Location: Home> Latest Articles> How to ensure the smooth execution of curl_multi_close when multiple requests are concurrent

How to ensure the smooth execution of curl_multi_close when multiple requests are concurrent

gitbox 2025-05-12

When making concurrent requests, the cURL extension in PHP can help us send multiple HTTP requests at the same time, improving program efficiency. However, when handling multiple requests, how to ensure that resources are properly released and avoid memory leaks or handle leaks has become a focus of developers. In this article, we will focus on how to correctly use the curl_multi_close function to ensure smooth resource release.

1. What is the curl_multi_close function?

The curl_multi_close function is used to close a cURL multi-handle and release all resources related to it. When using curl_multi_init to create a multiple handle and send multiple requests, all requests are managed through that handle. In order to avoid memory leaks, after completing all requests, curl_multi_close needs to be called to free up the relevant resources.

2. The correct process to use curl_multi_close

In the process of concurrent requests, the following steps are usually involved:

  1. Initialize multiple cURL sessions and add them to multiple handles.

  2. Perform all requests and wait for them to complete.

  3. Ensure that the response data for each request is processed correctly.

  4. Use the curl_multi_close function to close the multi-handle and free the resource.

Next, a basic code example demonstrates how to use curl_multi_close correctly:

 <?php
// 1. initialization cURL Multiple handles
$mh = curl_multi_init(); // 创建一个Multiple handles

// 2. Create a single cURL Session并添加到Multiple handles中
$ch1 = curl_init('http://gitbox.net/api/request1');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch1);

$ch2 = curl_init('http://gitbox.net/api/request2');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch2);

// 3. Perform multiple requests
do {
    $status = curl_multi_exec($mh, $active);
    if ($active) {
        curl_multi_select($mh); // Wait for the request to complete
    }
} while ($active && $status == CURLM_OK);

// 4. Get response data
$response1 = curl_multi_getcontent($ch1);
$response2 = curl_multi_getcontent($ch2);

// 5. closure cURL Session
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);

// 6. Free up resources
curl_close($ch1);
curl_close($ch2);

// 7. closureMultiple handles
curl_multi_close($mh);

echo "ask1Response: " . $response1 . "\n";
echo "ask2Response: " . $response2 . "\n";
?>

3. Things to note

  • Resource release order : Make sure to remove each individual cURL session from the multiple handles before closing it. Otherwise, the handle may be incorrectly released, causing memory leaks.

  • Error handling : During the execution of curl_multi_exec , you should pay attention to checking the execution status of each request. If a request fails, the error should be handled in time and the corresponding resources should be released.

  • Performance optimization : If there are a large number of requests, minimize unnecessary blocking. You can reduce CPU usage by curl_multi_select .

4. Summary

The correct use of the curl_multi_close function is crucial to freeing resources in concurrent requests. By following the above process, ensuring that each cURL handle and multiple handle are correctly closed and released, not only avoiding memory leaks, but also improving program stability and performance. Especially in high concurrency scenarios, rational management of resources will greatly improve the robustness of the code.