- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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 −
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