Using curl_errno() in PHP to Retrieve cURL Error Codes
This article explains the PHP curl_errno() function, shows how to use it to obtain cURL error codes with example code, lists common error numbers, and demonstrates proper error handling to improve application stability.
When making network requests, errors such as connection timeouts or DNS failures can occur; PHP provides the curl_errno() function to retrieve the error code of a cURL request. This article introduces the usage of curl_errno() and provides example code.
1. Function Overview
curl_errno()is a PHP function that returns the error code of the most recent cURL operation for a given cURL handle. int curl_errno ( resource $ch ) The function accepts a cURL handle as its argument and returns 0 if no error occurred, otherwise a non‑zero error code.
2. Example Code
The following example demonstrates initializing a cURL handle, setting options, executing the request, checking for errors with curl_errno(), and closing the handle.
<?php<br/>// Initialize a cURL handle<br/>$ch = curl_init();<br/><br/>// Set cURL options<br/>curl_setopt($ch, CURLOPT_URL, "http://www.example.com");<br/>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/><br/>// Execute the request<br/>$response = curl_exec($ch);<br/><br/>// Check for errors<br/>if (curl_errno($ch)) {<br/> // Get error code<br/> $error_code = curl_errno($ch);<br/> echo "cURL request error, code: " . $error_code;<br/>} else {<br/> // No error
echo "cURL request succeeded!";<br/>}<br/><br/>// Close the handle<br/>curl_close($ch);<br/>?>In this example, after initializing the handle and setting options (such as the request URL and whether to return the response), curl_exec() performs the request and stores the result in $response. Then curl_errno() checks for errors; if an error exists, the error code is retrieved and displayed, otherwise a success message is shown. Finally, curl_close() releases the handle.
3. Common Error Codes
Some frequently encountered cURL error codes and their meanings are:
1. CURLE_COULDNT_CONNECT (7) : Unable to connect to the host.
2. CURLE_OPERATION_TIMEDOUT (28) : Operation timed out.
3. CURLE_COULDNT_RESOLVE_HOST (6) : Failed to resolve the host name.
4. CURLE_SSL_CONNECT_ERROR (35) : SSL connection error.
5. CURLE_OK (0) : No error occurred.
Conclusion
By using the curl_errno() function, developers can easily obtain cURL error codes and implement appropriate error‑handling logic, thereby enhancing the stability and reliability of their applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
