curl网站开发指南

原文连接:阮一峰的网络日志 http://www.ruanyifeng.com/blog/2011/09/curl.htmlphp

 

做者: 阮一峰html

日期: 2011年9月 4日linux

我一贯觉得,curl只是一个编程用的函数库。编程

最近才发现,这个命令自己,就是一个无比有用的网站开发工具,请看我整理的它的用法。json

===================================服务器

curl网站开发指南cookie

阮一峰 整理网络

curl是一种命令行工具,做用是发出网络请求,而后获得和提取数据,显示在"标准输出"(stdout)上面。app

它支持多种协议,下面举例讲解如何将它用于网站开发。curl

1、查看网页源码

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

2、自动跳转

有的网址是自动跳转的。使用`-L`参数,curl就会跳转到新的网址。

  $ curl -L www.sina.com

键入上面的命令,结果就自动跳转为www.sina.com.cn。

3、显示头信息

`-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的头信息。

4、显示通讯过程

`-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文件查看。

5、发送表单信息

发送表单信息有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

6、HTTP动词

curl默认的HTTP动词是GET,使用`-X`参数能够支持其余动词。

  $ curl -X POST www.example.com

  $ curl -X DELETE www.example.com

7、文件上传

假定文件上传的表单是下面这样:

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

8、Referer字段

有时你须要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的。

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

9、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]

10、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

11、增长头信息

有时须要在http request之中,自行增长一个头信息。`--header`参数就能够起到这个做用。

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

12、HTTP认证

有些网域须要HTTP认证,这时curl须要用到`--user`参数。

  $ curl --user name:password example.com

【参考资料】

  * Using cURL to automate HTTP jobs

  * 教你学用CURL

  * 9 uses for cURL worth knowing

(完)

相关文章
相关标签/搜索