curl常见用法

本文主要介绍curl的常见用法。

说明:本文介绍的curl常见用法是面向Linux操作系统的。

1 概述

1.1 What

引用Manual中关于curl的介绍,内容如下:

curl is a tool to transfer data from or to a server, using one of the supported protocols(DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP).

引用GitHub中关于curl的介绍,内容如下:

Curl is a command-line tool for transferring data specified with URL syntax.

1.2 What's curl used for

curl is used in command lines or scripts to transfer data. 

curl is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands of software applications affecting billions of humans daily.

1.3 curl supports

curl supports DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet and TFTP. 

curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication(Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.

2 常见用法

2.1 获取页面

命令格式:

curl URL

示例用法如下:

curl https://www.baidu.com/

这是curl最简单的使用方法,运行上面的命令可以获取到“https://www.baidu.com/”指向的页面数据,同样,如果这里的URL指向的是一个文件或一幅图片,也可以直接下载到本地。

2.2 发送POST请求

命令格式:

curl -X POST -d 'post content' URL

-X选项的解释如下:

-X/--request <command>
      (HTTP) Specifies a custom request method to use when communicating with the HTTP 
server. The specified request will be used instead of the method otherwise used (which 
defaults to GET). Read the HTTP 1.1 specification for details and explanations.
      (FTP) Specifies a custom FTP command to use instead of LIST when doing file lists 
with ftp.
      If this option is used several times, the last one will be used.

-d选项的解释如下:

-d/--data <data>
      (HTTP) Sends the specified data in a POST request to the HTTP server, in a way that 
can emulate as if a user has filled in a HTML form and pressed the submit button. Note that 
the data is sent exactly as specified with no extra processing (with all newlines cut off). 
The data is expected to be "url-encoded". This will cause curl to pass the data to the 
server using the content-type application/x-www-form-urlencoded. Compare to -F/--form. If 
this option is used more than once on the same command line, the data pieces specified will 
be merged together with a separating &-letter. Thus, using '-d name=daniel -d skill=lousy' 
would generate a post chunk that looks like 'name=daniel&skill=lousy'.

示例用法如下:

curl -X -d 'api_ver=1.0.0&merchant_id=1800257711' http://api.gcdev.tfb8.com/cgi-bin/v2.0/api_merchant_agency_qry.cgi

注意:-d选项后的内容强烈建议用单引号“'”包起来(而非双引号)。

2.3 发送GET请求

命令格式:

curl URL?param1=value1\&param2=value2...

示例用法如下:

curl http://api.gcdev.tfb8.com/cgi-bin/v2.0/api_merchant_agency_qry.cgi?api_ver=1.0.0\&merchant_id=1800257711

注意:在shell中执行curl命令发送GET请求时,如果要发送多个参数,则需要将参数之间的分隔符“&”修改为“\&”,否则shell会将分隔符“&”理解为后台执行指令,导致GET请求参数被截断。

2.4 链接重定向/自动跳转(-L)

使用curl打开某些存在重定向的链接时,可能无法获取我们想要的网页内容。假设有链接“http://www.A.com”,使用浏览器访问该链接,会自动跳转到链接“http://www.B.com”。那么如果使用curl直接访问“A”链接,示例命令如下:

curl http://www.A.com

会提示“A”链接不存在,因为“A”链接已经重定向到“B”链接了,但是“curl”不能识别出来(这个重定向动作)。

此时,就需要使用“-L”选项了,该选项可以使curl打开的链接自动跳转,从而获取到最终的网页。示例命令如下:

curl -L http://www.A.com

使用上面的命令访问链接“A”时,就会自动跳转到其重定向的链接“B”中,最终获取到链接“B”的内容。

2.5 将页面响应写入文件(-o)

通过“-o”选项可以将网页的响应内容写入到一个文件中,如下:

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

上面的命令会将链接“http://mirrors.aliyun.com/repo/Centos-7.repo”的响应内容,写入到文件“/etc/yum.repos.d/CentOS-Base.repo”中。

2.6 通过POST请求发送json消息

参考2.2节中发送POST请求相关内容,通过POST请求发送json消息的命令格式如下:

curl -H "Content-Type:application/json" -X POST -d '{json消息体}' URL

示例命令如下:

curl -H "Content-Type:application/json" -X POST -d '{"action":"on_snapshot", "ip":"192.168.237.224", "app":"live", "stream":"livestream_afp"}' http://192.168.237.224:8085/api/v1/snapshot

-H 选项的解释如下:

-H, --header <header>
      (HTTP) Extra header to use when getting a web page. You may specify any number of extra headers. Note that if you should add a custom header that has the same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one. This allows you to make even trickier stuff than curl would normally do. You should not replace internally set headers without knowing perfectly well what you're doing. Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H "Host:". If you send the custom header with no-value then its header must be terminated with a semicolon, such as -H  "X-Custom-Header;" to send "X-Custom-Header:".

      curl will make sure that each header you add/replace is sent with the proper end-of-line marker, you should thus not add that as a part of the header content: do not add newlines
or carriage returns, they will only mess things up for you.

      See also the -A, --user-agent and -e, --referer options.

      This option can be used multiple times to add/replace/remove multiple headers.

1.访问http页面内容,输出到标准输出 curl http://www.neocanable.com 2.生成文件 curl -o index.html http://www.neocanable.com 以远程文件名保存 curl -O http://www.neocanable.com 参数-o为输出到某个文件,上面的命令等同于wget http://www.neocanable.com或者curl http://www.neocanable.com > index.html 3.添加proxy curl -x xxx.xxx.xxx.xxx http://www.neocanable.com 通过代理ip访问网页 4.添加浏览器信息 通常服务器的日志会记录客户端浏览器的信息 curl -A “浏览器信息” http://www.neocanable.com 5.批量下载文件 curl http://www.xxx.com/action/[1-100].html > /dev/null 这个最适合爬自己网站的缓存了 文件下载后重新命名和类正则使用,下载后的文件是demo1-001.html curl -o #1_#2 http://www.xxx.com/~{demo1,demo2}/[1-100].html 创建需要的目录 curl -o –create-dirs http://www.xxx.com/~{demo1,demo2}/[1-100].html 6.分块下载 curl -r 0-1024 http://www.xxx.com/aa.zip curl -r 1025- http://www.xxx.com/aa.zip 先下1M,然后再下剩下的 7.curl ftp 访问ftp地址 curl -u username:password ftp://www.xxx.com curl -u ftp://www.xxx.com 添加端口 curl -u username:password -P8899 ftp://www.xxx.com 上传文件到ftp curl -T /home/neo/demo.jpg -u username:password ftp://www.xxx.com 8.测试参数 测试站点相应时间 curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} www.google.com 查看http_code curl -o /dev/null -s -w %{http_code} http://www.neocanable.com 网页或文件大小 curl -o /dev/null -s -w %{size_header} http://www.neocanable.com http_code:http返回类似404,200,500等 time_total:总相应时间 time_namelookup:域名解析时间 time_connect:连接到目标地址耗费的时间 time_pretransfer:从执行到开始传输文件的时间间隔 time_starttransfer:从执行到开始传输文件的时间间隔 size_download:下载网页或文件大小 size_upload:上传文件大小 size_header:响应头 size_request:发送请求参数大小 speed_download:传输速度 speed_upload:平均上传速度 content_type:下载文件类型. (Added in 7.9.5) 9.post和get请求 get请求 curl “param1=name&params2=pass” http://www.xxx.com post请求 curl -d “param1=name&params2=pass” http://www.xxx.com 10.响应超时 curl -m 40 http://www.xxx.com curl –timeout 40 http://www.xxx.com 11.破解网站的防盗链 curl -e “http://www.a.net” http://www.b.net/acion 12.网站头部信息 curl -I http://www.neocanable.com 13.更总url跳转 curl -L http://url.cn/2yQFfd 14.正确的给url编码 curl –data-urlencode http://www.xxx.com/action?name=张三&sex=男 15.限制url的传输速度 curl –limit-rate http://www.xxx.com/action 16.限制下载文件大小 curl –max-filesize 1024 http://www.xxx.com/action 超过1M将不执行操作,并且返回出错 17.curl错误代码 1:未支持的协议。此版cURL不支持这一协议。 2:初始化失败。 3:URL格式错误。语法不正确。 5:无法解析代理。无法解析给定代理主机。 6:无法解析主机。无法解析给定的远程主机。 7:无法连接到主机。 8:FTP非正常的服务器应答。cURL无法解析服务器发送的数据。 9:FTP访问被拒绝。服务器拒绝登入或无法获取您想要的特定资源或目录。最有可能的是您试图进入一个在此服务器上不存在的目录。 11:FTP 非正常的PASS回复。cURL无法解析发送到PASS请求的应答。 13:FTP 非正常的的PASV应答,cURL无法解析发送到PASV请求的应答。 14:FTP非正常的227格式。cURL无法解析服务器发送的227行。 15:FTP无法连接到主机。无法解析在227行中获取的主机IP。 17:FTP无法设定为二进制传输。无法改变传输方式到二进制。 18:部分文件。只有部分文件被传输。 19:FTP不能下载/访问给定的文件, RETR (或类似)命令失败。 21:FTP quote错误。quote命令从服务器返回错误。 22:HTTP 找不到网页。找不到所请求的URL或返回另一个HTTP 400或以上错误。此返回代码只出现在使用了-f/–fail选项以后。 23:写入错误。cURL无法向本地文件系统或类似目的写入数据。 25:FTP 无法STOR文件。服务器拒绝了用于FTP上传的STOR操作。 26:读错误。各类读取问题。 27:内存不足。内存分配请求失败。 28:操作超时。到达指定的超时期限条件。 30:FTP PORT失败。PORT命令失败。并非所有的FTP服务器支持PORT命令,请尝试使用被动(PASV)传输代替! 31:FTP无法使用REST命令。REST命令失败。此命令用来恢复的FTP传输。 33:HTTP range错误。range “命令”不起作用。 34:HTTP POST错误。内部POST请求产生错误。 35:SSL连接错误。SSL握手失败。 36:FTP 续传损坏。不能继续早些时候被中止的下载。 37:文件无法读取。无法打开文件。权限问题? 38:LDAP 无法绑定。LDAP绑定(bind)操作失败。 39:LDAP 搜索失败。 41:功能无法找到。无法找到必要的LDAP功能。 42:由回调终止。应用程序告知cURL终止运作。 43:内部错误。由一个不正确参数调用了功能。 45:接口错误。指定的外发接口无法使用。 47:过多的重定向。cURL达到了跟随重定向设定的最大限额跟 48:指定了未知TELNET选项。 49:不合式的telnet选项。 51:peer的SSL证书或SSH的MD5指纹没有确定。 52:服务器无任何应答,该情况在此处被认为是一个错误。 53:找不到SSL加密引擎。 54:无法将SSL加密引擎设置为默认。 55:发送网络数据失败。 56:在接收网络数据时失败。 58:本地证书有问题。 59:无法使用指定的SSL密码。 60:peer证书无法被已知的CA证书验证。 61:无法辨识的传输编码。 62:无效的LDAP URL。 63:超过最大文件尺寸。 64:要求的FTP的SSL水平失败。 65:发送此数据需要的回卷(rewind)失败。 66:初始化SSL引擎失败。 67:用户名、密码或类似的信息未被接受,cURL登录失败。 68:在TFTP服务器上找不到文件。 69:TFTP服务器权限有问题。 70:TFTP服务器磁盘空间不足。 71:非法的TFTP操作。 72:未知TFTP传输编号(ID)。 73:文件已存在(TFTP) 。 74:无此用户(TFTP) 。 75:字符转换失败。 76:需要字符转换功能。 77:读SSL证书出现问题(路径?访问权限? ) 。 78:URL中引用的资源不存在。 79:SSH会话期间发生一个未知错误。 80:未能关闭SSL连接。 82:无法加载CRL文件,丢失或格式不正确(在7.19.0版中增加 ) 。 83:签发检查失败(在7.19.0版中增加 ) 。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liitdar

赠人玫瑰,手有余香,君与吾共勉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值