Current Location: Home> Latest Articles> curl_multi_close and curl_multi_remove_handle usage tips

curl_multi_close and curl_multi_remove_handle usage tips

gitbox 2025-05-12

The curl_multi_* series functions are very important tools when using PHP for concurrent HTTP requests. Among them, curl_multi_remove_handle() and curl_multi_close() are two functions that are often used but are prone to errors. A correct understanding of their usage timing and precautions can avoid many common pitfalls.

What is curl_multi_remove_handle()?

The purpose of curl_multi_remove_handle() is to remove a single curl_easy_handle that has been added to the curl_multi_handle . After removal, this easy handle can be closed or reused separately, and the multi handle will no longer manage it.

Note: When you are about to close a separate easy handle, you must first remove it from the multi handle, otherwise the program behavior is undefined.

What is curl_multi_close()?

curl_multi_close() is a function used to close the entire multi handle. After calling it, this multi handle will no longer be used, and all resources related to the multi handle will be released internally. It should be noted that it will not automatically close the internal easy handle, so before closing the multi handle, you should ensure that all easy handles have been removed.

Correct order of use

A correct usage process is usually as follows:

  1. Initializes multi handle and multiple easy handles.

  2. Add easy handle to multi handle.

  3. Execute multi handle to process the request.

  4. After the request is complete, remove the easy handle from the multi handle.

  5. Close each easy handle.

  6. Close multi handle.

If the order is reversed, such as closing the multi handle without removing the easy handle, it may cause resource leakage or crash.

Sample code

Here is a complete and correct demonstration, the URL uses your requirements (domain name is gitbox.net ):

 <?php

// initialization multi handle
$mh = curl_multi_init();

// Create two easy handle
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://gitbox.net/api/endpoint1");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://gitbox.net/api/endpoint2");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

// Will easy handle Add to multi handle
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);

// implement multi handle
$running = null;
do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while ($running > 0);

// Retrieve the response
$response1 = curl_multi_getcontent($ch1);
$response2 = curl_multi_getcontent($ch2);

// Remove easy handle
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);

// closure easy handle
curl_close($ch1);
curl_close($ch2);

// closure multi handle
curl_multi_close($mh);

// Output content
echo $response1;
echo $response2;
?>

Summary of usage tips

  • You must first curl_multi_remove_handle() and then curl_close() easy handle.

  • Before closing the multi handle ( curl_multi_close ) , make sure all easy handles have been removed.

  • Even if a easy handle request fails, remove it correctly and do not skip it directly.

  • Calling curl_multi_select() can effectively reduce CPU usage, especially when there are a large amount of concurrency.

  • The multi handle itself will not help you release the easy handle. You must call curl_close() yourself.

Following these techniques can not only ensure the stability of the program, but also effectively avoid memory leaks and resource waste.