天天看點

curl網站開發指南

最近才發現,這個指令本身,就是一個無比有用的網站開發工具,請看我整理的它的用法。

===================================

curl網站開發指南

阮一峰 整理

curl網站開發指南

它支援多種協定,下面舉例講解如何将它用于網站開發。

一、檢視網頁源碼

直接在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

`-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"> * 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

【參考資料】

(完)

繼續閱讀