最近才发现,这个命令本身,就是一个无比有用的网站开发工具,请看我整理的它的用法。
===================================
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
【参考资料】
(完)