Mastering curl_errno(): How to Retrieve and Handle cURL Error Codes in PHP

Learn how to use PHP's curl_errno() function to obtain cURL error codes, interpret common error numbers, and implement robust error handling in your network requests with clear code examples and best practices for reliable application stability.

php Courses
php Courses
php Courses
Mastering curl_errno(): How to Retrieve and Handle cURL Error Codes in PHP

Function Overview

curl_errno()

is a PHP function that returns the error code of the most recent cURL request. It accepts a cURL handle resource and returns 0 if no error occurred, otherwise a non‑zero error number.

Usage Example

The following example 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 the 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);
?>

Explanation of the Example

The script first creates a cURL handle and configures it with the target URL and the CURLOPT_RETURNTRANSFER option so the response is returned as a string. curl_exec() performs the request and stores the result in $response.

After execution, curl_errno($ch) checks whether an error occurred. If an error is detected, the same function retrieves the specific error code, which is then printed. If no error is found, a success message is displayed.

Finally, curl_close($ch) releases the resources associated with the handle.

Common cURL Error Codes

CURLE_COULDNT_CONNECT (7): Unable to connect to the remote host.

CURLE_OPERATION_TIMEDOUT (28): The operation timed out.

CURLE_COULDNT_RESOLVE_HOST (6): Host name could not be resolved.

CURLE_SSL_CONNECT_ERROR (35): SSL connection failed.

CURLE_OK (0): No error.

Conclusion

By using curl_errno(), developers can easily obtain the error code of a cURL request, allowing them to implement precise error handling, improve application stability, and respond appropriately to network issues.

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.

Error HandlingcURLweb-development
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.