PHP cURL curl_strerror() Function



The PHP cURL curl_strerror() function is used to return a text error message that explains the given error code. So basically this function give you a descriptive error messages which makes it easy to debug and handle errors easily.

Syntax

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

string curl_strerror (int $errornum)

Parameters

This function accepts $errornum parameter which is an error code from one of the cURL error codes constants.

Return Value

The curl_strerror() function returns an error message related to the given error number or NULL for invalid error code.

PHP Version

First introduced in core PHP 5.5.0, the curl_strerror() function continues to function easily in PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP cURL curl_strerror() function to get a text error message.

<?php
   // Start a cURL session with a wrong protocol in the URL
   $ch = curl_init("htp://example.com/");
   
   // Execute the cURL session
   curl_exec($ch);
   
   // Check if there is any error and show the error message
   if ($errno = curl_errno($ch)) {
       $error_message = curl_strerror($errno);
       echo "cURL error ({$errno}):\n {$error_message}";
   }
   
   // End the cURL session
   curl_close($ch);
?>

Output

Here is the outcome of the following code −

cURL error (1):
 Unsupported protocol

Example 2

In the below PHP code we will use an invalid URL and see the error message returned by the curl_strerror() function.

<?php
   // Start a cURL session
   $ch = curl_init("http://invalid.url");
   
   // Perform the cURL session
   curl_exec($ch);
   
   // Get the error number
   $error_num = curl_errno($ch);
   
   // Get the error message
   $error_message = curl_strerror($error_num);
   
   // Display the error message
   echo "Error: $error_message";
   
   // Close the cURL session
   curl_close($ch);
?> 

Output

This will generate the below output −

Error: Couldn't resolve host name

Example 3

Now in the below code we will provide a nonexistent page for a valid website URL and see the error message returned by the curl_strerror() function.

<?php
   // Start a cURL session
   $ch = curl_init("http://abc123.com/nonexistent-page1");
   
   // Perform the cURL session
   curl_exec($ch);
   
   // Check if any error occurred
   if (curl_errno($ch)) {
       // Get the error number and message
       $error_num = curl_errno($ch);
       $error_message = curl_strerror($error_num);
       
       // Display the error message
       echo "Failed to fetch the page. Error: $error_message";
   } else {
       echo "Page fetched successfully.";
   }
   
   // Close the cURL session
   curl_close($ch);
?> 

Output

This will create the below output −

404 - Not Found

Page fetched successfully.

Example 4

In the following example, we are using the curl_strerror() function to check if there is any error occur while logging in.

<?php
   // Start a cURL session
   $ch = curl_init("http://abc123.com");
   
   // Perform the cURL session
   curl_exec($ch);
   
   // Check if any error occurred
   if (curl_errno($ch)) {
       // Get the error number and message
       $error_num = curl_errno($ch);
       $error_message = curl_strerror($error_num);
       
       // Log the error message
       error_log("cURL Error: $error_message");
   } else {
       echo "Request successful.";
   }
   
   // Close the cURL session
   curl_close($ch);
?> 

Output

Following is the output of the above code −

Request successful.
php_function_reference.htm
Advertisements