Backend Development 4 min read

Using curl_errno() in PHP to Retrieve cURL Error Codes

This article explains the PHP curl_errno() function, its definition, usage, and provides sample code demonstrating how to retrieve and handle cURL error codes, along with a list of common error numbers and their meanings to improve network request error handling.

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

When making network requests, errors such as connection timeouts or DNS resolution failures often occur. PHP provides the useful curl_errno() function to obtain the error code of a cURL request. This article introduces the usage of curl_errno() and offers example code.

1. Function Overview

curl_errno() is a PHP function used to get the error code of a cURL request. Its definition is:

int curl_errno ( resource $ch )

The function accepts a cURL handle as its parameter and returns the error code from the most recent request. It returns 0 if no error occurred; otherwise, it returns a non‑zero error code.

2. Example Code

The following is a sample that uses the curl_errno() function:

<?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 cURL request
$response = curl_exec($ch);

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

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

In this example we first initialize a cURL handle and set options such as the request URL and whether to return the response. Then we call curl_exec() to perform the request and store the result in the $response variable.

Next we use curl_errno() to check if an error occurred. If there is an error, we retrieve the code with curl_errno() , store it in $error_code , and output the error message. If no error occurs, we output a success message.

Finally, we call curl_close() to close the handle and release resources.

3. Common Error Codes

Below are some common cURL error codes and their meanings:

1. CURLE_COULDNT_CONNECT (7): Unable to connect to the server.

2. CURLE_OPERATION_TIMEDOUT (28): Operation timed out.

3. CURLE_COULDNT_RESOLVE_HOST (6): Could not resolve host.

4. CURLE_SSL_CONNECT_ERROR (35): SSL connection error.

5. CURLE_OK (0): No error occurred.

These codes help developers understand why a cURL request failed and allow appropriate handling.

Conclusion

By using the curl_errno() function, developers can easily obtain cURL error codes and handle network request errors more effectively. Incorporating error‑code checks into your PHP applications improves stability and reliability.

BackendPHPError handlingcurlcurl_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

login 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.