Home > Backend Development > PHP Tutorial > PHP function introduction—curl_errno(): Get the error code of cURL request

PHP function introduction—curl_errno(): Get the error code of cURL request

PHPz
Release: 2023-07-25 14:12:02
Original
2373 people have browsed it

PHP function introduction—curl_errno(): Get the error code of cURL request

When making network requests, we often encounter some errors, such as connection timeout, DNS resolution failure, etc. In order to better handle these errors, PHP provides a very useful function curl_errno() for getting the error code of the cURL request. This article will introduce the usage of curl_errno() function and give some sample code.

1. Function introduction

curl_errno() is a function in PHP used to obtain the error code of cURL request. It is defined as follows:

int curl_errno (resource $ch)

This function accepts a cURL handle as a parameter and returns the error code of the most recent cURL request. If no error occurred, it will return 0, otherwise it will return a non-zero error code.

2. Sample code

The following is a sample code using the curl_errno() function:

<?php
// 初始化一个cURL句柄
$ch = curl_init();

// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 执行cURL请求
$response = curl_exec($ch);

// 检查是否发生错误
if(curl_errno($ch)){
    // 获取错误码
    $error_code = curl_errno($ch);
    echo "cURL请求发生错误,错误码:" . $error_code;
} else {
    // 没有发生错误
    echo "cURL请求成功!";
}

// 关闭cURL句柄
curl_close($ch);
?>
Copy after login

In the above example, we first initialize a cURL handle, and Some cURL options are set (such as the requested URL and whether to return response content). We then executed the cURL request using the curl_exec() function and stored its result in the $response variable.

Next, we use the curl_errno() function to check whether an error occurs. If an error occurs, we use the curl_errno() function to get the error code, save it in the $error_code variable, and output the error message on the page. If no errors occur, we output "cURL request successful!".

Finally, we use the curl_close() function to close the cURL handle and release the resources.

3. Common error codes

The following are some common cURL error codes and their meanings:

  1. CURLE_COULDNT_CONNECT (7): Unable to establish connection
  2. CURLE_OPERATION_TIMEDOUT (28): Operation timeout
  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 The occurrence of

These error codes can help us better understand the errors that occur in cURL requests and handle them accordingly.

Summary

By using the curl_errno() function, we can easily obtain the error code of the cURL request to better handle errors that may occur in network requests. In actual development, we can perform different error handling based on error codes to improve the stability and reliability of the application.

I hope the introduction and sample code of this article will help you understand and use the curl_errno() function!

The above is the detailed content of PHP function introduction—curl_errno(): Get the error code of cURL request. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template