cURL命令使用指南


cURL是什么

curl是Linux命令行工具,可以使用任何可支持的協議(如HTTP、FTP、IMAP、POP3、SCP、SFTP、SMTP、TFTP、TELNET、LDAP或FILE)在服務器之間傳輸數據。

在Linux下,curl是由libcurl 提供驅動封裝的cli客戶端,在libcurl驅動下,curl可以一次傳輸多個文件。而PHP中的cURL函數,也是基於libcurl驅動的。

curl由libcurl支持所有與傳輸相關的特性

cURL常用參數

參數 說明
-i 默認隱藏響應頭,此選項打印響應頭與
-I/--head 僅顯示響應頭
-o 將相應內容保存指定路徑下
-O 將相應內容保存在當前工作目錄下
-C 斷點續傳,在 crtl + c終端后,可以從中斷后部分開始
-v 顯示請求頭與響應頭
-x 使用代理
-X 指定請求方法,POST GET PUT DELETE等
-d 如GET/POST/PUT/DELETE 需要傳的表單參數,如JSON格式
-u username:password 當使用ftp有用戶名可以使用-u,ftp允許匿名用戶訪問可以忽略
–-limit-rate 2000B 限速
-T/--upload-file <file> 上傳一個文件
-c/--cookie-jar <file name> 將cookie下載到文件內
-k/--insecure 允許執行不安全的ssl連接,即調過SSL檢測
--header 'Host: targetapplication.com' 使用請求頭
-L/--location 接受服務端redirect的請求
-F 上傳二進制文件

下面整理了一些常用語法使用格式

cURL使用案例

限制下載速率

curl --limit-rate 100K http://yourdomain.com/yourfile.tar.gz -O

使用代理訪問

curl --proxy yourproxy:port https://yoururl.com

限速訪問

curl www.baidu.com  --limit-rate 1k

存儲cookie和使用cookie

[root@VM-0-2-centos ~]# curl --cookie-jar cnncookies.txt https://www.baidu.com/index.html -O -s -v
* About to connect() to www.baidu.com port 443 (#0)
*   Trying 14.215.177.39...
* Connected to www.baidu.com (14.215.177.39) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
*       start date: Apr 02 07:04:58 2020 GMT
*       expire date: Jul 26 05:31:02 2021 GMT
*       common name: baidu.com
*       issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
> GET /index.html HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Wed, 26 May 2021 12:14:41 GMT
< Etag: "58860402-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:18 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
* Added cookie BDORZ="27315" for domain baidu.com, path /, expire 1622117681
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< 
{ [data not shown]
* Connection #0 to host www.baidu.com left intact
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

.baidu.com      TRUE    /       FALSE   1622117681      BDORZ   27315
[root@VM-0-2-centos ~]# curl --cookie cnncookies.txt https://www.baidu.com -s -v -o /dev/null
* About to connect() to www.baidu.com port 443 (#0)
*   Trying 14.215.177.39...
* Connected to www.baidu.com (14.215.177.39) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
*       start date: Apr 02 07:04:58 2020 GMT
*       expire date: Jul 26 05:31:02 2021 GMT
*       common name: baidu.com
*       issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
> Cookie: BDORZ=27315
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Wed, 26 May 2021 12:23:27 GMT
< Etag: "58860402-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:18 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
* Replaced cookie BDORZ="27315" for domain baidu.com, path /, expire 1622118207
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/  # 這里可以看到設置的cookie
< 
{ [data not shown]
* Connection #0 to host www.baidu.com left intact

使用代理

curl -x socks5://127.0.0.1:10808 https://www.google.com

使用application/x-www-form-urlencoded表單類型

這里使用的為application/x-www-form-urlencoded

curl -d "option=value&something=anothervalue" -X POST https://{hostname}/

使用json格式作為body

curl  -H "Content-Type: application/json" -X POST https://host.com/ \
	-d '
	{
		"option": "value", 
		"something": "anothervalue"
	}'

使用curl 上傳文件

curl {host}/api/v1/upimg -F "file=@/Users/fungleo/Downloads/401.png" \
	-H "token: 222" \
	-v

也可以指定MIME類型。如:

curl -F 'file=@photo.png;type=image/png' https://{host}/api/v1/upimg

curl輸出的格式變量

curl -w參數提供了一些格式變量,可以達到緊緊獲取某些數據

僅獲取http狀態碼

curl -w %{http_code} www.baidu.com -o /dev/null -s

獲取整個請求的時間

獲取整個請求的耗時,單位秒,顯示單位 毫秒

curl -w %{time_total} www.baidu.com -o /dev/null -s

獲取域名解析時間

curl -w %{time_namelookup} www.baidu.com -o /dev/null -s

獲取TCP連接耗時

curl -w %{time_connect} www.baidu.com -o /dev/null -s

獲取SSL/SSH握手到遠程主機耗時

curl -w %{time_appconnect} https://www.baidu.com -o /dev/null -s -v

獲取所有重定向的耗時

這里是從查找、連接、傳輸整個事務的完成到開始傳送數據之前的耗時

curl -w %{time_redirect} www.baidu.com -o /dev/null -s

獲得下載的總字節數

這里是http相應的body長度,而不是加上頭部的大小

curl -w %{size_download} www.baidu.com -o /dev/null -s
[root@VM-0-2-centos ~]# curl -w %{size_download} www.baidu.com -o /dev/null -s
2381
[root@VM-0-2-centos ~]# cwww.baidu.com -s|wc l -s
      2     159    2381

獲得請求體送字節數

curl -w %{size_request} www.baidu.com -o /dev/null -s

獲得傳輸中的連接數

curl -w %{num_connects} www.baidu.com -o /dev/null -s

獲得重定向次數

curl -w %{num_redirects} www.360buy.com -o /dev/null -s  -L

獲得SSL驗證結果

0 表示是成功的

curl -w %{ssl_verify_result} https://www.baidu.com -o /dev/null -s -L

獲得重定向的地址

當沒有指定-L時,會返回被重定向后的地址

curl -w %{redirect_url} https://www.360buy.com -o /dev/null -s 

獲得上傳和下載速度

curl -w %{speed_download} https://www.360buy.com -o /dev/null -s
curl -w %{speed_upload} https://www.360buy.com -o /dev/null -s

根據自己需要拼接特定格式

curl -w "總共請求時長:%{time_total}\n總跳轉次數:%{num_redirects}\n" \
www.360buy.com \
-o /dev/null -s  \
-L

總共請求時長:1.338
總跳轉次數:3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM