PHP cURL curl_multi_getcontent() Function



The PHP Client URL curl_multi_getcontent() function is used to get the response content from the cURL handle $ch after it has been executed within the multi-handle. It is very helpful for working with multiple cURL handles at the same time.

This function can be used to get the response body for a specific cURL handle ($ch) once curl_multi_exec() has completed executing the request.

Syntax

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

string curl_multi_getcontent ( resource $ch )

Parameters

This function accepts $ch parameter which is a cURL handle returned by curl_init().

Return Value

The curl_multi_getcontent() function returns the content, or response body, connected with the cURL handle $ch. If the handle is empty or there is an error, it returns an empty string ("").

This function returns the content of a cURL handle as a string if the option CURLOPT_RETURNTRANSFER is set for a particular handle.

PHP Version

Introduced in core PHP 5, the curl_multi_getcontent() 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_getcontent() function to get the response content from the given URL.

<?php
   // Start a cURL handle
   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL, "http://example.com");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   
   // Add the handle to a multi-handle
   $mh = curl_multi_init();
   curl_multi_add_handle($mh, $ch);
   
   // Execute the handles
   $running = null;
   do {
       curl_multi_exec($mh, $running);
   } while ($running > 0);
   
   // Get the content for the specific handle
   $response_content = curl_multi_getcontent($ch);
   
   // Close the multi-handle and individual handle
   curl_multi_remove_handle($mh, $ch);
   curl_multi_close($mh);
   
   // Use $response_content as needed
   echo $response_content;

Output

The page will be displayed of the given URL −

curl_multi_getcontent Output

Example 2

In the below PHP code we will try to use the curl_multi_getcontent() function to fetch content from multiple URLs or APIs.

<?php
   // Array of URLs (Replace the URLs as per your requirement)
   $urls = [
      "https://jsonplaceholder.typicode.com/todos/1",
      "https://jsonplaceholder.typicode.com/todos/2",
      "https://jsonplaceholder.typicode.com/todos/3",
   ];

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

   // Array to store cURL handles
   $handles = [];

   // Create cURL handles with timeout
   foreach ($urls as $url) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Set timeout to 10 seconds
      curl_multi_add_handle($mh, $ch);
      $handles[] = $ch;
   }

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

   // Check for errors and get responses
   foreach ($handles as $ch) {
      if (curl_errno($ch)) {
         echo "Error: " . curl_error($ch) . "<br>";
      } else {
         $response = curl_multi_getcontent($ch);
         echo $response . "<br>";
      }
   }

   // Remove handles and close multi handle
   foreach ($handles as $ch) {
      curl_multi_remove_handle($mh, $ch);
      curl_close($ch);
   }
   curl_multi_close($mh);
?> 

Output

This will generate the below output −

{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }
{ "userId": 1, "id": 2, "title": "quis ut nam facilis et officia qui", "completed": false }
{ "userId": 1, "id": 3, "title": "fugiat veniam minus", "completed": false }

Example 3

Now we will use the curl_multi_getcontent() function and call the non-existent URL to find the error.

<?php
   // Invalid URL 
   $url = "http://invalid.com/api/nonexistent";
   
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   
   // Execute the request
   $response = curl_exec($ch);
   
   // Check for errors
   if ($response === false) {
       echo "Error: " . curl_error($ch);
   } else {
       $content = curl_multi_getcontent($ch);
       if (empty($content)) {
           echo "No content received from " . $url;
       } else {
           echo "Content from " . $url . ": " . substr($content, 0, 100) . "...";
       }
   }
   
   // Close the handle
   curl_close($ch);
?> 

Output

This will create the below output −

Error: Could not resolve host: abc23.com
php_function_reference.htm
Advertisements