关于curl:curl网站开发指南

6次阅读

共计 3703 个字符,预计需要花费 10 分钟才能阅读完成。

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
正文完
 0