man curl

NAME

curl - 一个利用URL语法在命令行下工作的网络工具

用法

curl [options] [URL...]

描述
curl是一种使用支持的协议(DICT、FILE、FTP、FTPS、GOPHER、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAP、POP3、POP3、RTMP、RTSP、SCP、SFTP、SMB、SMB、SMTP、SMTPS、TELNET和TFTP)与服务端交互的工具。该命令通常设计为在没有用户交互的情况下工作。

curl提供了大量有用的技巧,比如代理支持、用户身份验证、FTP上传、httppost、SSL连接、cookies、文件传输恢复、Metalink等等。

正如你下面将看到的,大量的功能将使用眼花缭乱。
curl由libcurl支持所有与传输相关的特性。有关详细信息,请参见libcurl(3)。

URL

URL语法依赖于协议。您可以在RFC3986中找到详细的描述。
可以通过在大括号中编写部分集来指定多个URL或URL的一部分,如中所示:
http://site.{one,two,three}.com
或者可以使用[]获得字母数字序列,如:

ftp://ftp.example.com/file[1-100].txt

ftp://ftp.example.com/file[001-100].txt    (with leading zeros)

ftp://ftp.example.com/file[a-z].txt

不支持嵌套序列,但可以使用相邻的多个序列:

http://example.com/archive[1996-1999]/vol[1-4]/part{a,b,c}.html

您可以在命令行上指定任意数量的URL。它们将按指定的顺序依次获取。

您可以为要每n个数字或字母获取的范围指定步进计数器:

http://example.com/file[1-100:10].txt

http://example.com/file[a-z:2].txt

当从命令行提示符调用时使用[]或{}序列时,您可能必须将完整的URL放在双引号内,以避免shell干扰

同样。这也适用于其他特殊字符,例如“&”、“?”和“*”。
在URL中为IPv6区域索引提供转义百分号和接口名称。就像在

http://[fe80::3%25eth0]/

如果您指定的URL没有protocol://前缀,curl将尝试猜测您可能需要的协议。然后它会默认为HTTP,但会尝试基于HTTP的其他协议-

使用了主机名前缀。例如,对于以“ftp”开头的主机名,curl将假定您想说ftp。

curl将尽最大努力使用您传递给它的内容作为URL。它并没有试图通过任何方式验证它是一个语法正确的URL,而是非常自由的使用什么

它接受。

curl将尝试为多个文件传输重用连接,这样从同一服务器获取多个文件就不会进行多次连接/握手。这个

提高速度。当然,这只在单个命令行上指定的文件上完成,不能在单独的curl调用之间使用。

进度表

curl通常在操作期间显示进度表,指示传输的数据量、传输速度和估计剩余时间等播放字节数,速度以字节/秒为单位。后缀(k,M,G,T,P)基于1024。例如,1k是1024字节。1M是1048576字节。

默认情况下,curl将此数据显示给终端,因此如果您调用curl执行操作,并且它将要向终端写入数据,那么它将禁用进度表,否则会弄乱输出混合进度表和响应数据。

如果您想要HTTP POST或PUT请求的进度表,则需要使用shell redirect(>)、-o、-output或类似方法将响应输出重定向到文件。

这与FTP上传的情况不同,因为该操作不会向终端输出任何响应数据。

如果你喜欢进度“条”而不是普通的仪表,#,–进度条是你的朋友。您还可以使用-s、-silent完全禁用进度表选项。

OPTIONS

选项以一个或两个破折号开头。许多选项都需要旁边的附加值。

例如,选项-d的短“单划线”形式可以用于或不包含其与其值之间的空格,尽管空间是建议的分隔符。例如,长“双划线”形式-d,–data 需要在其值之间留出空间。
短版本选项可以不需要任何附加值的立即相互使用,例如,
-O, -L 和 -v 或者 -OLv.

一般来说,所有布尔选项都使用–option启用,但再次禁用–no选项。也就是说,您使用完全相同的选项名称,但在其前缀为“no-”。

然而,在这个列表中,我们主要只列出并显示它们的–option版本(7.19.0中没有添加任何选项的概念。以前大多数选项都被切换重复使用同一命令行选项时打开/关闭。)

-A

-A参数指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]。

$ curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com

上面命令将User-Agent改成 Chrome 浏览器。

$ curl -A '' https://google.com

上面命令会移除User-Agent标头。

也可以通过-H参数直接指定标头,更改User-Agent。

$ curl -H 'User-Agent: php/1.0' https://google.com

-b

-b参数用来向服务器发送 Cookie。

$ curl -b 'foo=bar' https://google.com

上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。

$ curl -b 'foo1=bar;foo2=bar2' https://google.com

上面命令发送两个 Cookie。

$ curl -b cookies.txt https://www.google.com

上面命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。

-c

-c参数将服务器设置的 Cookie 写入一个文件。

$ curl -c cookies.txt https://www.google.com

上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。

-d

-d参数用于发送 POST 请求的数据体。

$ curl -d'login=emma&password=123'-X POST https://google.com/login

或者

$ curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login

使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST。

-d参数可以读取本地文本文件的数据,向服务器发送。

$ curl -d '@data.txt' https://google.com/login

上面命令读取data.txt文件的内容,作为数据体向服务器发送。

–data-urlencode

–data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。

$ curl --data-urlencode 'comment=hello world' https://google.com/login

上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。

-e

-e参数用来设置 HTTP 的标头Referer,表示请求的来源。

curl -e 'https://google.com?q=example' https://www.example.com

上面命令将Referer标头设为https://google.com?q=example。

-H参数可以通过直接添加标头Referer,达到同样效果。

curl -H 'Referer: https://google.com?q=example' https://www.example.com

-F

-F参数用来向服务器上传二进制文件。

$ curl -F 'file=@photo.png' https://google.com/profile

上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。

-F参数可以指定 MIME 类型。

$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile

上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。

-F参数也可以指定文件名。

$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile

上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。

-G

-G参数用来构造 URL 的查询字符串。

$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

上面命令会发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略–G,会发出一个 POST 请求。

如果数据需要 URL 编码,可以结合–data–urlencode参数。

$ curl -G --data-urlencode 'comment=hello world' https://www.example.com

-H

-H参数添加 HTTP 请求的标头。

$ curl -H 'Accept-Language: en-US' https://google.com

上面命令添加 HTTP 标头Accept-Language: en-US。

$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com

上面命令添加两个 HTTP 标头。

$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

上面命令添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。

-i

-i参数打印出服务器回应的 HTTP 标头。

$ curl -i https://www.example.com

上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。

-I

-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。

$ curl -I https://www.example.com

上面命令输出服务器对 HEAD 请求的回应。

–head参数等同于-I。

$ curl --head https://www.example.com

-k

-k参数指定跳过 SSL 检测。

$ curl -k https://www.example.com

上面命令不会检查服务器的 SSL 证书是否正确。

-L

-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。

$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet

–limit-rate
–limit-rate用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。

$ curl --limit-rate 200k https://google.com

上面命令将带宽限制在每秒 200K 字节。

-o

-o参数将服务器的回应保存成文件,等同于wget命令。

$ curl -o example.html https://www.example.com

上面命令将www.example.com保存成example.html。

-O

-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。

$ curl -O https://www.example.com/foo/bar.html

上面命令将服务器回应保存成文件,文件名为bar.html。

-s

-s参数将不输出错误和进度信息。

$ curl -s https://www.example.com

上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。

如果想让 curl 不产生任何输出,可以使用下面的命令。

$ curl -s -o /dev/null https://google.com

-S

-S参数指定只输出错误信息,通常与-s一起使用。

$ curl -s -o /dev/null https://google.com

上面命令没有任何输出,除非发生错误。

-u

-u参数用来设置服务器认证的用户名和密码。

$ curl -u 'bob:12345' https://google.com/login

上面命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。

curl 能够识别 URL 里面的用户名和密码。

$ curl https://bob:12345@google.com/login

上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。

$ curl -u 'bob' https://google.com/login

上面命令只设置了用户名,执行后,curl 会提示用户输入密码。

-v

-v参数输出通信的整个过程,用于调试。

$ curl -v https://www.example.com

–trace参数也可以用于调试,还会输出原始的二进制数据。

$ curl --trace - https://www.example.com

-x

-x参数指定 HTTP 请求的代理。

$ curl -x socks5://james:cats@myproxy.com:8080 
https://www.example.com

上面命令指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出。

如果没有指定代理协议,默认为 HTTP。

$ curl -x james:cats@myproxy.com:8080 https://www.example.com

上面命令中,请求的代理使用 HTTP 协议。

-X

-X参数指定 HTTP 请求的方法。

$ curl -X POST https://www.example.com

上面命令对https://www.example.com发出 POST 请求

-w

使curl在stdout上显示网络信息,支持变量。

curl -w "%{http_code}\n"  www.baidu.com

可用变量如下:

  1. content_type The Content-Type of the requested document, if there was any.
  2. url_effective 最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形。
  3. http_code http状态码,如200成功,301转向,404未找到,500服务器错误等。(The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.)
  4. http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)
  5. time_total 总时间,按秒计。精确到小数点后三位。 (The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)
  6. time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间。(The time, in seconds, it took from the start until the name resolving was completed.)
  7. time_connect 连接时间,从开始到建立TCP连接完成所用时间,包括前边DNS解析时间,如果需要单纯的得到连接时间,用这个time_connect时间减去前边time_namelookup时间。以下同理,不再赘述。(The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.)
  8. time_appconnect 连接建立完成时间,如SSL/SSH等建立连接或者完成三次握手时间。(The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0))
  9. time_pretransfer 从开始到准备传输的时间。(The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.)
  10. time_redirect 重定向时间,包括到最后一次传输前的几次重定向的DNS解析,连接,预传输,传输时间。(The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3))
  11. time_starttransfer 开始传输时间。在发出请求之后,Web 服务器返回数据的第一个字节所用的时间(The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.)
  12. size_download 下载大小。(The total amount of bytes that were downloaded.)
  13. size_upload 上传大小。(The total amount of bytes that were uploaded.)
  14. size_header 下载的header的大小(The total amount of bytes of the downloaded headers.)
  15. size_request 请求的大小。(The total amount of bytes that were sent in the HTTP request.)
  16. speed_download 下载速度,单位-字节每秒。(The average download speed that curl measured for the complete download. Bytes per second.)
  17. speed_upload 上传速度,单位-字节每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)
  18. content_type 就是content-Type,不用多说了,这是一个访问我博客首页返回的结果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)
  19. num_connects 最近的的一次传输中创建的连接数目。Number of new connects made in the recent transfer. (Added in 7.12.3)
  20. num_redirects 在请求中跳转的次数。Number of redirects that were followed in the request. (Added in 7.12.3)
    redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)
  21. ftp_entry_path 当连接到远程的ftp服务器时的初始路径。The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)
  22. ssl_verify_result ssl认证结果,返回0表示认证成功。( The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值