Loading... [curl](https://curl.haxx.se/) 不仅是一个编程用的函数库。也是一个无比有用的网站开发工具。熟练使用可以替代,图形工具 postman。 ## Usage ```sh # 调试 curl -v www.sina.com # 显示详细信息,显示一次http通信的整个过程 curl --trace output.txt www.sina.com # 查看更详细的通信过程 curl example.com/form.cgi?data=xxx # GET curl --limit-rate 200k https://google.com # 限制带宽 curl -k https://www.example.com # 跳过 SSL 检测,不会检查服务器的 SSL 证书是否正确 curl -o example.html https://www.example.com # 保存文件,命名为 example.html,同于 wget curl -O example.html https://www.example.com/foo/bar.html # 保存文件,命名为 bar.html curl -s -o /dev/null https://google.com # 完全安静 -s, 不输出错误和进度信息 curl -X POST https://www.example.com # 指定 HTTP 请求的方法。 ``` ### 用户代理标头 ```sh # GET curl www.sina.com # User-Agent ## curl 的默认用户代理字符串是curl/[version] curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://www.google.com ## 移除 User-Agent curl -A '' https://www.google.com ## 更改 User-Agent curl -H 'User-Agent: php/1.0' https://www.google.com ``` ### Cookie ```sh curl -b 'foo1=bar;foo2=bar2' https://www.google.com curl -b cookies.txt https://www.google.com # 读取本地文件 curl -c cookies.txt https://www.google.com # 将服务器设置的 Cookie 写入一个文件 ``` ### 请求体 发送 POST 请求的数据体,使用`-d`参数以后,HTTP 请求会自动加上标头`Content-Type : application/x-www-form-urlencoded`。并且会自动将请求转为 POST 方法,因此可以省略`-X POST`。 ```sh curl -d'login=emma&password=123'-X POST https://google.com/login # -d 和 --data-urlencode 差不多,d 比较智能 curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login curl -d '@data.txt' https://google.com/login # 读取本地文件,作为数据然后上传 ``` ### 请求的来源 ```sh curl -e 'https://google.com?q=example' https://www.example.com # 等价命令 curl -H 'Referer: https://google.com?q=example' https://www.example.com ``` ### 文件上传 给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传 ```sh curl -F 'file=@photo.png' https://google.com/profile # 指定 MIME 类型,否则自动为 application/octet-stream curl -F 'file=@photo.png;type=image/png' https://www.google.com/profile # 指定文件名 curl -F 'file=@photo.png;filename=me.png' https://www.google.com/profile ``` ### 构造 URL 的查询字符串 如果省略`-G`,会发出一个 POST 请求。 ```bash curl -G -d 'q=kitties' -d 'count=20' https://www.google.com/search ``` ### 请求的标头 ```sh curl -H 'Accept-Language: en-US' https://google.com curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com ``` 设置标头的同时发送json数据 ```bash curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login ``` ### 响应头 ```bash # 先输出服务器回应的标头,然后空一行,再输出网页的源码 curl -i https://www.google.com # 只输出服务器回应的标头 curl -i https://www.google.com curl --head https://www.google.com ## 等价于上一命令 ``` ### 重定向 curl 默认不重定向 ```bash curl -L -d 'tweet=hi' https://api.twitter.com/tweet # 开启重定向 ``` ### 代理 如果没有指定代理协议,默认为 HTTP ```sh curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com ``` ## Reference [curl网站开发指南](https://www.ruanyifeng.com/blog/2011/09/curl.html) [curl 的用法指南](https://www.ruanyifeng.com/blog/2019/09/curl-reference.html) 最后修改:2022 年 01 月 17 日 09 : 49 PM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 赞赏作者 支付宝微信