Using curl_errno() in PHP to Retrieve cURL Error Codes

This article explains the PHP curl_errno() function for obtaining cURL request error codes, provides its syntax, demonstrates practical example code for initializing a cURL handle, executing requests, checking errors, handling common error codes, and summarizing how to improve error handling in backend development.

php Courses
php Courses
php Courses
Using curl_errno() in PHP to Retrieve cURL Error Codes

When making network requests, errors such as timeouts or DNS failures can occur; PHP provides the curl_errno() function to retrieve the error code of a cURL request.

1. Function Overview

The curl_errno() function returns the last error number for the given cURL handle. Its signature is: int curl_errno ( resource $ch ) If no error occurred it returns 0; otherwise a non‑zero error code.

2. Example Code

The following PHP script demonstrates initializing a cURL handle, setting options, executing the request, checking for errors with curl_errno(), and closing the handle.

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

// Set cURL options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    // Get error code
    $error_code = curl_errno($ch);
    echo "cURL request failed, error code: " . $error_code;
} else {
    // No error
    echo "cURL request succeeded!";
}

// Close the handle
curl_close($ch);
?>

The script first creates a cURL handle, configures the URL and return option, runs curl_exec(), and stores the result in $response. It then uses curl_errno() to detect errors, prints the error code if any, or confirms success, and finally releases resources with curl_close().

3. Common Error Codes

Typical cURL error numbers and their meanings include:

CURLE_COULDNT_CONNECT (7): unable to connect

CURLE_OPERATION_TIMEDOUT (28): operation timed out

CURLE_COULDNT_RESOLVE_HOST (6): host could not be resolved

CURLE_SSL_CONNECT_ERROR (35): SSL connection error

CURLE_OK (0): no error

Conclusion

By using curl_errno(), developers can easily obtain the error code of a cURL request, enabling more precise error handling and improving the stability and reliability of backend applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPError Handlingcurl_errno
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.