curl 介绍与常见用法

本文主要介绍curl的相关知识及其常见用法。

1. 概述
1.1 what
curl is a command line tool and library for transferring data with URLs.

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 -L”可以使 curl 打开的链接自动跳转,获取到最终的网页。如下:

curl -L http://www.A.com
使用上面的命令访问链接A时,就会自动跳转到其重定向的链接B中,最终获取到链接B的内容。

2.5 将页面响应写入文件(-o)
使用 curl 将网页的响应消息写入到一个文件中,可以“-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 文件中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AllenLeungX

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值