Home > Backend Development > PHP Tutorial > Introduction to PHP functions—curl_getinfo(): Get information related to cURL requests

Introduction to PHP functions—curl_getinfo(): Get information related to cURL requests

PHPz
Release: 2023-07-25 12:42:01
Original
2574 people have browsed it

PHP function introduction—curl_getinfo(): Get relevant information about cURL requests

cURL is a powerful tool for sending and receiving data in PHP. It supports various protocols, such as HTTP and FTP , SMTP, etc. When we use cURL to send a request, sometimes we need to obtain some information related to the request, such as the requested URL, response status code, etc. At this time, you can use the curl_getinfo() function to obtain this information.

The curl_getinfo() function returns an associative array containing request-related information. We can obtain different information by specifying different options. The following are some commonly used options and their corresponding information:

CURLOPT_EFFECTIVE_URL: Returns the requested URL address in string form.
CURLOPT_HTTP_CODE: Returns the HTTP status code of the server response in integer form.
CURLOPT_RESPONSE_CODE: Returns the HTTP status code of the server response as an integer (available after cURL version 7.10.8).
CURLOPT_TOTAL_TIME: Returns the total time (seconds) spent in the entire request process as a floating point number.
CURLOPT_CONTENT_LENGTH_DOWNLOAD: Returns the number of bytes of downloaded content as an integer.
CURLOPT_CONTENT_LENGTH_UPLOAD: Returns the number of bytes of the uploaded content as an integer.

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

// 创建一个cURL资源
$curl = curl_init();

// 设置请求的URL地址
curl_setopt($curl, CURLOPT_URL, "https://www.example.com");

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

// 获取请求的相关信息
$info = curl_getinfo($curl);

// 输出请求的URL地址
echo "请求的URL地址:".$info['url']."<br>";

// 输出服务器响应的HTTP状态码
echo "HTTP状态码:".$info['http_code']."<br>";

// 输出整个请求过程花费的总时间
echo "总时间:".$info['total_time']."秒<br>";

// 输出下载内容的字节数
echo "下载内容字节数:".$info['download_content_length']."<br>";

// 输出上传内容的字节数
echo "上传内容字节数:".$info['upload_content_length']."<br>";

// 关闭cURL资源
curl_close($curl);
Copy after login

In the above code, first create a cURL resource and set the requested URL address. Then execute the request, obtain the requested information, and output it to the page. Finally close the cURL resource.

By using the curl_getinfo() function, we can easily obtain the relevant information of the cURL request and process and display it as needed. Whether you are developing a web crawler, making API calls, or sending HTTP requests, understanding and using the curl_getinfo() function is very useful.

To sum up, the curl_getinfo() function is a very practical function. It can help us obtain relevant information about cURL requests and better control and process requests. In daily PHP development, for scenarios where cURL is used for data interaction, we can make full use of this function to improve the flexibility and maintainability of the code.

The above is the detailed content of Introduction to PHP functions—curl_getinfo(): Get information related to cURL requests. For more information, please follow other related articles on the PHP Chinese website!

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