• 欢迎访问速搜资源吧,如果在网站上找不到你需要的资源,可以在留言板上留言,管理员会尽量满足你!

【速搜问答】cURL扩展是什么

问答 admin 1年前 (2020-09-06) 262次浏览 已收录 0个评论

汉英对照:
Chinese-English Translation:

CURL扩展是一个非常强大的开源库,支持很多协议,包括HTTP、FTP、TELNET等,使用它来发送HTTP请求。它给我们带来的好处是可以通过灵活的选项设置不同的HTTP协议参数,并且支持HTTPS。 CURL 扩展是一个非常强大的开源库,支持很多协议,包括 HTTP、FTP、TELNET 等,我们使用它来发送 HTTP 请求。它给我们带来的好处是可以通过灵活的选项设置不同的 HTTP 协议参数,并且支持 HTTPS。CURL 可以根据 URL 前缀是“HTTP” 还是“HTTPS”自动选择是否加密发送内容。

Curl extension is a very powerful open source library. It supports many protocols, including HTTP, FTP, Telnet, etc. it is used to send HTTP requests. The advantage it brings us is that we can set different HTTP protocol parameters through flexible options, and support HTTPS. Curl extension is a very powerful open source library. It supports many protocols, including HTTP, FTP, Telnet, etc. we use it to send HTTP requests. The advantage it brings us is that we can set different HTTP protocol parameters through flexible options, and support HTTPS. Curl can automatically choose whether to encrypt the sent content based on whether the URL prefix is “HTTP” or “HTTPS“.

使用 CURL 发送请求的基本流程

The basic process of sending requests with curl

使用 CURL 的 PHP 扩展完成一个 HTTP 请求的发送一般有以下几个步骤:

Using curl’s PHP extension to send an HTTP request generally includes the following steps:

初始化连接句柄;

Initialize the connection handle;

设置 CURL 选项;

Set the curl option;

执行并获取结果;

Execute and obtain results;

释放 VURL 连接句柄。

Release the vURL connection handle.

上述代码中使用到了四个函数

Four functions are used in the above code

curl_init() 和 curl_close() 分别是初始化 CURL 连接和关闭 CURL 连接,都比较简单。

curl_ Init() and curl_ Close() is to initialize the curl connection and close the curl connection, which is relatively simple.

curl_exec() 执行 CURL 请求,如果没有错误发生,该函数的返回是对应 URL 返回的数据,以字符串表示满意;如果发生错误,该函数返回 FALSE。需要注意的是,判断输出是否为 FALSE 用的是全等号,这是为了区分返回空串和出错的情况。

curl_ Exec() executes the curl request. If no error occurs, the return of this function is the data returned from the corresponding URL, which is expressed as a string. If an error occurs, the function returns false. It should be noted that if the output is false, the full sign is used to distinguish between the return of an empty string and an error.

CURL 函数库里最重要的函数是 curl_setopt(),它可以通过设定 CURL 函数库定义的选项来定制 HTTP 请求。上述代码片段中使用了三个重要的选项:

The most important function in the curl library is curl_ Setopt (), which can customize HTTP requests by setting options defined by the curl function library. The above three important options are used in the code:

CURLOPT_URL 指定请求的 URL;

CURLOPT_ URL specifies the URL of the request;

CURLOPT_RETURNTRANSFER 设置为 1 表示稍后执行的 curl_exec 函数的返回是 URL 的返回字符串,而不是把返回字符串定向到标准输出并返回 TRUE;

CURLOPT_ If returntransfer is set to 1, curl will be executed later_ The return of the exec function is the return string of the URL, rather than directing the return string to the standard output and returning true;

CURLLOPT_HEADER 设置为 0 表示不返回 HTTP 头部信息。

CURLLOPT_ If header is set to 0, no HTTP header information is returned.

CURL 的选项还有很多,可以到 PHP 的官方网站(http://www.php.net/manual/en/function.curl-setopt.php)上查看 CURL 支持的所有选项列表。

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

获取 CURL 请求的输出信息

Get output information of curl request

在 curl_exec()函数执行之后,可以使用 curl_getinfo()函数获取 CURL 请求输出的相关信息,示例代码如下:

At curl_ After the exec() function is executed, curl can be used_ The getinfo() function gets the relevant information output from the curl request. The example code is as follows:

图片

picture

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

Curl in the above code_ Getinfo returns an associative array containing the following data:

url:网络地址。

URL: network address.

content_type:内容编码。

content_ Type: content encoding.

http_code:HTTP 状态码。

http_ code:HTTP Status code.

header_size:header 的大小。

header_ size:header The size of.

request_size:请求的大小。

request_ Size: the size of the request.

filetime:文件创建的时间。

FILETIME: the time when the file was created.

ssl_verify_result:SSL 验证结果。

ssl_ verify_ result:SSL Verification results.

redirect_count:跳转计数。

redirect_ Count: jump count.

total_time:总耗时。

total_ Time: total time.

namelookup_time:DNS 查询耗时。

namelookup_ time:DNS Query time consuming.

connect_time:等待连接耗时。

connect_ Time: waiting for connection time.

pretransfer_time:传输前准备耗时。

pretransfer_ Time: preparation time before transmission.

size_uplpad:上传数据的大小。

size_ Uplpad: the size of the uploaded data.

size_download:下载数据的大小。

size_ Download: the size of the download data.

speed_download:下载速度。

speed_ Download: download speed.

speed_upload:上传速度。

speed_ Upload: upload speed.

download_content_length:下载内容的长度。

download_ content_ Length: the length of the downloaded content.

upload_content_length:上传内容的长度。

upload_ content_ Length: the length of the uploaded content.

starttransfer_time:开始传输的时间表。

starttransfer_ Time: schedule to start the transfer.

redirect_time:重定向耗时。

redirect_ Time: redirection time.

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

curl_ The getinfo() function also has an optional parameter $opt. Through this parameter, you can set some constants corresponding to the previous operation field. If the second parameter is set, only the specified information will be returned. For example, set $opt to curlinfo_ TOTAL_ Time, then curl_ The getinfo() function only returns total_ Time, that is, the total transmission time. When you only need to pay attention to some transmission information, setting the $opt parameter is very meaningful.


速搜资源网 , 版权所有丨如未注明 , 均为原创丨转载请注明原文链接:【速搜问答】cURL扩展是什么
喜欢 (0)
[361009623@qq.com]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址