一、curl命令介绍
cURL利用URL语法在命令行下工作的文件传输工具,1997年首次发行,常用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
它的功能非常强大,命令行参数多达几十种。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下载工具,cURL还包含了用于程序开发的libcurl库,可以直接使用C语言调用。
cURL支持的通信协议有FTP、FTPS、HTTP、HTTPS、TFTP、SFTP、Gopher、SCP、Telnet、DICT、FILE、LDAP、LDAPS、IMAP、POP3、SMTP和RTSP。
curl还支持SSL认证、HTTP POST、HTTP PUT、FTP上传, HTTP form based upload、proxies、HTTP/2、cookies、用户名 密码认证(Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos)、file transfer resume、proxy tunneling。
二、curl命令常用用法介绍
2.1 使用curl命令捕获网页数据
代码语言:javascript复制wbyq@wbyq:~$ curl www.baidu.com
该命令的语法是直接向www.baidu.com服务器地址发出get请求,服务器返回的数据会直接显示在命令行。
如果想将返回的数据保存在文件里,可以使用重定向命令。
代码语言:javascript复制wbyq@wbyq:~$ curl "www.baidu.com" >test.html
或者
wbyq@wbyq:~$ curl -o test.html "www.baidu.com"
如果请求的URL里含有&转义字符,可以在URL连接上加上双引号 或者在&前加 就可以取消转义。
代码语言:javascript复制curl "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=图片&hs=2&pn=0&spn=0&di=175890&pi=0&rn=1&tn=baiduimagedetail&is=0,0&ie=utf-8&oe=utf-8&cl=2&lm=-1&cs=2534506313,1688529724&os=1097436471,408122739&simid=3354786982,133358663&adpicid=0&lpn=0&ln=30&fr=ala&fm=&sme=&cg=&bdtype=0&oriquery=图片&objurl=http://a3.att.hudong.com/14/75/01300000164186121366756803686.jpg&fromurl=ippr_z2C$qAzdH3FAzdH3Fp7rtwg_z&e3Bkwthj_z&e3Bv54AzdH3Ftrw1AzdH3Fwn_89_0c_a8naaaaa8m98bm8d8nmm0cmbanmbm_3r2_z&e3Bip4s&gsm=6&islist=&querylist="
2.2 使用curl命令下载文件
下面命令是下载一张腾讯官网的图片,只要给对地址直接下载即可。
代码语言:javascript复制curl "http://qzonestyle.gtimg.cn/qzone/qzactStatics/imgs/20190626150721_85796c.png" >123.png
如果不想指定保存的名称,可以使用使用-O参数,按照服务器上的文件名,自动存在本地。
代码语言:javascript复制wbyq@wbyq:~$ curl -O "http://qzonestyle.gtimg.cn/qzone/qzactStatics/imgs/20190626150721_85796c.png"
使用curl下载QQ安装包。
代码语言:javascript复制wbyq@wbyq:~$ curl -O "https://down.qq.com/qqweb/PCQQ/PCQQ_EXE/PCQQ2020.exe"
如果想知道请求过程,可以加上-v参数,方便调试。
代码语言:javascript复制wbyq@wbyq:~$ curl -v -o 123.png "http://qzonestyle.gtimg.cn/qzone/qzactStatics/imgs/20190626150721_85796c.png"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 58.49.224.43...
* TCP_NODELAY set
* Connected to qzonestyle.gtimg.cn (58.49.224.43) port 80 (#0)
> GET /qzone/qzactStatics/imgs/20190626150721_85796c.png HTTP/1.1
> Host: qzonestyle.gtimg.cn
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: X2S_Platform
< Connection: keep-alive
< Date: Fri, 09 Oct 2020 08:36:40 GMT
< Cache-Control: max-age=259200
< Expires: Mon, 12 Oct 2020 08:36:40 GMT
< Last-Modified: Wed, 26 Jun 2019 15:08:46 GMT
< Content-Type: image/png
< Content-Length: 21366
< X-NWS-LOG-UUID: 13077040326766635903 360f37d0bbde5c541b71c39a5a6c037d
< Access-Control-Expose-Headers: x-client-proto-ver
< Access-Control-Expose-Headers: X-Server-Ip
< Access-Control-Expose-Headers: X-Upstream-IP
< server_ip: 58.49.224.17
< Keep-Alive: timeout=60
< alt-svc: quic=":443"; ma=86400; v="39,38,37,36,35"
< Vary: Origin, Accept
< X-Client-Ip: 172.24.57.14
< X-Server-IP: 58.49.224.17
< X-Cache-Lookup: Hit From Disktank3
< X-DataSrc: 2
< X-ReqGue: 0
< X-Cache-Lookup: Hit From Inner Cluster
<
{ [1720 bytes data]
100 21366 100 21366 0 0 113k 0 --:--:-- --:--:-- --:--:-- 112k
* Connection #0 to host qzonestyle.gtimg.cn left intact
wbyq@wbyq:~$
2.3 curl调用接口获取北京标准时间
免费的数据接口地址: http://www.k780.com/
获取北京时间:
代码语言:javascript复制wbyq@wbyq:~$ curl "http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"
{"success":"1","result":{"timestamp":"1602232873","datetime_1":"2020-10-09 16:41:13","datetime_2":"2020年10月09日 16时41分13秒","week_1":"5","week_2":"星期五","week_3":"周五","week_4":"Friday"}}
2.4 curl调用接口获取天气预报
获取南昌未来7天的天气预报。
代码语言:javascript复制wbyq@wbyq:~$ curl "http://api.k780.com:88/?app=weather.future&weaid=南昌&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"
{"success":"1","result":[{"weaid":"237","days":"2020-10-09","week":"星期五","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/17℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/0.gif","wind":"北风","winp":"小于3级","temp_high":"25","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"8","winpid":"0","weather_iconid":"0","weather_iconid1":"0"},{"weaid":"237","days":"2020-10-10","week":"星期六","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/17℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/0.gif","wind":"东北风转北风","winp":"小于3级","temp_high":"25","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"1","winpid":"0","weather_iconid":"0","weather_iconid1":"0"},{"weaid":"237","days":"2020-10-11","week":"星期日","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/17℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/0.gif","wind":"北风","winp":"小于3级","temp_high":"25","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"8","winpid":"0","weather_iconid":"0","weather_iconid1":"0"},{"weaid":"237","days":"2020-10-12","week":"星期一","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/17℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/0.gif","wind":"北风","winp":"3-4级","temp_high":"25","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"8","winpid":"1","weather_iconid":"0","weather_iconid1":"0"},{"weaid":"237","days":"2020-10-13","week":"星期二","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/18℃","humidity":"0%/0%","weather":"晴转多云","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/1.gif","wind":"北风","winp":"3-4级","temp_high":"25","temp_low":"18","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"2","windid":"8","winpid":"1","weather_iconid":"0","weather_iconid1":"1"},{"weaid":"237","days":"2020-10-14","week":"星期三","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"22℃/17℃","humidity":"0%/0%","weather":"阴","weather_icon":"http://api.k780.com/upload/weather/d/2.gif","weather_icon1":"http://api.k780.com/upload/weather/n/2.gif","wind":"北风","winp":"3-4级","temp_high":"22","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"3","weatid1":"3","windid":"8","winpid":"1","weather_iconid":"2","weather_iconid1":"2"},{"weaid":"237","days":"2020-10-15","week":"星期四","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"19℃/16℃","humidity":"0%/0%","weather":"中雨转大雨","weather_icon":"http://api.k780.com/upload/weather/d/8.gif","weather_icon1":"http://api.k780.com/upload/weather/n/9.gif","wind":"无持续风向","winp":"小于3级","temp_high":"19","temp_low":"16","humi_high":"0","humi_low":"0","weatid":"9","weatid1":"10","windid":"0","winpid":"0","weather_iconid":"8","weather_iconid1":"9"}]}
请求参数说明
string | 1 | 接口 此处应为:weather.future |
---|---|---|
string | 1 | 使用 API 的唯一凭证 说明 |
string | 1 | md5 后的 32 位密文,登陆用. 说明 |
{json|xml} | 0 | 返回格式 |
string | 1 | 城市气象编号 如:beijing/北京/1/101010100/202.104.153.201 |
可以是 weather.city 接口中的 weaid,cityid,citynm;也可以是 IP 地址(系统自动分析该 IP 所在城市) jsoncallback string 0 js 跨域使用 jsonp 时可使用此参数 返回参数说明
{0/1} | 执行结果 |
---|---|
string | 出错消息,当出错时将会出现此节点,否则不出现 |
date | 日期 |
string | 星期 |
string | 城市/地区 |
string | 温度 |
string | 气象编号 |
string | 湿度 |
string | 天气 详见:weather.wtype |
string | 天气图标(目录 b/c/d/n 供使用) 下载图标 |
string | 天气图标 1(目录 b/c/d/n 供使用) 下载图标 |
string | 风向 |
string | 风力 |
number | 最高温度 |
number | 最低温度 |
number | 最高湿度 |
number | 最低湿度 |
number | 天气 ID 详见:weather.wtype |
number | 天气 ID1 详见:weather.wtype |
number | 风向 ID |
number | 风力 ID |
具体的参数介绍可以到这里查看:http://www.k780.com/
2.5 使用post请求向服务器上传参数
语法格式:
代码语言:javascript复制curl http://ip:port/service/name/op -X POST -d 'param1=16¶m2=0¶m3=[{"value":1,"value":1}]'
例如:
代码语言:javascript复制curl "http://xxx.xxx.xxx:8083/carmonitorsys/errlog/upload.action?token=6899fa62-86b3-4fe3-925e-8afd31dd3370&devId=1126626497aaa88" -X POST -H "Content-type:application/json" -d '{"日志":"2020年10月9日"}'