Home  >  Article  >  Backend Development  >  A brief introduction to PHP's CURL_PHP tutorial

A brief introduction to PHP's CURL_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:44:10745browse

什么是CURL,PHP与CURL

(PHP CURL)

一 什么是CURL

curl是一个利用URL语法在命令行方式下工作的文件传输工具。curl是一个利用URL语法在命令行方式下工作的文件传输工具。

它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同样支持HTTPS认证,HTTP POST方法, HTTP PUT方法, FTP上传, kerberos认证, HTTP上传, 代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,

上载文件断点续传, http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器, 通过http代理服务器上传文件到FTP服务器等等,功能十分强大。

原来php默认并不进行此项功能的扩展,但还是有的,只是没有让它生效罢了。打开PHP安装目录,搜索以下三个文件 ssleay32.dll、libeay32.dll和 php_curl.dll,

一一拷贝到系统目录下的system32文件夹下,修改php.ini文件,找到;extension= php_curl.dll行,去掉前面的;号,保存,重启服务器

网站采集,登陆,COOKIE等

二 PHP中的CURL库

curl_close - 关闭一个curl会话

curl_copy_handle - 拷贝一个curl连接资源的所有内容和参数

curl_errno - 返回一个包含当前会话错误信息的数字编号

curl_error - 返回一个包含当前会话错误信息的字符串

curl_exec - 执行一个curl会话

curl_getinfo - 获取一个curl连接资源句柄的信息

curl_init - 初始化一个curl会话

curl_multi_add_handle - 向curl批处理会话中添加单独的curl句柄资源

curl_multi_close - 关闭一个批处理句柄资源

curl_multi_exec - 解析一个curl批处理句柄

curl_multi_getcontent - 返回获取的输出的文本流

curl_multi_info_read - 获取当前解析的curl的相关传输信息

curl_multi_init - 初始化一个curl批处理句柄资源

curl_multi_remove_handle - 移除curl批处理句柄资源中的某个句柄资源

curl_multi_select - Get all the sockets associated with the cURL extension, which can then be "selected"

curl_setopt_array - 以数组的形式为一个curl设置会话参数

curl_setopt - 为一个curl设置会话参数

curl_version - 获取curl相关的版本信息

curl_init()函数的作用初始化一个curl会话,curl_init()函数唯一的一个参数是可选的,表示一个url地址。

curl_exec()函数的作用是执行一个curl会话,唯一的参数是curl_init()函数返回的句柄。

curl_close()函数的作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。

三关于CURL_SETOPT()函数的设置

bool curl_setopt (int ch, string option, mixed value)

curl_setopt()函数将为一个CURL会话设置选项。option参数是你想要的设置,value是这个选项给定的值。

下列选项的值将被作为长整形使用(在option参数中指定):

* CURLOPT_INFILESIZE: 当你上传一个文件到远程站点,这个选项告诉PHP你上传文件的大小。

* CURLOPT_VERBOSE: 如果你想CURL报告每一件意外的事情,设置这个选项为一个非零值。

* CURLOPT_HEADER: 如果你想把一个头包含在输出中,设置这个选项为一个非零值。

* CURLOPT_NOPROGRESS: 如果你不会PHP为CURL传输显示一个进程条,设置这个选项为一个非零值。注意:PHP自动设置这个选项为非零值,你应该仅仅为了调试的目的来改变这个选项。

* CURLOPT_NOBODY: 如果你不想在输出中包含body部分,设置这个选项为一个非零值。

* CURLOPT_FAILONERROR: 如果你想让PHP在发生错误(HTTP代码返回大于等于300)时,不显示,设置这个选项为一人非零值。默认行为是返回一个正常页,忽略代码。

* CURLOPT_UPLOAD: 如果你想让PHP为上传做准备,设置这个选项为一个非零值。

* CURLOPT_POST: 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。

* CURLOPT_FTPLISTONLY: 设置这个选项为非零值,PHP将列出FTP的目录名列表。

* CURLOPT_FTPAPPEND: 设置这个选项为一个非零值,PHP将应用远程文件代替覆盖它。

* CURLOPT_NETRC: 设置这个选项为一个非零值,PHP将在你的 ~./netrc 文件中查找你要建立连接的远程站点的用户名及密码。

* CURLOPT_FOLLOWLOCATION: 设置这个选项为一个非零值(象 “Location: “)的头,服务器会把它当做HTTP头的一部分发送(注意这是递归的,PHP将发送形如 “Location: “的头)。

* CURLOPT_PUT: Set this option to a non-zero value to upload a file using HTTP. To upload this file the CURLOPT_INFILE and CURLOPT_INFILESIZE options must be set.

* CURLOPT_MUTE: Set this option to a non-zero value and PHP will be completely silent for the CURL function.

* CURLOPT_TIMEOUT: Set a long integer as the maximum number of seconds.

* CURLOPT_LOW_SPEED_LIMIT: Set a long integer to control how many bytes are sent.

* CURLOPT_LOW_SPEED_TIME: Set a long integer to control how many seconds to transmit the number of bytes specified by CURLOPT_LOW_SPEED_LIMIT.

* CURLOPT_RESUME_FROM: Pass a long parameter containing the byte offset address, (the start form you want to transfer to).

* CURLOPT_SSLVERSION: Pass a long parameter containing the SSL version. By default PHP will do its own hard work, in more security you have to set it manually.

* CURLOPT_TIMECONDITION: Pass a long parameter to specify how to handle the CURLOPT_TIMEVALUE parameter. You can set this parameter to TIMECOND_IFMODSINCE or TIMECOND_ISUNMODSINCE. This is only for HTTP.

* CURLOPT_TIMEVALUE: Pass a number of seconds from 1970-1-1 to now. This time will be used as the specified value by the CURLOPT_TIMEVALUE option, or by the default TIMECOND_IFMODSINCE.

The values ​​of the following options will be treated as strings:

* CURLOPT_URL: This is the URL address you want to retrieve using PHP. You can also set this option during initialization with the curl_init() function.

* CURLOPT_USERPWD: Pass a string in the form of [username]:[password] and use PHP to connect.

* CURLOPT_PROXYUSERPWD: Pass a string in the format [username]:[password] to connect to the HTTP proxy.

* CURLOPT_RANGE: Pass a range you want to specify. It should be in "X-Y" format, with X or Y being excluded. HTTP transfers also support several intervals, separated by commas (X-Y,N-M).

* CURLOPT_POSTFIELDS: Pass a string containing all the data as an HTTP "POST" operation.

* CURLOPT_REFERER: A string containing a "referer" header in the HTTP request.

* CURLOPT_USERAGENT: A string containing a "user-agent" header in the HTTP request.

* CURLOPT_FTPPORT: Pass an IP address containing the IP address used by the ftp "POST" command. This POST command tells the remote server to connect to the IP address we specified. This string can be an IP address, a host name, a network interface name (under UNIX), or '-' (use the system default IP address).

* CURLOPT_COOKIE: Pass a header connection containing an HTTP cookie.

* CURLOPT_SSLCERT: Pass a string containing the certificate in PEM format.

* CURLOPT_SSLCERTPASSWD: Pass a password containing the necessary password to use the CURLOPT_SSLCERT certificate.

* CURLOPT_COOKIEFILE: Pass a string containing the name of the file containing cookie data. This cookie file can be in Netscape format, or it can be a stack of HTTP-style headers stored in the file.

* CURLOPT_CUSTOMREQUEST: When making an HTTP request, pass a character to be used by GET or HEAD. Pass a string to be used instead of GET or HEAD when doing an HTTP request. This is useful for doing or another, more obscure, HTTP request. Note: Make sure your server supports Order not to do it yet. The following options require a file description (obtained using the fopen() function):

* CURLOPT_FILE: This file will be the output file you place and transmit. The default is STDOUT.

* CURLOPT_INFILE: This file is the input file you sent.

* CURLOPT_WRITEHEADER: This file contains the header part of your output.

* CURLOPT_STDERR: This file has errors written instead of stderr. An example of obtaining a page that requires login. The current method is to log in once every time. People who need it will make improvements.

Four related examples

1 Example
view plaincopy to clipboardprint?

$cookie_jar = tempnam(./tmp,cookie);

$ch = curl_init(); curl_setopt($ch,CURLOPT_URL , http://******);

curl_setopt($ch, CURLOPT_POST, 1);

$request = email_address=&password=&action=;

curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//Save the returned cookie information in the $cookie_jar file

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);

//Set whether the returned data is automatically displayed

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Set whether to display header information

curl_setopt( $ch, CURLOPT_HEADER, false);

//Set whether to output page content

curl_setopt($ch, CURLOPT_NOBODY, false);

curl_exec($ch);

curl_close($ch); //get data after login

$ch2 = curl_init();

curl_setopt($ch2, CURLOPT_URL, http://** ***);

curl_setopt($ch2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478764.htmlTechArticleWhat is CURL, PHP and CURL (PHP CURL) 1. What is CURL curl is a tool that uses URL syntax on the command line File transfer tool that works in . curl is a command line method that uses URL syntax...
Statement:
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