PHP cURL curl_multi_close() Function



The PHP Client URL curl_multi_close() function is used to close a cURL multi handle which was previously created with the help of curl_multi_init() function. This function is very useful when managing multiple cURL requests in PHP.

Syntax

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

void curl_multi_close (resource $mh)

Parameters

This function accepts $mh parameter which is a cURL multi handle resource returned by curl_multi_init().

Return Value

The curl_multi_close() function does not return any value.

PHP Version

Introduced in core PHP 5, the curl_multi_close() function works well with PHP 7, and PHP 8.

Example 1

Here is the basic example of how to use the PHP cURL curl_multi_close() function to close a cURL multi handle.

<?php
   // Start a cURL multi handle
   $mh = curl_multi_init();
   
   // Initialize individual cURL handles
   $ch1 = curl_init("http://abc.com");
   $ch2 = curl_init("http://abc.org");
   
   // Add individual handles to the multi handle
   curl_multi_add_handle($mh, $ch1);
   curl_multi_add_handle($mh, $ch2);
   
   // Execute all requests
   do {
       $status = curl_multi_exec($mh, $active);
       curl_multi_select($mh);
   } while ($active && $status == CURLM_OK);
   
   // Remove and close the individual handles
   curl_multi_remove_handle($mh, $ch1);
   curl_multi_remove_handle($mh, $ch2);
   curl_close($ch1);
   curl_close($ch2);
   
   // Close the multi handle
   curl_multi_close($mh);

Output

The pages will be displayed of the given URLs code −

curl_multi_close Output

Example 2

In the below PHP code we will try to use the curl_multi_close() function to close multiple handles.

<?php
   // Array of URLs
   $urls = ["http://abc123.com", "http://abc123.org", "http://abc123.net"];
   $multiHandle = curl_multi_init();
   $curlHandles = [];

   // Initialize and add individual cURL handles
   foreach ($urls as $url) {
      $ch = curl_init($url);
      curl_multi_add_handle($multiHandle, $ch);
      $curlHandles[] = $ch;
   }

   // Execute all requests
   do {
      $status = curl_multi_exec($multiHandle, $active);
      curl_multi_select($multiHandle);
   } while ($active && $status == CURLM_OK);

   // Remove and close the individual handles
   foreach ($curlHandles as $ch) {
      curl_multi_remove_handle($multiHandle, $ch);
      curl_close($ch);
   }

   // Close the multi handle
   curl_multi_close($multiHandle);
?> 

Output

This will show the pages of the given URLs −

curl_multi_close Output

Example 3

Now we will use the curl_multi_close() function and call a rest api to get the repos of the github user.

<?php
   $mh = curl_multi_init();
   $ch1 = curl_init("http://invalid-url.com");
   $ch2 = curl_init("http://invalidurl.org");
   
   curl_multi_add_handle($mh, $ch1);
   curl_multi_add_handle($mh, $ch2);
   
   do {
       $status = curl_multi_exec($mh, $active);
       curl_multi_select($mh);
   } while ($active && $status == CURLM_OK);
   
   // Check for errors
   foreach ([$ch1, $ch2] as $ch) {
       if (curl_errno($ch)) {
           echo 'Error: ' . curl_error($ch);
       }
       curl_multi_remove_handle($mh, $ch);
       curl_close($ch);
   }
   
   curl_multi_close($mh); 
?> 

Output

This will create the below output −

Error: Could not resolve host: invalid-url.com
Error: Could not resolve host: invalidurl.org
php_function_reference.htm
Advertisements