Current Location: Home> Latest Articles> How to properly handle multi cURL requests in PHP and end with curl_close?

How to properly handle multi cURL requests in PHP and end with curl_close?

gitbox 2025-05-18

In PHP, cURL is a powerful library for performing network requests such as accessing REST APIs or crawling web pages. When processing a single cURL request, it is standard process to initialize the handle using curl_init() , curl_exec() execute the request, and finally release the resource through curl_close() . However, when handling multiple concurrent requests, especially when using curl_multi_* series functions, it is particularly important to properly close and clean resources. Failure to do so may lead to memory leaks or network connection problems.

1. Review of basic cURL request process

In a single request, the typical code is as follows:

 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://gitbox.net/api/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch); // Release resources correctly

Here, curl_close($ch) is required. It closes the cURL handle created by curl_init() and frees the relevant resources.

2. Common ways to handle multiple requests

For scenarios where multiple requests are required to initiate multiple requests at the same time, PHP provides a curl_multi_* interface. Examples are as follows:

 $urls = [
    'https://gitbox.net/api/one',
    'https://gitbox.net/api/two',
    'https://gitbox.net/api/three',
];

$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;
}

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

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

At this time, if you do not execute the next step curl_multi_remove_handle and curl_close , the resource is not released.

3. Correctly release multiple requested resources

After all requests have been executed and the response is retrieved, each handle must be cleaned:

 foreach ($curlHandles as $ch) {
    curl_multi_remove_handle($multiHandle, $ch); // from multi handle Removed in
    curl_close($ch); // Close a single cURL Handle,Free up resources
}

curl_multi_close($multiHandle); // Last Close multi handle

This is a critical step to ensure that all resources are released. It is not enough to just turn off curl_multi_close() without calling curl_close() one by one.

4. Best practices and suggestions

  1. Always explicitly release : Whether it is a single or multiple requests, always remember to use curl_close() .

  2. Use try-finally or error handling : There may be exceptions during processing. It is recommended to use the try-finally structure to ensure that the handle is finally released.

  3. Monitor memory and connection count : In high concurrency environments, unreleased cURL handles can quickly lead to resource exhaustion.

  4. Encapsulation request logic : It is recommended to encapsulate it into a function or class to handle resource release internally to avoid omissions.

5. Summary

curl_close() is not optional in PHP, especially in multiple request scenarios. Correct use of curl_multi_remove_handle() and curl_close() can ensure system stability and performance. After writing the request logic, don’t forget this step: cleaning resources in time is the best performance optimization.