curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。作用是发出网络请求,然后得到和提取数据,显示在”标准输出”(stdout)上面。

功能

这里主要介绍常见的使用参数。

查看网页源码=O|o|

1
2
3
curl www.sina.com
# curl -o [文件名] www.sina.com 会保存显示的内容保存下来,相当于使用wget命令了
# curl -O www.sina.com # 不需要指定文件名,将 URL 的最后部分当作文件名

返回

1
2
3
4
5
6
7
  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
  </body></html>

自动跳转=L

1
2
3
curl -L www.sina.com
# 对于自动跳转的网址,使用L参数,curl会自动跳转到新的网址。
# -> 结果就自动跳转为www.sina.com.cn

显示头信息=i

1
2
3
4
curl -i www.sina.com
# 可以显示http response的头信息,连同网页代码一起
curl -I www.sina.com
# 只显示http response的头信息

显示通信过程=v

1
2
3
4
5
curl -v www.sina.com
# 显示一次http通信的整个过程,包括端口连接和http request头信息。
# 也可以使用以下的方式获得更全的内容
curl --trace output.txt www.sina.com
curl --trace-ascii output.txt www.sina.com

返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  * About to connect() to www.sina.com port 80 (#0)
  * Trying 61.172.201.195... connected
  * Connected to www.sina.com (61.172.201.195) port 80 (#0)
  > GET / HTTP/1.1
  > User-Agent: curl/7.21.3 (i686-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
  > Host: www.sina.com
  > Accept: */*
  >
  * HTTP 1.0, assume close after body
  < HTTP/1.0 301 Moved Permanently
  < Date: Sun, 04 Sep 2011 00:42:39 GMT
  < Server: Apache/2.0.54 (Unix)
  < Location: http://www.sina.com.cn/
  < Cache-Control: max-age=3600
  < Expires: Sun, 04 Sep 2011 01:42:39 GMT
  < Vary: Accept-Encoding
  < Content-Length: 231
  < Content-Type: text/html; charset=iso-8859-1
  < X-Cache: MISS from sh201-19.sina.com.cn
  < Connection: close
  <
  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
  </body></html>
  * Closing connection #0

发送表单信息

1
2
3
4
5
6
# 发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。
curl example.com/form.cgi?data=xxx
# POST方法必须把数据和网址分开,curl就要用到--data参数。
curl -X POST --data "data=xxx" example.com/form.cgi
# 如果你的数据没有经过表单编码,还可以让curl为你编码,参数是`--data-urlencode`
curl -X POST--data-urlencode "date=April 1" example.com/form.cgi

HTTP动词=X

1
2
3
# curl默认的HTTP动词是GET,使用`-X`参数可以支持其他动词。
curl -X POST www.example.com
curl -X DELETE www.example.com

文件上传

假定文件上传的表单是下面这样:

1
2
3
4
5
6
  <form method="POST" enctype='multipart/form-data' action="upload.cgi">
    <input type=file name=upload>
    <input type=submit name=press value="OK">
  </form>
# 你可以用curl这样上传文件
curl --form upload=@localfilename --form press=OK [URL]

Referer字段

1
2
# 有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的
curl --referer http://www.example.com http://www.example.com

User Agent字段=A

1
2
3
4
5
6
7
8
9
# iPhone4的User Agent
  Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
curl --user-agent "[User Agent]" [URL]
# 通过 A 将User-Agent改成 Chrome 浏览器
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标头
curl -A '' https://google.com
# 直接指定标头,更改User-Agent
curl -H 'User-Agent: php/1.0' https://google.com
1
2
3
4
5
6
7
8
9
10
11
12
# 具体的cookie的值,可以从http response头信息的`Set-Cookie`字段中得到
curl --cookie "name=xxx" www.example.com
# 将服务器设置的 Cookie 写入一个文件,将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt
curl -c cookies.txt https://www.google.com

# 发送 POST 请求的数据体。HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X 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参数可以读取本地文本文件的数据,向服务器发送。读取data.txt文件的内容,作为数据体向服务器发送。
curl -d '@data.txt' https://google.com/login

增加头信息

1
2
# 有时需要在http request之中,自行增加一个头信息。`--header`参数就可以起到这个作用。
curl --header "Content-Type:application/json" http://example.com

HTTP认证

1
2
# 有些网域需要HTTP认证,这时curl需要用到`--user`参数。
curl --user name:password example.com

错误和进度信息=s|S

1
2
3
4
5
6
7
# 不输出错误和进度信息,不发生错误的话,会正常显示运行结果
curl -s https://www.example.com
# 如果想让 curl 不产生任何输出,可以使用下面的命令
curl -s -o /dev/null https://google.com

# 只输出错误信息,通常与-s一起使用。没有任何输出,除非发生错误
curl -s -o /dev/null https://google.com

用户名和密码=u

用来设置服务器认证的用户名和密码

1
2
3
4
5
6
7
8
# 设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。curl 能够识别 URL 里面的用户名和密码。
curl -u 'bob:12345' https://google.com/login

# 能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头
curl https://bob:[email protected]/login

# 只设置了用户名,执行后,curl 会提示用户输入密码
curl -u 'bob' https://google.com/login

参考

阮一峰