http://xshow./blog/1597170
curl是一種命令行工具,作用是發(fā)出網(wǎng)絡(luò)請(qǐng)求,然后得到和提取數(shù)據(jù),顯示在"標(biāo)準(zhǔn)輸出"(stdout)上面。@舍得Share
它支持多種協(xié)議,下面舉例講解如何將它用于網(wǎng)站開(kāi)發(fā)。
一、查看網(wǎng)頁(yè)源碼
直接在curl命令后加上網(wǎng)址,就可以看到網(wǎng)頁(yè)源碼。我們以網(wǎng)址www.sina.com為例(選擇該網(wǎng)址,主要因?yàn)樗木W(wǎng)頁(yè)代碼較短):
curl www.
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
The document has moved <a href="http://www./">here.
</body></html>
如果要把這個(gè)網(wǎng)頁(yè)保存下來(lái),可以使用-o參數(shù),這就相當(dāng)于使用wget命令了。
curl -o [文件名] www.
二、自動(dòng)跳轉(zhuǎn)
有的網(wǎng)址是自動(dòng)跳轉(zhuǎn)的。使用-L參數(shù),curl就會(huì)跳轉(zhuǎn)到新的網(wǎng)址。
curl -L www.
鍵入上面的命令,結(jié)果就自動(dòng)跳轉(zhuǎn)為www.。
三、顯示頭信息
-i參數(shù)可以顯示http response的頭信息,連同網(wǎng)頁(yè)代碼一起。
curl -i www.
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>
The document has moved <a >here.
</body></html>
-I參數(shù)則是只顯示http response的頭信息。
四、顯示通信過(guò)程
-v參數(shù)可以顯示一次http通信的整個(gè)過(guò)程,包括端口連接和http request頭信息。
curl -v www.
* 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>
The document has moved <a >here.
</body></html>
* Closing connection #0
如果你覺(jué)得上面的信息還不夠,那么下面的命令可以查看更詳細(xì)的通信過(guò)程。
curl --trace output.txt www.
或者
curl --trace-ascii output.txt www.
運(yùn)行后,請(qǐng)打開(kāi)output.txt文件查看。
五、發(fā)送表單信息
發(fā)送表單信息有GET和POST兩種方法。GET方法相對(duì)簡(jiǎn)單,只要把數(shù)據(jù)附在網(wǎng)址后面就行。
curl /form.cgi?data=xxx
POST方法必須把數(shù)據(jù)和網(wǎng)址分開(kāi),curl就要用到--data參數(shù)。
curl --data "data=xxx" /form.cgi
如果你的數(shù)據(jù)沒(méi)有經(jīng)過(guò)表單編碼,還可以讓curl為你編碼,參數(shù)是--data-urlencode。
curl --data-urlencode "date=April 1" /form.cgi
六、文件上傳
假定文件上傳的表單是下面這樣:
<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字段
有時(shí)你需要在http request頭信息中,提供一個(gè)referer字段,表示你是從哪里跳轉(zhuǎn)過(guò)來(lái)的。
curl --referer http://www.
http://www.
八、User Agent字段
這個(gè)字段是用來(lái)表示客戶端的設(shè)備信息。服務(wù)器有時(shí)會(huì)根據(jù)這個(gè)字段,針對(duì)不同設(shè)備,返回不同格式的網(wǎng)頁(yè),比如手機(jī)版和桌面版。
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參數(shù),可以讓curl發(fā)送cookie。
curl --cookie "name=xxx" www.
至于具體的cookie的值,可以從http response頭信息的Set-Cookie字段中得到。
十、增加頭信息
有時(shí)需要在http request之中,自行增加一個(gè)頭信息。--header參數(shù)就可以起到這個(gè)作用。
curl --header "xxx: xxxxxx" http://
十一、HTTP認(rèn)證
有些網(wǎng)域需要HTTP認(rèn)證,這時(shí)curl需要用到--user參數(shù)。
curl --user name:password
【參考資料】
* Using cURL to automate HTTP jobs