简介

curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。

它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。

本文介绍它的主要命令行参数,作为日常的参考,方便查阅。内容主要翻译自《curl cookbook》。为了节约篇幅,下面的例子不包括运行时的输出,初学者可以先看我以前写的《curl 初学者教程》

不带有任何参数时,curl 就是发出 GET 请求。


$ curl https://www.example.com

上面命令向www.example.com发出 GET 请求,服务器返回的内容会在命令行输出。

-A

-A参数指定客户端的用户代理标头,即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://google.com

上面命令将User-Agent改成 Chrome 浏览器。


$ curl -A '' https://google.com

上面命令会移除User-Agent标头。

也可以通过-H参数直接指定标头,更改User-Agent


$ curl -H 'User-Agent: php/1.0' https://google.com

-b

-b参数用来向服务器发送 Cookie。


$ curl -b 'foo=bar' https://google.com

上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。


$ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com

上面命令发送两个 Cookie。


$ curl -b cookies.txt https://www.google.com

上面命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。

-c

-c参数将服务器设置的 Cookie 写入一个文件。


$ curl -c cookies.txt https://www.google.com

上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt

-d

-d参数用于发送 POST 请求的数据体。


$ curl -d'login=emma&password=123'-X POST https://google.com/login
# 或者
$ curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login

使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST

-d参数可以读取本地文本文件的数据,向服务器发送。


$ curl -d '@data.txt' https://google.com/login

上面命令读取data.txt文件的内容,作为数据体向服务器发送。

--data-urlencode

--data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。


$ curl --data-urlencode 'comment=hello world' https://google.com/login

上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。

-e

-e参数用来设置 HTTP 的标头Referer,表示请求的来源。


curl -e 'https://google.com?q=example' https://www.example.com

上面命令将Referer标头设为https://google.com?q=example

-H参数可以通过直接添加标头Referer,达到同样效果。


curl -H 'Referer: https://google.com?q=example' https://www.example.com

-F

-F参数用来向服务器上传二进制文件。


$ curl -F 'file=@photo.png' https://google.com/profile

上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。

-F参数可以指定 MIME 类型。


$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile

上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream

-F参数也可以指定文件名。


$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile

上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png

-G

-G参数用来构造 URL 的查询字符串。


$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

上面命令会发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略--G,会发出一个 POST 请求。

如果数据需要 URL 编码,可以结合--data--urlencode参数。


$ curl -G --data-urlencode 'comment=hello world' https://www.example.com

-H

-H参数添加 HTTP 请求的标头。


$ curl -H 'Accept-Language: en-US' https://google.com

上面命令添加 HTTP 标头Accept-Language: en-US


$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com

上面命令添加两个 HTTP 标头。


$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

上面命令添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。

-i

-i参数打印出服务器回应的 HTTP 标头。


$ curl -i https://www.example.com

上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。

-I

-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。


$ curl -I https://www.example.com

上面命令输出服务器对 HEAD 请求的回应。

--head参数等同于-I


$ curl --head https://www.example.com

-k

-k参数指定跳过 SSL 检测。


$ curl -k https://www.example.com

上面命令不会检查服务器的 SSL 证书是否正确。

-L

-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。


$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet

--limit-rate

--limit-rate用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。


$ curl --limit-rate 200k https://google.com

上面命令将带宽限制在每秒 200K 字节。

-o

-o参数将服务器的回应保存成文件,等同于wget命令。


$ curl -o example.html https://www.example.com

上面命令将www.example.com保存成example.html

-O

-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。


$ curl -O https://www.example.com/foo/bar.html

上面命令将服务器回应保存成文件,文件名为bar.html

-s

-s参数将不输出错误和进度信息。


$ curl -s https://www.example.com

上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。

如果想让 curl 不产生任何输出,可以使用下面的命令。


$ curl -s -o /dev/null https://google.com

-S

-S参数指定只输出错误信息,通常与-s一起使用。


$ curl -s -o /dev/null https://google.com

上面命令没有任何输出,除非发生错误。

-u

-u参数用来设置服务器认证的用户名和密码。


$ curl -u 'bob:12345' https://google.com/login

上面命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1

curl 能够识别 URL 里面的用户名和密码。


$ curl https://bob:12345@google.com/login

上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。


$ curl -u 'bob' https://google.com/login

上面命令只设置了用户名,执行后,curl 会提示用户输入密码。

-v

-v参数输出通信的整个过程,用于调试。


$ curl -v https://www.example.com

--trace参数也可以用于调试,还会输出原始的二进制数据。


$ curl --trace - https://www.example.com

-x

-x参数指定 HTTP 请求的代理。


$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com

上面命令指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出。

如果没有指定代理协议,默认为 HTTP。


$ curl -x james:cats@myproxy.com:8080 https://www.example.com

上面命令中,请求的代理使用 HTTP 协议。

-X

-X参数指定 HTTP 请求的方法。


$ curl -X POST https://www.example.com

上面命令对https://www.example.com发出 POST 请求。

curl 基础的更多相关文章

  1. CURL基础

    下载单个文件: #下载单个文件,默认将输出打印到标准输出中(STDOUT)中curl http://www.centos.org # 将文件下载到本地并命名为mygettext.html curl - ...

  2. 常用curl测试命令

    1.curl 基础用法 2.curl 常用 3.curl 拓展 1.curl基础用法 语法:# curl [option] [url] curl除了用以请求数据,还可以用来上传下载 -A/--user ...

  3. 【Curl】【转】curl用法!

    curl基础用法! www.ruanyifeng.com/blog/2019/09/curl-reference.html

  4. thinkphp实现短信验证注册

    前言 注册时经常需要用到短信验证码,本文记录一下思路和具体实现. 短信验证平台使用云片,短信验证码的生成使用thinkphp. 思路 1.用户输入手机号,请求获取短信验证码. 2.thinkphp生成 ...

  5. 一个基础的CURL类

    /** * 一个基础的CURL类 * * @author Smala */ class curl{ public $ch; public $cookie = '/cookie'; public $rs ...

  6. PHP 中的 cURL 爬虫实战基础

    最近准备入手 PHP 爬虫,发现 PHP 的 cURL 这一知识点不可越过.本文探讨基础实战,需要提前了解命令行的使用并会进行 PHP 的环境搭建. cURL 的概念 cURL,Client URL ...

  7. Linux 基础命令-CURL 表单上传文件

    CURL -F, --form <name=content> (HTTP) This lets curl emulate a filled-in form in which a user ...

  8. Linux基础命令介绍七:网络传输与安全 wget curl rsync iptables

    本篇接着介绍网络相关命令:wget 文件下载工具.curl 网络数据传输工具.rsync 文件传输工具等. 本篇接着介绍网络相关命令 1.wget 文件下载工具 wget [option]... [U ...

  9. 【原创】Linux基础之curl

    http请求过程如下: # curl -v http://www.baidu.com % Total % Received % Xferd Average Speed Time Time Time C ...

随机推荐

  1. [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  2. vue中:key 和react 中key={} 的作用,以及ref的特性?

    vue中:key 和react 中key={} 为了给 vue 或者react 一个提示,以便它能跟踪每个节点的身份,从而重用和重新排序现有元素,你需要为每项提供一个唯一 key 属性 一句话概括就是 ...

  3. vue computed 源码分析

    我们来看看computed的实现.最简单的一个demo如下: <html> <head> <meta http-equiv="Content-Type" ...

  4. vue与webpack

    由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西,开发过程中需要改动到build和config里面一些相关的配置,所以刚好趁此机会将所有配置文件看一遍,理一理思路,也便于以后修 ...

  5. sql 左连接与右连接

    假设有A,B两个表. 表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 a20050114 5 a20050115 表B记录如下: bID ...

  6. leetcode-两个数组的交集

    C++解题方法: class Solution { public: vector<int> intersection(vector<int>& nums1, vecto ...

  7. 单源最短路径问题1 (Bellman-Ford算法)

    /*单源最短路径问题1 (Bellman-Ford算法)样例: 5 7 0 1 3 0 3 7 1 2 4 1 3 2 2 3 5 2 4 6 3 4 4 输出: [0, 3, 7, 5, 9] */ ...

  8. delphi xe10 网络文件传送

    //网络传送文件(类似Server/Client) TTetheringManager|设备管理.TTetheringAppProfile|文件发送 待补充

  9. java script 基本函数

    Math.random()    是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值. 日期时间函数(需要用变量调用):var b = new Date();      // ...

  10. thinkphp 快速缓存

    如果你的存储数据没有有效期的需求,那么系统还提供了一个快速缓存方法F可以用来更快的操作. 大理石平台厂家 F方法可以支持不同的存储类型,如果是文件类型的话,默认保存在DATA_PATH目录下面. 快速 ...