PHP cURL curl_multi_remove_handle() Function



The PHP Client URL curl_multi_remove_handle() function is used to remove a cURL handle from a cURL multi handle.

Syntax

Below is the syntax of the PHP cURL curl_multi_remove_handle() function −

int curl_multi_remove_handle(resource $mh, resource $ch)

Parameters

Following are the parameters required for the curl_multi_exec() function −

  • $mh − It is the cURL multi handle resource from which the cURL handle should be removed.

  • $ch − It is the cURL handle resource to be removed.

Return Value

The curl_multi_remove_handle() function returns zero on success, or a non-zero error code on failure.

PHP Version

This function was introduced in core PHP 5 and is compatible with PHP 7, and PHP 8.

Example 1

Here is the basic example of how to use the PHP cURL curl_multi_remove_handle() function to remove cURL handles after executing them.

<?php
   // Create multiple cURL handles
   $ch1 = curl_init();
   $ch2 = curl_init();
   
   // Set options for each handle
   curl_setopt($ch1, CURLOPT_URL, "http://example.com");
   curl_setopt($ch2, CURLOPT_URL, "http://example.org");
   
   // Create a cURL multi handle
   $mh = curl_multi_init();
   
   // Add the cURL handles to the multi handle
   curl_multi_add_handle($mh, $ch1);
   curl_multi_add_handle($mh, $ch2);
   
   // Execute all queries simultaneously
   do {
       $status = curl_multi_exec($mh, $active);
       // Wait a short time to avoid busy-waiting
       curl_multi_select($mh);
   } while ($active && $status == CURLM_OK);
   
   // Remove the handles from the multi handle
   curl_multi_remove_handle($mh, $ch1);
   curl_multi_remove_handle($mh, $ch2);
   
   // Close the handles
   curl_close($ch1);
   curl_close($ch2);
   curl_multi_close($mh);
?> 

Output

Here is the outcome of the following code −

curl_multi_remove_handle Output

Example 2

In the below PHP code we will use the curl_multi_remove_handle() function to remove the handle after processing them.

<?php
   // Create multiple cURL handles
   $ch1 = curl_init("http://tutorialspoint.com");
   $ch2 = curl_init("http://tutorix.com");
   $ch3 = curl_init("http://example.net");
   
   // Create a cURL multi handle
   $mh = curl_multi_init();
   
   // Add the cURL handles to the multi handle
   curl_multi_add_handle($mh, $ch1);
   curl_multi_add_handle($mh, $ch2);
   curl_multi_add_handle($mh, $ch3);
   
   // Execute all queries simultaneously
   do {
       $status = curl_multi_exec($mh, $active);
       // Wait a short time to avoid busy-waiting
       curl_multi_select($mh);
   } while ($active && $status == CURLM_OK);
   
   // Remove the handles from the multi handle
   curl_multi_remove_handle($mh, $ch1);
   curl_multi_remove_handle($mh, $ch2);
   curl_multi_remove_handle($mh, $ch3);
   
   // Close the handles
   curl_close($ch1);
   curl_close($ch2);
   curl_close($ch3);
   curl_multi_close($mh);
?> 

Output

This will display the content of all the given URLs −

curl_multi_remove_handle Output

Example 3

Now we will download the image files and remove the URLs from the cURL handle using the curl_multi_remove_handle() function.

<?php
   // Create cURL handles for image URLs
   $ch1 = curl_init("../image.jpg");
   $ch2 = curl_init("../logo.gif");
   
   // Set options to save the images to files
   $fp1 = fopen("image1.jpg", "wb");
   $fp2 = fopen("image2.jpg", "wb");
   curl_setopt($ch1, CURLOPT_FILE, $fp1);
   curl_setopt($ch2, CURLOPT_FILE, $fp2);
   
   // Create a cURL multi handle
   $mh = curl_multi_init();
   
   // Add the cURL handles to the multi handle
   curl_multi_add_handle($mh, $ch1);
   curl_multi_add_handle($mh, $ch2);
   
   // Execute all queries 
   do {
       $status = curl_multi_exec($mh, $active);
       // Wait a short time to avoid busy-waiting
       curl_multi_select($mh);
   } while ($active && $status == CURLM_OK);
   
   // Remove the handles from the multi handle
   curl_multi_remove_handle($mh, $ch1);
   curl_multi_remove_handle($mh, $ch2);
   
   echo "The images are saved as image1.jpg and image2.jpg";

   // Close the handles and files
   curl_close($ch1);
   curl_close($ch2);
   fclose($fp1);
   fclose($fp2);
   curl_multi_close($mh);
?> 

Output

This will create the below output −

The images are saved as image1.jpg and image2.jpg

Summary

The curl_multi_remove_handle() function removes a handle from the multi handle handle by removing it. Once the handle has been removed, using curl_exec() on this handle is again perfectly legal. If the handle is removed while the transfer is in progress, it will terminate the transfer process.

php_function_reference.htm
Advertisements