Detailed explanation of using CURL in PHP

不言
Release: 2023-03-23 16:36:01
Original
2583 people have browsed it

The content of this article introduces a detailed explanation of the use of CURL in PHP. Now I share it with you. Friends in need can refer to it

CURL is a very powerful open source library that supports many Protocols, including HTTP, FTP, TELNET, etc., we use it to send HTTP requests. The benefit it brings us is that we can set different HTTP protocol parameters through flexible options and supports HTTPS. CURL can automatically choose whether to encrypt the sent content based on whether the URL prefix is "HTTP" or "HTTPS".

The basic process of sending a request using CURL

Using CURL'sPHP extension to complete sending an HTTP request generally has the following steps:

  1. Initialize the connection handle;

  2. Set CURL options;

  3. Execute and get the results;

  4. Release the VURL connection handle.

The following program fragment is a typical process of sending HTTP using CURL


Detailed explanation of using CURL in PHP# #

// 1. 初始化 $ch = curl_init(); // 2. 设置选项,包括URL curl_setopt($ch,CURLOPT_URL,"http://www.devdo.net"); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_HEADER,0); // 3. 执行并获取HTML文档内容 $output = curl_exec($ch); if($output === FALSE ){ echo "CURL Error:".curl_error($ch); } // 4. 释放curl句柄 curl_close($ch);
Copy after login

Detailed explanation of using CURL in PHP

Four functions are used in the above code

  • curl_init() and curl_close() are initialization respectively CURL connection and closing CURL connection are relatively simple.

  • curl_exec() executes a CURL request. If no error occurs, the function returns the data returned by the corresponding URL, indicating satisfaction as a string; if an error occurs, the function returns FALSE. It should be noted that the equal sign is used to determine whether the output is FALSE. This is to distinguish between returning an empty string and an error.

  • The most important function in the CURL function library is curl_setopt(), which can customize HTTP requests by setting options defined by the CURL function library. Three important options are used in the above code snippet:

  1. CURLOPT_URL specifies the requested URL;

  2. CURLOPT_RETURNTRANSFER is set to 1. The return of the curl_exec function executed later is the return string of the URL, instead of directing the return string to the standard output and returning TRUE;

  3. CURLLOPT_HEADER is set to 0 to indicate that no HTTP header is returned department information.

There are many options for CURL, you can go to PHP’s official website (http://www.php.net/manual/en/function.curl-setopt.php ) to view a list of all options supported by CURL.

Get the output information of the CURL request

After the curl_exec() function is executed, you can use the curl_getinfo() function to obtain the relevant information output of the CURL request. The sample code is as follows:

curl_exec($ch);$info = curl_getinfo($sh);echo ' 获取 '.$info['url'].'耗时'.$info['total_time'].'秒';
Copy after login

上述代码中curl_getinfo返回的是一个关联数组,包含以下数据:

  • url:网络地址。

  • content_type:内容编码。

  • http_code:HTTP状态码。

  • header_size:header的大小。

  • request_size:请求的大小。

  • filetime:文件创建的时间。

  • ssl_verify_result:SSL验证结果。

  • redirect_count:跳转计数。

  • total_time:总耗时。

  • namelookup_time:DNS查询耗时。

  • connect_time:等待连接耗时。

  • pretransfer_time:传输前准备耗时。

  • size_uplpad:上传数据的大小。

  • size_download:下载数据的大小。

  • speed_download:下载速度。

  • speed_upload:上传速度。

  • download_content_length:下载内容的长度。

  • upload_content_length:上传内容的长度。

  • starttransfer_time:开始传输的时间表。

  • redirect_time:重定向耗时。

curl_getinfo()函数还有一个可选择参数$opt,通过这个参数可以设置一些常量,对应到上术这个字段,如果设置了第二个参数,那么返回的只有指定的信息。例如设置$opt为CURLINFO_TOTAL_TIME,则curl_getinfo()函数只返回total_time,即总传输消耗的时间,在只需要关注某些传输信息时,设置$opt参数很有意义。

使用CURL发送GET请求

如何使用CURL来发送GET请求,发送GET请求的关键是拼装格式正确的URL。请求地址和GET数据由一个“?”分割,然后GET变量的名称和值用“=”分隔,各个GET名称和值由“&”连接。PHP为我们提供了一个函数专门用来拼装GET请求和数据部分——http_build_query,该函数接受一个关联数组,返回由该关联数据描述的GET请求字符串。使用这个函数,结合CURL发送HTTP请求的一般流程,我们封闭了一个发送GET请求的函数——doCurlGetRequest,具体代码如下:


Detailed explanation of using CURL in PHP

** *@desc 封闭curl的调用接口,get的请求方式。*/function doCurlGetRequest($url,$data,$timeout = 5){ if($curl == "" || $timeout 

Detailed explanation of using CURL in PHP

这个函数把使用http_build_query 拼装好的带GET参数的URL传给curl_init函数,然后使用CURL发送HTTP请求。

使用CURL发送POST请求

可以使用CURL提供的选项CURLOPT_POSTFIELDS,设置该选项为POST字符串数据就可以把请求放在正文中。同样我们实现了一个发送POST请求的函数——doCurlPostRequest,代码如下:


Detailed explanation of using CURL in PHP

/** ** @desc 封装 curl 的调用接口,post的请求方式 **/function doCurlPostRequest($url,$requestString,$timeout = 5){ if($url == '' || $requestString == '' || $timeout 

Detailed explanation of using CURL in PHP

上面代码中除了设置CURLOPT_POSTFIELDS外,我们还设置了CURL_POST为true,标识这个请求是一个POST请求。在POST请求中也是可以传输GET数据的,只需要在URL中拼装GET请求数据即可秀

相关推荐:

PHP中如何利用CURL实现GET和POST请求

PHP利用CURL将他人服务器图片上传到自己的图片云空间

PHP中的curl 函数并发实现减少后端访问时间

Copy after login

The above is the detailed content of Detailed explanation of using CURL in PHP. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!