Application of CURL and PHP-CLI [CURL]

PHP中文网
Release: 2016-08-08 09:24:23
Original
1374 people have browsed it

curl is an extremely powerful HTTP transfer tool that supports file upload and download;

curl usage at the command line

Command parameters
-a/--append 上传文件时,附加到目标文件
-A/--user-agent设置用户代理发送给服务器
- anyauth   可以使用“任何”身份验证方法
-b/--cookiecookie字符串或文件读取位置
- basic 使用HTTP基本验证
-B/--use-ascii 使用ASCII /文本传输
-c/--cookie-jar操作结束后把cookie写入到这个文件中
-C/--continue-at断点续转
-d/--dataHTTP POST方式传送数据
--data-ascii以ascii的方式post数据
--data-binary以二进制的方式post数据
--negotiate     使用HTTP身份验证
--digest        使用数字身份验证
--disable-eprt  禁止使用EPRT或LPRT
--disable-epsv  禁止使用EPSV
-D/--dump-header把header信息写入到该文件中
--egd-file为随机数据(SSL)设置EGD socket路径
--tcp-nodelay   使用TCP_NODELAY选项
-e/--referer 来源网址
-E/--cert客户端证书文件和密码 (SSL)
--cert-type证书文件类型 (DER/PEM/ENG) (SSL)
--key私钥文件名 (SSL)
--key-type私钥文件类型 (DER/PEM/ENG) (SSL)
--pass私钥密码 (SSL)
--engine加密引擎使用 (SSL). "--engine list" for list
--cacertCA证书 (SSL)
--capathCA目录 (made using c_rehash) to verify peer against (SSL)
--ciphersSSL密码
--compressed    要求返回是压缩的形势 (using deflate or gzip)
--connect-timeout设置最大请求时间
--create-dirs   建立本地目录的目录层次结构
--crlf          上传是把LF转变成CRLF
-f/--fail          连接失败时不显示http错误
--ftp-create-dirs 如果远程目录不存在,创建远程目录
--ftp-method [multicwd/nocwd/singlecwd] 控制CWD的使用
--ftp-pasv      使用 PASV/EPSV 代替端口
--ftp-skip-pasv-ip 使用PASV的时候,忽略该IP地址
--ftp-ssl       尝试用 SSL/TLS 来进行ftp数据传输
--ftp-ssl-reqd  要求用 SSL/TLS 来进行ftp数据传输
-F/--form模拟http表单提交数据
-form-string模拟http表单提交数据
-g/--globoff 禁用网址序列和范围使用{}和[]
-G/--get 以get的方式来发送数据
-h/--help 帮助
-H/--header自定义头信息传递给服务器
--ignore-content-length  忽略的HTTP头信息的长度
-i/--include 输出时包括protocol头信息
-I/--head  只显示文档信息
从文件中读取-j/--junk-session-cookies忽略会话Cookie
- 界面指定网络接口/地址使用
- krb4 启用与指定的安全级别krb4
-j/--junk-session-cookies 读取文件进忽略session cookie
--interface使用指定网络接口/地址
--krb4使用指定安全级别的krb4
-k/--insecure 允许不使用证书到SSL站点
-K/--config  指定的配置文件读取
-l/--list-only 列出ftp目录下的文件名称
--limit-rate设置传输速度
--local-port强制使用本地端口号
-m/--max-time设置最大传输时间
--max-redirs设置最大读取的目录数
--max-filesize设置最大下载的文件总量
-M/--manual  显示全手动
-n/--netrc 从netrc文件中读取用户名和密码
--netrc-optional 使用 .netrc 或者 URL来覆盖-n
--ntlm          使用 HTTP NTLM 身份验证
-N/--no-buffer 禁用缓冲输出
-o/--output 把输出写到该文件中
-O/--remote-name 把输出写到该文件中,保留远程文件的文件名
-p/--proxytunnel   使用HTTP代理
--proxy-anyauth 选择任一代理身份验证方法
--proxy-basic   在代理上使用基本身份验证
--proxy-digest  在代理上使用数字身份验证
--proxy-ntlm    在代理上使用ntlm身份验证
-P/--ftp-port使用端口地址,而不是使用PASV
-Q/--quote文件传输前,发送命令到服务器
-r/--range检索来自HTTP/1.1或FTP服务器字节范围
--range-file 读取(SSL)的随机文件
-R/--remote-time   在本地生成文件时,保留远程文件时间
--retry传输出现问题时,重试的次数
--retry-delay传输出现问题时,设置重试间隔时间
--retry-max-time传输出现问题时,设置最大重试时间
-s/--silent静音模式。不输出任何东西
-S/--show-error   显示错误
--socks4用socks4代理给定主机和端口
--socks5用socks5代理给定主机和端口
--stderr-t/--telnet-optionTelnet选项设置
--trace对指定文件进行debug
--trace-asciiLike --跟踪但没有hex输出
--trace-time    跟踪/详细输出时,添加时间戳
-T/--upload-file上传文件
--urlSpet URL to work with
-u/--user设置服务器的用户和密码
-U/--proxy-user设置代理用户名和密码
-v/--verbose
-V/--version 显示版本信息
-w/--write-out [format]什么输出完成后
-x/--proxy在给定的端口上使用HTTP代理
-X/--request指定什么命令
-y/--speed-time 放弃限速所要的时间。默认为30
-Y/--speed-limit 停止传输速度的限制,速度时间'秒'
-z/--time-cond  传送时间设置
-0/--http1.0  使用HTTP 1.0
-1/--tlsv1  使用TLSv1(SSL)
-2/--sslv2 使用SSLv2的(SSL)
-3/--sslv3         使用的SSLv3(SSL)
--3p-quote      like -Q for the source URL for 3rd party transfer
--3p-url        使用url,进行第三方传送
--3p-user       使用用户名和密码,进行第三方传送
-4/--ipv4   使用IP4
-6/--ipv6   使用IP6
-#/--progress-bar 用进度条显示当前的传送状态
Copy after login
Instances

Catch page content into a file

curl -o home.html  http://www.pinlehuo.com 
$ -o/--output 把输出写到该文件中

# 或者 ↓↓

$ curl http://www.linuxidc.com > page.html
Copy after login

To capture a specific file, use -O (capital), and the following URL must be specific to a certain file, otherwise it will not be captured. We can also use regular expressions to capture things, but the file name still has to be specific

$ curl -O www.pinlehuo.com/d/file/admin/2015/05/5551bf06428ec.jpg

$ curl -O http://www.codesky.net/wp-content/uploads/2010/[0-9][0-9]/aaaaa.jpg
Copy after login

Simulate form information, simulate login, save cookie information

# -D/--dump-header把header信息写入到该文件中
$ curl -c ./cookie_c.txt -F log=aaaa -F pwd=****** http://www.codesky.net/wp-login.php (没试过~)
Copy after login

View response header information

curl -I http://www.sunrisecorp.net/
Copy after login

Simulate form information, simulate login, save header information

curl -D ./cookie_D.txt -F log=aaaa -F pwd=****** http://www.codesky.net/wp-login.php
# -c(小写)产生的cookie和-D里面的cookie是不一样的。
Copy after login

Use cookie files

curl -b ./cookie_c.txt  http://www.codesky.net/wp-admin
Copy after login

Resume breakpoints, -C (capital)

curl -C -O http://www.codesky.net/wp-content/uploads/2010/09/compare_varnish.jpg
Copy after login

Transfer data, it is best to use the login page to test, because after you pass the value, curl will capture the data, and you can see that you passed Is it successful?

curl -d log=aaaa  http://www.codesky.net/wp-login.php
Copy after login

Forge the source address. Some websites will judge and request the source address

curl -e http://localhost http://www.codesky.net/wp-login.php
Copy after login

When we often use curl to get other people’s things, they will block your IP. At this time, we You can use a proxy

curl -x 24.10.28.84:32779 -o home.html http://www.codesky.net
Copy after login

For larger things, we can download in sections

[root@krlcgcms01 mytest]# curl -r 0-100 -o img.part1 http://www.codesky.net/wp-
content/uploads/2010/09/compare_varnish.jpg
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 Dload  Upload   Total   Spent    Left  Speed
100   101  100   101    0     0    105      0 --:--:-- --:--:-- --:--:--     0
[root@krlcgcms01 mytest]# curl -r 100-200 -o img.part2 http://www.codesky.net/wp-
content/uploads/2010/09/compare_varnish.jpg
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 Dload  Upload   Total   Spent    Left  Speed
100   101  100   101    0     0     57      0  0:00:01  0:00:01 --:--:--     0
[root@krlcgcms01 mytest]# curl -r 200- -o img.part3 http://www.codesky.net/wp-
content/uploads/2010/09/compare_varnish.jpg
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 Dload  Upload   Total   Spent    Left  Speed
100  104k  100  104k    0     0  52793      0  0:00:02  0:00:02 --:--:-- 88961
[root@krlcgcms01 mytest]# ls |grep part | xargs du -sh
4.0K    one.part1
112K    three.part3
4.0K    two.part2
用的时候,把他们cat一下就OK了,cat img.part* >img.jpg
Copy after login

Display download progress information

[root@krlcgcms01 mytest]# curl -# -O  http://www.codesky.net/wp-content/uploads/2010/09/compare_varnish.jpg
######################################################################## 100.0%
Copy after login

Download files through ftp

[zhangy@BlackGhost ~]$ curl -u 用户名:密码 -O http://www.codesky.net/demo/curtain/bbstudy_files/style.css
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 Dload  Upload   Total   Spent    Left  Speed
101  1934  101  1934    0     0   3184      0 --:--:-- --:--:-- --:--:--  7136
或者用下面的方式
[zhangy@BlackGhost ~]$ curl -O ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/style.css
Copy after login

Upload through ftp

[zhangy@BlackGhost ~]$ curl -T test.sql ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/
Copy after login

Use of curl under PHP scripts

Install cURL Library

WAMP environment
Open the following 2 files respectively:
wampbinphp(your php version)php.ini
wampbinApache(your apache version)binphp.ini
Search for extension=php_curl.dll, and then delete the previous symbols;
Restart WAMP That’s it.
//↑↑Follow the above operations first, if not, then refer to the following two;
Note: If it is a 64-bit system, you also need to visit http://www.anindya.com/ to download the corresponding version of 64-bit php Compressed package, after decompression, overwrite the php_curl.dll file in the ext directory with the php_curl.dll file in the ext directory of wamp php corresponding version wampbinphpphp5.4.12, and then restart WAMP
Reference website: http://blog.csdn.net/ewili/ article/details/9008493

window
1.php.ini remove the semicolon in front of extension=php_curl.dll;
2.php_curl.dll,libeay32.dll,ssleay32.dll and copy them to %windir%/system32

linux
If php has been compiled in the system, new extensions need to be added later. One way is to completely recompile PHP, and the other way is to compile the extension library separately and extend it in the form of extension. The following is an example of installing the curl extension:
./configure
make
php requires that the curl directory must have include and lib directories, and there must be two files easy.h and curl.h under include, and libcurl.a must be under lib. . After compilation, the files in include exist, but not in the lib directory. It was originally generated in the lib/.libs directory, so you need to copy it to the lib directory

2. Enter the source code directory where the original php was installed,
cd ext
cd curl
phpize
./configure --with-curl=DIR
make
The curl.so file will be generated under PHPDIR/ext/curl/moudles/. U Curl function library

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设置会话参数
参考网址:http://blog.csdn.net/superbirds/article/details/7532277: curl_setopt 会话参数详解
参考网址:http://www.zhibotie.net/space-1-do-blog-id-71.html

curl_version — 获取curl相关的版本信息
curl_init()函数的作用初始化一个curl会话,curl_init()函数唯一的一个参数是可选的,表示一个url地址。
curl_exec()函数的作用是执行一个curl会话,唯一的参数是curl_init()函数返回的句柄。
curl_close()函数的作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。
Copy after login
ordinary grabbing webpage instances
// 1. 初始化
$ch = curl_init();
//$ch = curl_init("http://www.baidu.com/");

// 2. 设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/");//CURLOPT_URL: 这是你想用PHP取回的URL地址。
你也可以在用curl_init()函数初始化时设置这个选项

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设定是否输出页面内容,这里为0的话,后面就不用echo,
就直接把curl抓回来的内容输出;
curl_setopt($ch, CURLOPT_HEADER, 0);//设定是否显示头信息,为1的时候,就会把响应头信息也给一并输出

// 3. 执行并获取HTML文档内容
$output = curl_exec($ch);
//echo $output;//输出抓回来的网页;如果curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0),就不需要此句;

// 4. 释放curl句柄
curl_close($ch);
Copy after login

judge errors and output errors on the basis of capturing. The application of PHP-CLI [CURL] includes various aspects. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!