CURL用法

cURL簡介

cURL的官方定義爲:curl is a command line tool for transferring data with URL syntax,即使用URL語法規則來傳輸數據的命令行工具

cURL是一個命令行工具,作用是發出網絡請求,然後得到和提取數據,顯示在“標準輸出”(stdout)上面。

簡單的GET請求

當我們使用curl請求不帶任何參數的時候,默認就是發送GET請求。

curl http://www.example.com

上面命令向www.example.com發出 GET 請求,服務器返回的內容會在命令行輸出。

示例:

curl http://www.master.com/index.html
<!DOCTYPE html>
<html>
<head>
<title>CURL</title>
<style>
    
</style>
</head>
<body>
    <p>Learn CURL!</p>
</body>
</html>

如果想把這個網頁保存下來,可以使用-o參數,相當於使用wget命令:

curl -o [文件名] http://www.example.com

-X參數:指定請求方法

-X參數指定HTTP請求的方法。

curl -X [GET/POST/PUT/DELETE/HEAD/OPTIONS/TRACE/CONNECT] www.example.com

-d參數:指定POST請求的數據體

-d參數用於發送 POST 請求的數據體。

curl -X POST www.example.com -d 'user=itbsl&password=123456'
# 或
curl -X POST www.example.com -d 'user=itbsl' -d 'password=123456'

使用-d參數以後,HTTP 請求會自動加上標頭Content-Type : application/x-www-form-urlencoded。並且會自動將請求轉爲 POST 方法,因此可以省略-X POST

-d參數可以讀取本地文本文件的數據,向服務器發送。

curl -d '@data.txt' www.example.com

--data-urlencode:編碼後的-d

--data-urlencode參數等同於-d,發送 POST 請求的數據體,區別在於會自動將發送的數據進行 URL 編碼。

curl -X POST www.example.com --data-urlencode "user=hello world"

上面代碼中,發送的數據hello world之間有一個空格,需要進行 URL 編碼。

注意:如果使用--data-urlencode的方式指定了多個參數,可能會出現下面的情況

curl -X POST www.master.com --data-urlencode 'user=hello world&password=123456'
<pre>array(1) {
  ["user"] =&gt; string(27) "hello world&amp;password=123456"
}

-G參數:構造URL查詢字符串

-G參數用來構造 URL 的查詢字符串。

curl -G www.example.com -d 'user=itbsl' -d 'password=123456'

上面命令會發出一個 GET 請求,實際請求的 URL 爲https://example.com?user=itbsl&password=123456。如果省略-G,會發出一個 POST 請求。

如果數據需要 URL 編碼,可以結合--data--urlencode參數。

-H參數:添加HTTP請求標頭

-H參數添加HTTP請求的標頭。

curl -H 'User-Agent: itbsl/1.0' www.example.com

下面添加兩個標頭

curl -H 'User-Agent: itbsl/1.0' -H 'Sign: xxyyzz' www.example.com

下面命令添加 HTTP 請求的標頭是Content-Type: application/json,然後用-d參數發送 JSON 數據。

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

-L參數:自動跳轉

有的網址是自動跳轉的。使用-L參數,curl就會跳轉到新的網址。

curl -L www.example.com

示例:

www.master.com下的index.php增加重定向代碼

header('Location: http://www.slave.com/index.html');

當我們在終端執行curl -L www.master.com命令時,展示的是www.slave.com/index.html頁面的代碼

curl -L www.master.com
<!DOCTYPE html>
<html>
<head>
<title>CURL</title>
<style>
</style>
</head>
<body>
    <p>Learn CURL!</p>
</body>
</html>

-i參數:顯示頭信息

-i參數可以顯示http response的頭信息,聯通網頁代碼一起。

curl -i www.example.com

示例:

curl -i www.master.com/index.html
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 20 Jun 2021 08:09:44 GMT
Content-Type: text/html
Content-Length: 126
Last-Modified: Sat, 19 Jun 2021 17:24:32 GMT
Connection: keep-alive
ETag: "60ce2850-7e"
Accept-Ranges: bytes

<!DOCTYPE html>
<html>
<head>
<title>CURL</title>
<style>
</style>
</head>
<body>
    <p>Learn CURL!</p>
</body>
</html>

-I參數則是隻顯示http response的頭信息。--head參數等同於-I

-A:指定User-Agent

-A參數指定客戶端的用戶代理標頭,即User-Agent。curl 的默認用戶代理字符串是curl/[version]

curl -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36' www.example.com

上面命令將User-Agent改成 Chrome 瀏覽器。

curl -A '' www.master.com

上面命令會移除User-Agent標頭。

也可以通過--user-agent指定User-Agent參數。

也可以通過-H參數直接修改指定標頭,更改User-Agent

curl -H 'User-Agent: itbsl/1.0' http://www.master.com

-k參數:跳過SSL監測

-k參數指定跳過 SSL 檢測。

curl -k www.example.com

--limit-rate:限制請求和響應帶寬

--limit-rate用來限制 HTTP 請求和迴應的帶寬,模擬慢網速的環境。

curl --limit-rate 200k www.example.com

-v參數:調試

-v參數輸出通信的整個過程,用於調試。

curl -v www.example.com

--trace參數也可以用於調試,還會輸出原始的二進制數據。

curl --trace - www.example.com

-x參數:指定請求代理

-x參數指定 HTTP 請求的代理。

curl -x socks5://james:[email protected]:8080 https://www.example.com

上面命令指定 HTTP 請求通過myproxy.com:8080的 socks5 代理髮出。

如果沒有指定代理協議,默認爲 HTTP。

curl -x james:[email protected]:8080 https://www.example.com

上面命令中,請求的代理使用 HTTP 協議。

-o參數:保存文件

-o參數將服務器的迴應保存成文件,等同於wget命令。

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

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

-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參數指定只輸出錯誤信息,通常與-o一起使用。

curl -S -o 'error.txt' www.example.com

-u參數:用戶設置服務器認證的用戶名和密碼

-u參數用來設置服務器認證的用戶名和密碼。

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

上面命令設置用戶名爲bob,密碼爲12345,然後將其轉爲 HTTP 標頭Authorization: Basic Ym9iOjEyMzQ1

curl 能夠識別 URL 裏面的用戶名和密碼。

curl https://bob:[email protected]/login

上面命令能夠識別 URL 裏面的用戶名和密碼,將其轉爲上個例子裏面的 HTTP 標頭。

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

上面命令只設置了用戶名,執行後,curl 會提示用戶輸入密碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章