CURL 用法

本文详细介绍了curl命令在网站开发中的多种用途,包括查看网页源码、自动跳转、显示头信息等,并提供了具体示例。

我一向以为,curl只是一个编程用的函数库。

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

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

curl网站开发指南

阮一峰 整理

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


十二、HTTP body

-H is short for --header, -d for --data

-H "Content-Type: application/json"
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login


【参考资料】

  * Using cURL to automate HTTP jobs

  * 教你学用CURL

  * 9 uses for cURL worth knowing

### curl 使用方法、教程与示例 #### 什么是 curl? `curl` 是一个强大的命令行工具,支持多种协议(HTTP/HTTPS、FTP 等),能够用于发送和接收网络数据包。它不仅适用于文件下载,还可以作为开发中的调试工具来模拟 HTTP 请求[^2]。 --- #### 安装验证 在 Windows 上完成 `curl` 的安装后,可以通过以下方式验证其是否正常工作: ```bash curl --version ``` 如果返回版本号及相关信息,则说明安装成功[^3]。 --- #### 基本使用 以下是几个常见的 `curl` 使用场景: 1. **GET 请求** 发送简单的 GET 请求获取网页内容: ```bash curl https://www.example.com ``` 2. **带自定义头部的请求** 添加特定的 HTTP 头部字段,例如设置 Content-Type 和 Authorization: ```bash curl -H 'Content-Type: application/json' \ -H 'Authorization: Bearer YOUR_ACCESS_TOKEN' \ https://api.example.com/resource ``` 此处 `-H` 参数用来指定额外的头信息[^4]。 3. **POST 请求** 向服务器提交 JSON 数据时可采用如下形式: ```bash curl -X POST "https://example.com/api/v1/create" \ -d '{"key":"value"}' ``` 这里 `-X POST` 明确指定了请求类型为 POST,而 `-d` 表示附加的数据体。 4. **携带认证令牌访问资源** 对受保护接口发起调用需附带身份凭证: ```bash curl -H 'Authorization: Bearer JWT_TOKEN_HERE' \ https://secure-api.example.org/profiles/me ``` 5. **静默模式运行** 若要屏蔽进度条和其他冗余输出,可以加入选项 `-sS` 来实现安静执行但仍保留错误提示: ```bash curl -sS http://target-site.com/page.html > output.txt ``` 6. **保存响应至本地文件** 当需要把远程 URL 下载的内容存储成磁盘上的某个位置时可用此法: ```bash curl -O https://remote-server/file.zip ``` 或者手动命名目标路径名: ```bash curl -o custom-name.pdf https://source/document.pdf ``` 7. **批量处理多个链接** 如果有若干地址要逐一抓取的话,可通过循环结构简化操作流程: ```bash urls=("http://site-a/" "http://site-b/") for url in "${urls[@]}"; do echo Fetching $url... curl "$url" done ``` 8. **SSL 验证跳过** 在某些情况下可能遇到证书校验失败的情况,此时可以用下面的方法绕开检查(注意生产环境下应谨慎对待): ```bash curl -k https://self-signed-cert-domain/ ``` 9. **跟踪重定向链路** 默认状态下当源站点发生转移不会自动跟随新的 URI;为了确保整个过程透明化,应当启用追踪机制: ```bash curl -L http://original-url-that-redirects-to-another-one/ ``` 10. **限流控制** 控制上传或下载速率以避免占用过多带宽资源: ```bash curl --limit-rate 100K https://big-file-download-location/ ``` --- #### 实际应用场景举例 假设我们正在构建搜索引擎优化(SEO)项目,并希望通过百度提供的 API 推送新页面索引给爬虫引擎,则按照官方指导编写脚本如下所示: ```bash curl -i -H "Host: data.zz.baidu.com" \ -H "Content-Length: 83" \ -H "User-Agent: curl/7.12.1" \ -H "Content-Type: text/plain" \ --data-binary @urls.txt \ "http://data.zz.baidu.com/urls?site=yourdomain.com&token=YourTokenHere" ``` 其中 `@urls.txt` 文件包含待更新的一系列网址列表[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值