PHP cURL curl_multi_init() Function



The PHP Client URL curl_multi_init() function is used to initialize a new cURL multi handle. This function allows to execute multiple cURL sessions in parallel.

Syntax

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

CurlMultiHandle curl_multi_init ()

Parameters

The curl_multi_exec() function does not take any parameters.

Return Value

The curl_multi_init() function returns a cURL multi handle resource on success, or FALSE 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_init() function to initialize a new cURL multi handle.

<?php
   // Start both cURL resources
   $ch1 = curl_init();
   $ch2 = curl_init();
   
   // Set URL and other required options
   curl_setopt($ch1, CURLOPT_URL, "http://www.google.co.in/");
   curl_setopt($ch1, CURLOPT_HEADER, 0);
   curl_setopt($ch2, CURLOPT_URL, "http://www.tutorix.com/");
   curl_setopt($ch2, CURLOPT_HEADER, 0);
   
   // Create the multiple cURL handle
   $mh = curl_multi_init();
   
   //add the two handles
   curl_multi_add_handle($mh,$ch1);
   curl_multi_add_handle($mh,$ch2);
   
   // Execute the multi handle
   do {
       $status = curl_multi_exec($mh, $active);
       if ($active) {
           curl_multi_select($mh);
       }
   } while ($active && $status == CURLM_OK);
   
   // Close the handles
   curl_multi_remove_handle($mh, $ch1);
   curl_multi_remove_handle($mh, $ch2);
   curl_multi_close($mh);
?> 

Output

It will fetch the contents of both the URLs −

curl_multi_init Output

Example 2

In the below PHP code we will use the curl_multi_init() function to fetch multiple handles at the same time and display the content.

<?php
   // Array of URLs (Replace the URLs as per your requirement)
   $urls = [
       "https://abc.com/todos/1",
       "https://abc.com/todos/2",
       "https://abc.com/todos/3",
   ];
   
   // start the multi handle
   $multiHandle = curl_multi_init();
   $curlHandles = [];
   
   // start and set options for each cURL handle
   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;
   }
   
   // Execute the multi handle
   do {
       $status = curl_multi_exec($multiHandle, $active);
       if ($active) {
           curl_multi_select($multiHandle);
       }
   } while ($active && $status == CURLM_OK);
   
   // Get the content and close each handle
   foreach ($curlHandles as $ch) {
       $response = curl_multi_getcontent($ch);
       echo "Response: $response\n";
       curl_multi_remove_handle($multiHandle, $ch);
       curl_close($ch);
   }
   
   // Close the multi handle
   curl_multi_close($multiHandle);   
?> 

Output

This will generate the below output −

Response: { "userId": 1, "id": 4, "title": "et porro tempora", "completed": true }
Response: { "userId": 1, "id": 5, "title": "laboriosam mollitia et enim quasi adipisci quia provident illum", "completed": false }
Response: { "userId": 1, "id": 6, "title": "qui ullam ratione quibusdam voluptatem quia omnis", "completed": false }

Example 3

Now we will use the curl_multi_init() function to handle errors if any URL is invalid or it does not exist.

<?php
   // Array of the URLs (Replace the URLs as per your requirement)
   $urls = [
    "http://tutorialspoint.com/page1",
    "http://nonexistentwebsite.com/page2", // This link may fail
    "http://tutorix.com/page3"
   ];

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

   do {
      $status = curl_multi_exec($multiHandle, $active);
      if ($active) {
         curl_multi_select($multiHandle);
      }
   } while ($active && $status == CURLM_OK);

   foreach ($curlHandles as $ch) {
      if (curl_errno($ch)) {
         echo "Error: " . curl_error($ch) . "\n";
      } else {
         $response = curl_multi_getcontent($ch);
         echo "Response: $response\n";
      }
      curl_multi_remove_handle($multiHandle, $ch);
      curl_close($ch);
   }

   curl_multi_close($multiHandle);
?> 

Output

This will create the below output −

Response:
Found
The document has moved here.

Response: Response:
Found
The document has moved here.

Summary

The curl_multi_init() is a built-in function to perform multiple cURL requests at the same time. So using this function we can improve the speed and efficiency of our application by handling multiple HTTP requests.

php_function_reference.htm
Advertisements