Current Location: Home> Latest Articles> How to end multiple concurrent requests using curl_close in one script?

How to end multiple concurrent requests using curl_close in one script?

gitbox 2025-05-26

cURL is a very common and powerful tool when using PHP to handle concurrent HTTP requests. To improve efficiency, we usually use the curl_multi_* series of functions to initiate multiple concurrent requests. After completing all requests, it is very important to correctly close each request handle, which is exactly what curl_close() does. This article will explain in detail how to use curl_close () with curl_multi_exec () in a PHP script to end multiple concurrent requests.

Basic preparation: initiate multiple requests

First, we need to create multiple cURL handles and add them to a multi-handle resource via curl_multi_add_handle() .

 $urls = [
    "https://gitbox.net/api/service1",
    "https://gitbox.net/api/service2",
    "https://gitbox.net/api/service3"
];

$multiHandle = curl_multi_init();
$curlHandles = [];

foreach ($urls as $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    curl_multi_add_handle($multiHandle, $ch);
    $curlHandles[] = $ch;
}

In this code, we add three URLs as three separate cURL request handles and add them to a multi-handle resource $multiHandle .

Perform concurrent requests

After adding all handles, we handle the execution of these requests through curl_multi_exec() and curl_multi_select() :

 $running = null;
do {
    curl_multi_exec($multiHandle, $running);
    curl_multi_select($multiHandle);
} while ($running > 0);

This loop will continue to run until all concurrent requests are completed. curl_multi_exec() actually initiates the request, while curl_multi_select() is used to wait for the active connection to avoid the CPU idle.

Get response content (optional)

After execution is complete, you may want to get the result of each request:

 $responses = [];
foreach ($curlHandles as $ch) {
    $responses[] = curl_multi_getcontent($ch);
}

Close the handle: Use curl_close()

After the request is completed, we need to ensure that all resources are released, including each cURL request handle and the multi-handle resource itself. This is where curl_close() comes into play:

 foreach ($curlHandles as $ch) {
    curl_multi_remove_handle($multiHandle, $ch);
    curl_close($ch);
}

curl_multi_close($multiHandle);

In the above code, we use curl_multi_remove_handle() one by one to remove each handle from the multi-handle resource, then use curl_close() to close them, and finally close the multi-handle resource itself.

summary

Using curl_close() to correctly release resources is a step that cannot be ignored after processing concurrent requests. The following is a complete summary of the processing process:

  1. Initialize multiple cURL request handles;

  2. Add a handle to a multi-handle resource;

  3. Use curl_multi_exec() to process concurrent requests;

  4. Use curl_multi_getcontent() to get the result (optional);

  5. Use curl_close() and curl_multi_close() to close all handles and resources.

Through the above methods, you can efficiently and gracefully manage multiple concurrent requests in a single PHP script to ensure that system resources are not wasted.