curl
是使用url语法,进行网络操作的一个命令。比如可以获取一个网站的页面的内容。可以发送get或者post请求等。功能非常强大,支持各种网络协议。比如 http, https, ftp, ftps, imap, pop3 等等。
常用的一些选择
选项 | 含义 |
---|---|
-# | 显示进度条 |
-0 / --http1.0 | 使用 HTTP 1.0 默认使用 1.1 |
-a / --appent | 上传文件,(不存在)添加或者(存在)追加。 |
-A / --user-agent | 指定用户代理。可以使用 -H(--header) 代替 |
-b / --cookie | 发送http请求的时候,带上cookie |
-c / --cookie-jar | 使用文件的方式发送cookie |
--connect-timeout | 设置超时的时间单位是秒 |
-d | 发送post请求指定数据 |
-D / --dump-header | 协议的头部信息写入指定的文件 |
-e / --referer | 指定 referer |
-f | 失败处理,如果失败不显示服务器提供的错误信息,通过 $? 可以判读是否失败 |
-F | curl -F "file=@\"localfile\";filename=\"nameinpost\"" url.com |
-H/--header | 添加header |
-i / --include | 显示头部(header)信息 |
-I | 只返回头部(header)信息 |
-j / --junk-session-cookie | 废弃老的session cookies(服务器可能会更新cooike) |
-o | 输出的结果,默认是终端。写入文件 |
-s / --slient | 安静模式,不输出错误,或者进度条之类的。 |
--stderr file | 错误输出到指定到文件 |
-T / --upload-file | curl -T "{file1,file2}" url.php 文件上传 |
-v / --verbose | 显示http的传输过程 |
-w | 输出一些定义的元数据 curl -s -o /dev/null -w %{http_code}"-"%{content_type}"\n" url |
curl 命令的一些示例
1.下载页面,默认会显示额外的信息类似
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl https://www.shelljiaoben.com
# 使用 -s 选项仅仅显示内容
curl -s https://www.shelljiaoben.com
2. 仅仅显示header信息
curl -s -I https://www.shelljiaoben.com
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 22 Dec 2017 03:10:49 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
3. 输出的内容保存在文件中,终端不输出
curl -s -o /myfile.txt https://www.shelljiaoben.com
4. 仅仅显示http状态代码
# 首先使用 -s -o 终端不输出任何信息, 然后使用 -w
curl -s -o /dev/null -w %{http_code}"\n" http://www.baidu.com
# 如果使用 http 协议,可以看到 301 到https
curl -s -o /dev/null -w %{http_code}" "%{time_total}" "%{redirect_url} http://www.shelljiaoben.com
需要注意最后有个"\n"换行符号,默认没有, 除去http_code,还有 http_code
, http_content
, local_ip
, local_port
, redirect_url
, remote_ip
, remote_port
, size_download
, size_header
, size_upload
, speed_download
, speed_upload
, time_appconnect
, time_connect
, time_namelookup
, time_pertransfer
, time_redirect
, time_starttransfer
, time_total
等
5. 模拟浏览器访问
# 模拟浏览器访问
curl -A "Mozilla/5.0 (Windows NT ...." https://www.shelljiaoben.com
6. "伪造从哪里访问的"
# referer 可以理解成从哪里来的,网站依次访问 a.html, b.html, c.html 。访问b.html的是referer就是a.html, 这样就知道页面从哪里来的。
curl -e "www.shelljiaoben.com" https://www.shelljiaoben.com
7. 自动跳转
# 网站中存在一些跳转比如 301, 默认使用curl是获取不到
curl -I -L http://www.shelljiaoben.com
#输出有两个部分,可以看了自动的跳转
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 22 Dec 2017 03:32:50 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://www.shelljiaoben.com/
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 22 Dec 2017 03:32:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
8. curl显示通信的过程
# 可以查看到发送和接收的http头部信息
curl -I -v http://www.shelljiaoben.com
# 输出的结果
> HEAD / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.shelljiaoben.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently
< Server: nginx
Server: nginx
< Date: Fri, 22 Dec 2017 03:35:06 GMT
Date: Fri, 22 Dec 2017 03:35:06 GMT
< Content-Type: text/html
Content-Type: text/html
< Content-Length: 178
Content-Length: 178
< Connection: keep-alive
Connection: keep-alive
< Location: https://www.shelljiaoben.com/
Location: https://www.shelljiaoben.com/
<
* Connection #0 to host www.shelljiaoben.com left intact
9. 如果想让curl显示更详细的通信情况,可以用 --trace, 详情可以查看 test.out文件
curl --trace test.out http://www.shelljiaoben.com
10. curl 使用HTTP 的动词,默认是 GET, 一共有四种 GET, HEAD, POST PUT
curl -X GET http://www.shelljiaoben.com
11. curl 发送表单
表单的发送有两种方式POST,和 GET。如果用来发送文件就需用POST, GET比较简单直接写在访问地址就可以。发送的文件前面需要有@,格式是@文件名
html 表单
<form method="POST" enctype='multipart/form-data' action="/test.php">
<input type=text name=name>
<input type=file name=file>
<input type=submit name=submit value="subit">
</form>
php 文件 test.php
<?php
var_dump(_GET);
var_dump(_POST);
var_dump($_FILES);
测试的命令
## get请求,注意 &前面有个 \
curl /test.php?a=1\&b=2
curl -X GET /test.php?a=1\&b=2
## post 请求, 可以发送数组
curl -X POST --data "name=myname" /test.php
curl -X POST --data "name=myname&age[]=100&age[]=200" /test.php
## post 直接上传文件
curl --form upload=@/etc/passwd --form name=100 /test.php?a=1\&b=2
输出
# _GET
Array
(
[a] => 1
[b] => 2
)
# _POST
Array
(
[name] => 100
)
# _FILES
Array
(
[upload] => Array
(
[name] => passwd
[type] => application/octet-stream
[tmp_name] => /tmp/phpyMGCCV
[error] => 0
[size] => 1313
)
)
12. curl 发送 cookie
cookie 的发送可以通过字符串的方式,也可以通过文件的方式,通过文件的方式,功能是比较强大的,可以保存服务器发送的cookie, 保存的文件中。发送给服务器的时候同样指定这个文件,这样就可以模拟浏览器的工作方式了。
# 简单的通过字符串来方式来进行数据的发送
curl --cookie "webname=shelljiaoben" /test.php
curl 使用文件的方式发送和接收 cookie,
模拟测试的 test.php文件
<?php
if(!isset(_COOKIE['test'])) {
setcookie("test", 1);
echo "设置 cookie, test = 1";
} else {n = _COOKIE['test'] + 1;
setcookie('test',n);
}
echo $_COOKIE['test'];
使用curl 访问 。 参数 -c
保存服务器端发送的cookie, 参数-b
向服务器发送cookie使用的文件, 两个文件设置成统一个文件。
curl -c cookie.txt -b cookie.txt /test.php
# 第一次返回结果 “设置 cookie, test = 1”
# 第二次返回结果 1
# 第三次返回结果 2
查看 cookie.txt的内容
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
www.shelljiaoben.com FALSE / FALSE 0 test 4
13. 增加http的信息头
curl --header "add-header:value"