linux命令curl用法詳解

原文鏈接:https://blog.csdn.net/binglong_world/article/details/80755193

在Linux中curl是一個利用URL規則在命令行下工作的文件傳輸工具,可以說是一款很強大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具,但按傳統,習慣稱url爲下載工具。

語法:# curl [option] [url]

常見參數:

-A/--user-agent <string> 設置用戶代理髮送給服務器

-b/--cookie <name=string/file>    cookie字符串或文件讀取位置
-c/--cookie-jar <file>                    操作結束後把cookie寫入到這個文件中
-C/--continue-at <offset>            斷點續轉
-D/--dump-header <file>              把header信息寫入到該文件中
-e/--referer                                  來源網址
-f/--fail                                          連接失敗時不顯示http錯誤
-o/--output                                  把輸出寫到該文件中
-O/--remote-name                      把輸出寫到該文件中,保留遠程文件的文件名
-r/--range <range>                      檢索來自HTTP/1.1或FTP服務器字節範圍
-s/--silent                                    靜音模式。不輸出任何東西
-T/--upload-file <file>                  上傳文件
-u/--user <user[:password]>      設置服務器的用戶和密碼
-w/--write-out [format]                什麼輸出完成後
-x/--proxy <host[:port]>              在給定的端口上使用HTTP代理
-#/--progress-bar                        進度條顯示當前的傳送狀態

 

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

它支持多種協議,下面舉例講解如何將它用於網站開發。

一、查看網頁源碼

直接在curl命令後加上網址,就可以看到網頁源碼。我們以網址www.sina.com爲例(選擇該網址,主要因爲它的網頁代碼較短):

  $ curl www.sina.com

  <!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>

如果要把這個網頁保存下來,可以使用`-o`參數,這就相當於使用wget命令了。

  $ curl -o [文件名] www.sina.com

二、自動跳轉

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

  $ curl -L www.sina.com

鍵入上面的命令,結果就自動跳轉爲www.sina.com.cn。

三、顯示頭信息

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

  $ curl -i www.sina.com

  HTTP/1.0 301 Moved Permanently
  Date: Sat, 03 Sep 2011 23:44:10 GMT
  Server: Apache/2.0.54 (Unix)
  Location: http://www.sina.com.cn/
  Cache-Control: max-age=3600
  Expires: Sun, 04 Sep 2011 00:44:10 GMT
  Vary: Accept-Encoding
  Content-Length: 231
  Content-Type: text/html; charset=iso-8859-1
  Age: 3239
  X-Cache: HIT from sh201-9.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>

`-I`參數則是隻顯示http response的頭信息。

四、顯示通信過程

`-v`參數可以顯示一次http通信的整個過程,包括端口連接和http request頭信息。

  $ curl -v www.sina.com

  * 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

如果你覺得上面的信息還不夠,那麼下面的命令可以查看更詳細的通信過程。

  $ curl --trace output.txt www.sina.com

或者

  $ curl --trace-ascii output.txt www.sina.com

運行後,請打開output.txt文件查看。

五、發送表單信息

發送表單信息有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動詞

curl默認的HTTP動詞是GET,使用`-X`參數可以支持其他動詞。

  $ curl -X POST www.example.com

  $ curl -X DELETE www.example.com

七、文件上傳

假定文件上傳的表單是下面這樣:

  <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字段

有時你需要在http request頭信息中,提供一個referer字段,表示你是從哪裏跳轉過來的。

  $ curl --referer http://www.example.com http://www.example.com

九、User Agent字段

這個字段是用來表示客戶端的設備信息。服務器有時會根據這個字段,針對不同設備,返回不同格式的網頁,比如手機版和桌面版。

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可以這樣模擬:

  $ curl --user-agent "[User Agent]" [URL]

十、cookie

使用`--cookie`參數,可以讓curl發送cookie。

  $ curl --cookie "name=xxx" www.example.com

至於具體的cookie的值,可以從http response頭信息的`Set-Cookie`字段中得到。

`-c cookie-file`可以保存服務器返回的cookie到文件,`-b cookie-file`可以使用這個文件作爲cookie信息,進行後續的請求。

  $ curl -c cookies http://example.com
  $ curl -b cookies http://example.com

十一、增加頭信息

有時需要在http request之中,自行增加一個頭信息。`--header`參數就可以起到這個作用。

  $ curl --header "Content-Type:application/json" http://example.com

十二、HTTP認證

有些網域需要HTTP認證,這時curl需要用到`--user`參數。

  $ curl --user name:password example.com

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