Current Location: Home> Latest Articles> How to Properly Remove a cURL Handle from a Multi Handle Using curl_multi_remove_handle?

How to Properly Remove a cURL Handle from a Multi Handle Using curl_multi_remove_handle?

gitbox 2025-08-12

What is curl_multi_remove_handle?

curl_multi_remove_handle is a function used to remove an individual cURL handle from a multi cURL handle. Multi cURL handles allow us to perform multiple requests simultaneously by managing several cURL handles collectively. When a request finishes or is no longer required, we can use curl_multi_remove_handle to remove that request from the multi handle.

Function Prototype:

<span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-title function_ invoke__">curl_multi_remove_handle</span></span><span> ( resource </span><span><span class="hljs-variable">$multi_handle</span></span><span> , resource </span><span><span class="hljs-variable">$ch</span></span><span> )
</span></span>
  • $multi_handle: A cURL multi handle resource, usually initialized by curl_multi_init().

  • $ch: The individual cURL handle resource to be removed, typically initialized by curl_init().

The function returns a boolean value: TRUE on success and FALSE on failure.


Usage Scenarios

Generally, when performing multiple cURL requests, we use curl_multi_init() to initialize a multi cURL handle, and then add multiple individual cURL handles to it using curl_multi_add_handle(). Once a request completes or encounters an error, we need to remove the corresponding individual handle from the multi handle, for which curl_multi_remove_handle is used.

Example Code:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
<p></span>// Initialize multi cURL handle<br>
</span>$multi_handle = curl_multi_init();</p>
<p>// Initialize two individual cURL handles<br>
</span>$ch1 = curl_init();<br>
$ch2 = curl_init();</p>
<p>// Set URL and other options for the first request<br>
</span>curl_setopt($ch1, CURLOPT_URL, </span>'<a rel="noopener" target="_new" class="" href="http://example.com/api/endpoint1&#039;</span></span">http://example.com/api/endpoint1&#039;</span></span</a>>);<br>
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);</p>
<p></span>// Set URL and other options for the second request<br>
</span>curl_setopt($ch2, CURLOPT_URL, </span>'<a rel="noopener" target="_new" class="" href="http://example.com/api/endpoint2&#039;</span></span">http://example.com/api/endpoint2&#039;</span></span</a>>);<br>
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);</p>
<p></span>// Add these two cURL handles to the multi cURL handle<br>
</span>curl_multi_add_handle($multi_handle, </span>$ch1);<br>
</span>curl_multi_add_handle($multi_handle, </span>$ch2);</p>
<p></span>// Execute requests<br>
</span>do {<br>
$status = curl_multi_exec(</span>$multi_handle, </span>$active);<br>
</span>if ($active) {<br>
curl_multi_select($multi_handle);<br>
}<br>
} </span>while ($active && $status == CURLM_OK);</p>
<p>// Remove the first cURL handle<br>
</span>curl_multi_remove_handle($multi_handle, $ch1);</p>
<p>// Close all cURL handles<br>
</span>curl_multi_close($multi_handle);<br>
curl_close($ch1);<br>
curl_close($ch2);<br>
?><br>
</span>

Code Explanation

  1. Initialize multi cURL handle: We first initialize a multi handle using curl_multi_init(). This handle manages multiple requests.

  2. Initialize individual cURL handles: Then, we initialize two cURL handles, ch1 and ch2, each representing a request.

  3. Set request options: For each individual handle, set the request URL and options for returning the response data.

  4. Add handles to multi cURL handle: Use curl_multi_add_handle() to add ch1 and ch2 to the multi handle for concurrent execution.

  5. Execute requests: Use curl_multi_exec() to process and execute all requests in the multi handle, and curl_multi_select() to wait for completion.

  6. Remove cURL handle: Once a request finishes or is no longer needed, use curl_multi_remove_handle() to remove its handle from the multi handle.

  7. Close handles: Finally, close the multi handle and all individual handles to free up resources.


Precautions

  1. Timing of removal: Only remove a cURL handle after the request is complete or confirmed unnecessary. After removal, the handle no longer participates in any request execution.

  2. Resource release: Even after removing the cURL handle, you must explicitly call curl_close() to release the cURL resources; otherwise, it may cause memory leaks.

  3. Concurrency management: In high concurrency scenarios, using curl_multi_remove_handle helps effectively manage multiple concurrent requests by promptly clearing unused handles, preventing unnecessary resource consumption.