Current Location: Home> Latest Articles> PHP Function Tutorial: How to Use curl_errno() to Get cURL Request Error Code

PHP Function Tutorial: How to Use curl_errno() to Get cURL Request Error Code

gitbox 2025-06-12

PHP Function Tutorial: How to Use curl_errno() to Get cURL Request Error Code

When making network requests, developers often encounter various errors, such as connection timeouts or DNS resolution failures. To handle these errors more effectively, PHP provides a useful function called curl_errno()

In the example above, we first initialize a cURL handle and set several options, such as the request URL and whether to return the response content. Then, we execute the cURL request using the curl_exec() function, storing the result in the $response variable.

Next, we use the curl_errno() function to check for errors. If an error occurs, we use curl_errno() to retrieve the error code, store it in the $error_code variable, and display the error message. If no error occurs, we display "cURL request successful!".

Finally, we close the cURL handle using curl_close() to release the resources.

3. Common Error Codes

Here are some common cURL error codes and their meanings:

  1. CURLE_COULDNT_CONNECT (7): Unable to connect
  2. CURLE_OPERATION_TIMEDOUT (28): Operation timed out
  3. CURLE_COULDNT_RESOLVE_HOST (6): Unable to resolve host
  4. CURLE_SSL_CONNECT_ERROR (35): SSL connection error
  5. CURLE_OK (0): No error occurred

These error codes help developers understand the issues that may arise during a cURL request, enabling them to handle the errors accordingly.

Conclusion

By using the curl_errno() function, developers can easily retrieve the error code of a cURL request and handle network request errors more effectively. In real-world development, different error codes can be processed in various ways to improve the stability and user experience of applications.

We hope this article and the sample code have helped you understand and use the curl_errno() function!