什么是CURL
curl是利用URL語法在命令行方式下工作的開源文件傳輸工具.它被廣泛應用在Unix\多種Linux發(fā)行版中,并且有DOS和Win32\Win64下的移植版本.
常見用法
1.獲取頁面內(nèi)容
root@amu:/Data/apps# curl 127.0.0.1/login.php
<!DOCTYPE html>
<html>
<head>
<title>login</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
</head>
<body>
歡迎注冊募舟,請輸入用戶名和密碼
<form method="post" action="register.php">
<p>姓名:<input type="text" name="name" size="10"></p>
<p>密碼:<input type="password" name="password" size="10"></p>
<p>
<input type="submit" value="確定">
<input type="reset" value="取消">
</p>
</form>
<a >微信連Wi-Fi</a>
</body>
</html>
當我們不加任何選修使用curl
時,默認會發(fā)送GET
請求來獲取鏈接內(nèi)容到標準輸出
2.顯示HTTP頭
root@amu:/Data/apps# curl -I 127.0.0.1
HTTP/1.1 302 Found
Server: nginx/1.13.8
Date: Thu, 10 May 2018 03:47:56 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.1.13
Location: http://127.0.0.1/login.php
也可以同時顯示HTTP
頭和文件內(nèi)容,使用-i
選項
curl -i 127.0.0.1
3.將內(nèi)容保存到文件
root@amu:/Data/apps# curl -o index.html 127.0.0.1
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 969 0 969 0 0 429k 0 --:--:-- --:--:-- --:--:-- 473k
root@amu:/Data/apps# ls
index.html nginx1.13.8 openresty php7.1.13
root@amu:/Data/apps# curl -O 127.0.0.1/index.php
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 969 0 969 0 0 441k 0 --:--:-- --:--:-- --:--:-- 473k
root@amu:/Data/apps# ls
index.html index.php nginx1.13.8 openresty php7.1.13
兩種用法:-o 和 -O
-o:結(jié)果會被保存到命令行中提供的文件名
-O:URL中的文件名會被用作保存輸出的文件名
4.同時下載多個文件
curl -O 127.0.0.1/index.php -O 127.0.0.1/login.php
當然用-o
也可以
5.使用 -L
跟蹤重定向
從第二個例子中我們可以看到網(wǎng)站給我們返回的狀態(tài)碼是302,說明網(wǎng)站跳轉(zhuǎn)了,在這種情況下就無法獲取我們想要的網(wǎng)頁內(nèi)容,這是我們就可以用-L
選項了.
root@amu:/Data/apps# curl -L 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>login</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
</head>
<body>
歡迎注冊蹋宦,請輸入用戶名和密碼
<form method="post" action="register.php">
<p>姓名:<input type="text" name="name" size="10"></p>
<p>密碼:<input type="password" name="password" size="10"></p>
<p>
<input type="submit" value="確定">
<input type="reset" value="取消">
</p>
</form>
<a >微信連Wi-Fi</a>
</body>
</html>
6.使用 -A 自定義 USER-Agent
curl --user-agent "[User Agent]" [URL]
curl --user-agent "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7" www.baidu.com
7.使用 -H 自定義 header
curl -H 'Host: 192.168.199.4' -H 'Accept-Language:es' http://127.0.0.1
8.使用 Cookie
#保存http的responses里面的Cookie信息
curl -c cookiec.txt http://www.baidu.com
#保存http的response里面的header信息
curl -D cookied.txt http://www.baidu.com
#使用cookie
curl -b cookiec.txt http://www.baidu.com
9.傳遞數(shù)據(jù)
http協(xié)議有各種請求的方法,如:GET\POST\HEADD\PUT等.這些方法curl都支持.使用-X
指定請求方法,沒有指定的情況下,默認使用GET
請求方法.POST
和PUT
方法提交的數(shù)據(jù)用-d
參數(shù)指定
//GET方法
curl http://127.0.0.1?age=2
//HEAD方法,獲取響應的頭部信息
curl -I http://127.0.0.1
// POST 方法
curl -X POST http://127.0.0.1 -d "name=admin&pwd=123"
// PUT 方法
curl -X PUT http://127.0.0.1 -d '{"name":"admin"}'
10.查看詳細請求和響應過程
root@amu:/Data/apps# curl -v 127.0.0.1
* Rebuilt URL to: 127.0.0.1/
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 302 Found
< Server: nginx/1.13.8
< Date: Thu, 10 May 2018 04:41:13 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/7.1.13
< Location: http://127.0.0.1/login.php
<
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: HTTP_REFERER in /Data/apps/nginx1.13.8/html/index.php on line <i>66</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0002</td><td bgcolor='#eeeeec' align='right'>373888</td><td bgcolor='#eeeeec'>{main}( )</td><td title='/Data/apps/nginx1.13.8/html/index.php' bgcolor='#eeeeec'>.../index.php<b>:</b>0</td></tr>
</table></font>
* Connection #0 to host 127.0.0.1 left intact
11.查看請求或響應的具體某一指標
root@amu:/Data/apps# curl -s -w "%{time_total}\n%{size_download}\n" https://github.com -o /dev/null
7.514
54697
root@amu:/Data/apps# curl -w %{http_code} https://github.com -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 54697 0 54697 0 0 11389 0 --:--:-- 0:00:04 --:--:-- 13515
12.完成認證
curl --basic --user 'admin:123' http://example.com/
13文件上傳
curl --form upload=@localfilename --form press=OK [URL]
curl http://api.example.com/profile -F "image=@profile.jpg"
curl http://api.example.com/profile -F "image=@profile.jpg" -F "phone=123456789"
其他一些用法
curl --trace output.txt www.sina.com
curl --referer http://www.example.com http://www.example.com
歡迎關注個人微信公眾號: