PHP cURL curl_multi_errno() Function
The PHP Client URL curl_multi_errno() function is used to return an integer containing the last multi-curl error number. The error number shows if there is an issue. It will parse the content of the response if there are no problems.
Syntax
Below is the syntax of the PHP cURL curl_multi_errno() function −
int curl_multi_errno ( resource $multi_handle )
Parameters
This function accepts $multi_handle parameter which is the multi handle resource returned by curl_multi_init().
Return Value
The curl_multi_errno() function returns the error number (an integer) for the most recent error or it returns CURLM_OK (which is 0).
PHP Version
Introduced in core PHP 7.1.0, the curl_multi_errno() function works well with PHP 8.
Example 1
Here is the simple example to show you how to use the PHP cURL curl_multi_errno() function.
<?php
// Array of URLs
$multi_handle = curl_multi_init();
$ch1 = curl_init("http://example.com");
$ch2 = curl_init("http://example.org");
curl_multi_add_handle($multi_handle, $ch1);
curl_multi_add_handle($multi_handle, $ch2);
do {
$status = curl_multi_exec($multi_handle, $active);
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);
if ($status !== CURLM_OK) {
$error_no = curl_multi_errno($multi_handle);
echo "cURL multi handle error: $error_no\n";
}
curl_multi_remove_handle($multi_handle, $ch1);
curl_multi_remove_handle($multi_handle, $ch2);
curl_close($ch1);
curl_close($ch2);
curl_multi_close($multi_handle);
?>
Output
In the browser the pages will be displayed of the given URLs if no error found −
Example 2
Here is another way to use the curl_multi_errno() function to check if there was any error at the time of execution.
<?php
// Start a cURL multi handle
$multi_handle = curl_multi_init();
// Start individual cURL handles
$ch1 = curl_init();
$ch2 = curl_init();
// Set options for cURL handles
curl_setopt($ch1, CURLOPT_URL, "http://tutorialspoint.org");
curl_setopt($ch2, CURLOPT_URL, "http://tutorix.org");
// Add the handles to the multi handle
curl_multi_add_handle($multi_handle, $ch1);
curl_multi_add_handle($multi_handle, $ch2);
// Execute the multi handle
do {
$status = curl_multi_exec($multi_handle, $active);
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);
// Check for errors
if ($status !== CURLM_OK) {
$error_no = curl_multi_errno($multi_handle);
echo "cURL multi handle error: $error_no\n";
}
// Remove the handles from the multi handle
curl_multi_remove_handle($multi_handle, $ch1);
curl_multi_remove_handle($multi_handle, $ch2);
// Close the handles
curl_close($ch1);
curl_close($ch2);
curl_multi_close($multi_handle);
Output
This will generate the below output −
Object not found! The requested URL was not found on this server. Error 404
Summary
The curl_multi_errno() method searches for errors after the multi handle has been processed. If there is an issue, the error number is displayed. If there are no errors, it will parse the content of the response.