curl 是一个利用 URL 语法在命令行下工作的文件传输工具,1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称 curl 为下载工具。
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、file transfer resume、proxy tunneling 等。
curl 库提供了很多强大的功能,你可以利用它来进行 HTTP/HTTPS 请求、上传/下载文件等,且支持 Cookie、认证、代理、限速等。
通过命令 curl --version 查看 curl 工具版本:
$ curl --version
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.36 zlib/1.2.7 libidn/1.28 libssh2/1.4.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets
通过命令 curl --help 查看 curl 支持的选项(options)参数如下所示:
$ curl --help
Usage: curl [options...] <url>
Options: (H) means HTTP/HTTPS only, (F) means FTP only
--anyauth Pick "any" authentication method (H)
-a, --append Append to target file when uploading (F/SFTP)
--basic Use HTTP Basic Authentication (H)
--cacert FILE CA certificate to verify peer against (SSL)
--capath DIR CA directory to verify peer against (SSL)
-E, --cert CERT[:PASSWD] Client certificate file and password (SSL)
--cert-type TYPE Certificate file type (DER/PEM/ENG) (SSL)
--ciphers LIST SSL ciphers to use (SSL)
--compressed Request compressed response (using deflate or gzip)
-K, --config FILE Specify which config file to read
--connect-timeout SECONDS Maximum time allowed for connection
-C, --continue-at OFFSET Resumed transfer offset
-b, --cookie STRING/FILE String or file to read cookies from (H)
-c, --cookie-jar FILE Write cookies to this file after operation (H)
--create-dirs Create necessary local directory hierarchy
--crlf Convert LF to CRLF in upload
--crlfile FILE Get a CRL list in PEM format from the given file
-d, --data DATA HTTP POST data (H)
--data-ascii DATA HTTP POST ASCII data (H)
--data-binary DATA HTTP POST binary data (H)
--data-urlencode DATA HTTP POST data url encoded (H)
--delegation STRING GSS-API delegation permission
--digest Use HTTP Digest Authentication (H)
--disable-eprt Inhibit using EPRT or LPRT (F)
--disable-epsv Inhibit using EPSV (F)
-D, --dump-header FILE Write the headers to this file
--egd-file FILE EGD socket path for random data (SSL)
--engine ENGINGE Crypto engine (SSL). "--engine list" for list
-f, --fail Fail silently (no output at all) on HTTP errors (H)
-F, --form CONTENT Specify HTTP multipart POST data (H)
--form-string STRING Specify HTTP multipart POST data (H)
--ftp-account DATA Account data string (F)
--ftp-alternative-to-user COMMAND String to replace "USER [name]" (F)
--ftp-create-dirs Create the remote dirs if not present (F)
--ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F)
--ftp-pasv Use PASV/EPSV instead of PORT (F)
-P, --ftp-port ADR Use PORT with given address instead of PASV (F)
--ftp-skip-pasv-ip Skip the IP address for PASV (F)
--ftp-pret Send PRET before PASV (for drftpd) (F)
--ftp-ssl-ccc Send CCC after authenticating (F)
--ftp-ssl-ccc-mode ACTIVE/PASSIVE Set CCC mode (F)
--ftp-ssl-control Require SSL/TLS for ftp login, clear for transfer (F)
-G, --get Send the -d data with a HTTP GET (H)
-g, --globoff Disable URL sequences and ranges using {} and []
-H, --header LINE Custom header to pass to server (H)
-I, --head Show document info only
-h, --help This help text
--hostpubmd5 MD5 Hex encoded MD5 string of the host public key. (SSH)
-0, --http1.0 Use HTTP 1.0 (H)
--ignore-content-length Ignore the HTTP Content-Length header
-i, --include Include protocol headers in the output (H/F)
-k, --insecure Allow connections to SSL sites without certs (H)
--interface INTERFACE Specify network interface/address to use
-4, --ipv4 Resolve name to IPv4 address
-6, --ipv6 Resolve name to IPv6 address
-j, --junk-session-cookies Ignore session cookies read from file (H)
--keepalive-time SECONDS Interval between keepalive probes
--key KEY Private key file name (SSL/SSH)
--key-type TYPE Private key file type (DER/PEM/ENG) (SSL)
--krb LEVEL Enable Kerberos with specified security level (F)
--libcurl FILE Dump libcurl equivalent code of this command line
--limit-rate RATE Limit transfer speed to this rate
-l, --list-only List only names of an FTP directory (F)
--local-port RANGE Force use of these local port numbers
-L, --location Follow redirects (H)
--location-trusted like --location and send auth to other hosts (H)
-M, --manual Display the full manual
--mail-from FROM Mail from this address
--mail-rcpt TO Mail to this receiver(s)
--mail-auth AUTH Originator address of the original email
--max-filesize BYTES Maximum file size to download (H/F)
--max-redirs NUM Maximum number of redirects allowed (H)
-m, --max-time SECONDS Maximum time allowed for the transfer
--metalink Process given URLs as metalink XML file
--negotiate Use HTTP Negotiate Authentication (H)
-n, --netrc Must read .netrc for user name and password
--netrc-optional Use either .netrc or URL; overrides -n
--netrc-file FILE Set up the netrc filename to use
-N, --no-buffer Disable buffering of the output stream
--no-keepalive Disable keepalive use on the connection
--no-sessionid Disable SSL session-ID reusing (SSL)
--noproxy List of hosts which do not use proxy
--ntlm Use HTTP NTLM authentication (H)
-o, --output FILE Write output to <file> instead of stdout
--pass PASS Pass phrase for the private key (SSL/SSH)
--post301 Do not switch to GET after following a 301 redirect (H)
--post302 Do not switch to GET after following a 302 redirect (H)
--post303 Do not switch to GET after following a 303 redirect (H)
-#, --progress-bar Display transfer progress as a progress bar
--proto PROTOCOLS Enable/disable specified protocols
--proto-redir PROTOCOLS Enable/disable specified protocols on redirect
-x, --proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port
--proxy-anyauth Pick "any" proxy authentication method (H)
--proxy-basic Use Basic authentication on the proxy (H)
--proxy-digest Use Digest authentication on the proxy (H)
--proxy-negotiate Use Negotiate authentication on the proxy (H)
--proxy-ntlm Use NTLM authentication on the proxy (H)
-U, --proxy-user USER[:PASSWORD] Proxy user and password
--proxy1.0 HOST[:PORT] Use HTTP/1.0 proxy on given port
-p, --proxytunnel Operate through a HTTP proxy tunnel (using CONNECT)
--pubkey KEY Public key file name (SSH)
-Q, --quote CMD Send command(s) to server before transfer (F/SFTP)
--random-file FILE File for reading random data from (SSL)
-r, --range RANGE Retrieve only the bytes within a range
--raw Do HTTP "raw", without any transfer decoding (H)
-e, --referer Referer URL (H)
-J, --remote-header-name Use the header-provided filename (H)
-O, --remote-name Write output to a file named as the remote file
--remote-name-all Use the remote file name for all URLs
-R, --remote-time Set the remote file's time on the local output
-X, --request COMMAND Specify request command to use
--resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS
--retry NUM Retry request NUM times if transient problems occur
--retry-delay SECONDS When retrying, wait this many seconds between each
--retry-max-time SECONDS Retry only within this period
-S, --show-error Show error. With -s, make curl show errors when they occur
-s, --silent Silent mode. Don't output anything
--socks4 HOST[:PORT] SOCKS4 proxy on given host + port
--socks4a HOST[:PORT] SOCKS4a proxy on given host + port
--socks5 HOST[:PORT] SOCKS5 proxy on given host + port
--socks5-basic Enable username/password auth for SOCKS5 proxies
--socks5-gssapi Enable GSS-API auth for SOCKS5 proxies
--socks5-hostname HOST[:PORT] SOCKS5 proxy, pass host name to proxy
--socks5-gssapi-service NAME SOCKS5 proxy service name for gssapi
--socks5-gssapi-nec Compatibility with NEC SOCKS5 server
-Y, --speed-limit RATE Stop transfers below speed-limit for 'speed-time' secs
-y, --speed-time SECONDS Time for trig speed-limit abort. Defaults to 30
--ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP)
--ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP)
-2, --sslv2 Use SSLv2 (SSL)
-3, --sslv3 Use SSLv3 (SSL)
--ssl-allow-beast Allow security flaw to improve interop (SSL)
--stderr FILE Where to redirect stderr. - means stdout
--tcp-nodelay Use the TCP_NODELAY option
-t, --telnet-option OPT=VAL Set telnet option
--tftp-blksize VALUE Set TFTP BLKSIZE option (must be >512)
-z, --time-cond TIME Transfer based on a time condition
-1, --tlsv1 Use => TLSv1 (SSL)
--tlsv1.0 Use TLSv1.0 (SSL)
--tlsv1.1 Use TLSv1.1 (SSL)
--tlsv1.2 Use TLSv1.2 (SSL)
--trace FILE Write a debug trace to the given file
--trace-ascii FILE Like --trace but without the hex output
--trace-time Add time stamps to trace/verbose output
--tr-encoding Request compressed transfer encoding (H)
-T, --upload-file FILE Transfer FILE to destination
--url URL URL to work with
-B, --use-ascii Use ASCII/text transfer
-u, --user USER[:PASSWORD] Server user and password
--tlsuser USER TLS username
--tlspassword STRING TLS password
--tlsauthtype STRING TLS authentication type (default SRP)
--unix-socket FILE Connect through this UNIX domain socket
-A, --user-agent STRING User-Agent to send to server (H)
-v, --verbose Make the operation more talkative
-V, --version Show version number and quit
-w, --write-out FORMAT What to output after completion
--xattr Store metadata in extended file attributes
-q If used as the first parameter disables .curlrc
如上所示,curl(7.29.0)所支持的选项(options)参数如下:
在以下选项中,(H) 表示仅适用 HTTP/HTTPS ,(F) 表示仅适用于 FTP
--anyauth 选择 "any" 认证方法 (H)
-a, --append 添加要上传的文件 (F/SFTP)
--basic 使用HTTP基础认证(Basic Authentication)(H)
--cacert FILE CA 证书,用于每次请求认证 (SSL)
--capath DIR CA 证书目录 (SSL)
-E, --cert CERT[:PASSWD] 客户端证书文件及密码 (SSL)
--cert-type TYPE 证书文件类型 (DER/PEM/ENG) (SSL)
--ciphers LIST SSL 秘钥 (SSL)
--compressed 请求压缩 (使用 deflate 或 gzip)
-K, --config FILE 指定配置文件
--connect-timeout SECONDS 连接超时设置
-C, --continue-at OFFSET 断点续转
-b, --cookie STRING/FILE Cookies字符串或读取Cookies的文件位置 (H)
-c, --cookie-jar FILE 操作结束后,要写入 Cookies 的文件位置 (H)
--create-dirs 创建必要的本地目录层次结构
--crlf 在上传时将 LF 转写为 CRLF
--crlfile FILE 从指定的文件获得PEM格式CRL列表
-d, --data DATA HTTP POST 数据 (H)
--data-ascii DATA ASCII 编码 HTTP POST 数据 (H)
--data-binary DATA binary 编码 HTTP POST 数据 (H)
--data-urlencode DATA url 编码 HTTP POST 数据 (H)
--delegation STRING GSS-API 委托权限
--digest 使用数字身份验证 (H)
--disable-eprt 禁止使用 EPRT 或 LPRT (F)
--disable-epsv 禁止使用 EPSV (F)
-D, --dump-header FILE 将头信息写入指定的文件
--egd-file FILE 为随机数据设置EGD socket路径(SSL)
--engine ENGINGE 加密引擎 (SSL). "--engine list" 指定列表
-f, --fail 连接失败时不显示HTTP错误信息 (H)
-F, --form CONTENT 模拟 HTTP 表单数据提交(multipart POST) (H)
--form-string STRING 模拟 HTTP 表单数据提交 (H)
--ftp-account DATA 帐户数据提交 (F)
--ftp-alternative-to-user COMMAND 指定替换 "USER [name]" 的字符串 (F)
--ftp-create-dirs 如果不存在则创建远程目录 (F)
--ftp-method [MULTICWD/NOCWD/SINGLECWD] 控制 CWD (F)
--ftp-pasv 使用 PASV/EPSV 替换 PORT (F)
-P, --ftp-port ADR 使用指定 PORT 及地址替换 PASV (F)
--ftp-skip-pasv-ip 跳过 PASV 的IP地址 (F)
--ftp-pret 在 PASV 之前发送 PRET (drftpd) (F)
--ftp-ssl-ccc 在认证之后发送 CCC (F)
--ftp-ssl-ccc-mode ACTIVE/PASSIVE 设置 CCC 模式 (F)
--ftp-ssl-control ftp 登录时需要 SSL/TLS (F)
-G, --get 使用 HTTP GET 方法发送 -d 数据 (H)
-g, --globoff 禁用的 URL 队列 及范围使用 {} 和 []
-H, --header LINE 要发送到服务端的自定义请求头 (H)
-I, --head 仅显示响应文档头
-h, --help 显示帮助
-0, --http1.0 使用 HTTP 1.0 (H)
--ignore-content-length 忽略 HTTP Content-Length 头
-i, --include 在输出中包含协议头 (H/F)
-k, --insecure 允许连接到 SSL 站点,而不使用证书 (H)
--interface INTERFACE 指定网络接口/地址
-4, --ipv4 将域名解析为 IPv4 地址
-6, --ipv6 将域名解析为 IPv6 地址
-j, --junk-session-cookies 读取文件中但忽略会话cookie (H)
--keepalive-time SECONDS keepalive 包间隔
--key KEY 私钥文件名 (SSL/SSH)
--key-type TYPE 私钥文件类型 (DER/PEM/ENG) (SSL)
--krb LEVEL 启用指定安全级别的 Kerberos (F)
--libcurl FILE 命令的libcurl等价代码
--limit-rate RATE 限制传输速度
-l, --list-only 只列出FTP目录的名称 (F)
--local-port RANGE 强制使用的本地端口号
-L, --location 跟踪重定向 (H)
--location-trusted 类似 --location 并发送验证信息到其它主机 (H)
-M, --manual 显示全手动
--mail-from FROM 从这个地址发送邮件
--mail-rcpt TO 发送邮件到这个接收人(s)
--mail-auth AUTH 原始电子邮件的起始地址
--max-filesize BYTES 下载的最大文件大小 (H/F)
--max-redirs NUM 最大重定向数 (H)
-m, --max-time SECONDS 允许的最多传输时间
--metalink 处理指定的URL上的XML文件
--negotiate 使用 HTTP Negotiate 认证 (H)
-n, --netrc 必须从 .netrc 文件读取用户名和密码
--netrc-optional 使用 .netrc 或 URL; 将重写 -n 参数
--netrc-file FILE 设置要使用的 netrc 文件名
-N, --no-buffer 禁用输出流的缓存
--no-keepalive 禁用 connection 的 keepalive
--no-sessionid 禁止重复使用 SSL session-ID (SSL)
--noproxy 不使用代理的主机列表
--ntlm 使用 HTTP NTLM 认证 (H)
-o, --output FILE 将输出写入文件,而非 stdout
--pass PASS 传递给私钥的短语 (SSL/SSH)
--post301 在 301 重定向后不要切换为 GET 请求 (H)
--post302 在 302 重定向后不要切换为 GET 请求 (H)
--post303 在 303 重定向后不要切换为 GET 请求 (H)
-#, --progress-bar 以进度条显示传输进度
--proto PROTOCOLS 启用/禁用 指定的协议
--proto-redir PROTOCOLS 在重定向上 启用/禁用 指定的协议
-x, --proxy [PROTOCOL://]HOST[:PORT] 在指定的端口上使用代理
--proxy-anyauth 在代理上使用 "any" 认证方法 (H)
--proxy-basic 在代理上使用 Basic 认证 (H)
--proxy-digest 在代理上使用 Digest 认证 (H)
--proxy-negotiate 在代理上使用 Negotiate 认证 (H)
--proxy-ntlm 在代理上使用 NTLM 认证 (H)
-U, --proxy-user USER[:PASSWORD] 代理用户名及密码
--proxy1.0 HOST[:PORT] 在指定的端口上使用 HTTP/1.0 代理
-p, --proxytunnel 使用HTTP代理 (用于 CONNECT)
--pubkey KEY 公钥文件名 (SSH)
-Q, --quote CMD 在传输开始前向服务器发送命令 (F/SFTP)
--random-file FILE 读取随机数据的文件 (SSL)
-r, --range RANGE 仅检索范围内的字节
--raw 使用原始HTTP传输,而不使用编码 (H)
-e, --referer Referer URL (H)
-J, --remote-header-name 从远程文件读取头信息 (H)
-O, --remote-name 将输出写入远程文件
--remote-name-all 使用所有URL的远程文件名
-R, --remote-time 将远程文件的时间设置在本地输出上
-X, --request COMMAND 使用指定的请求命令
--resolve HOST:PORT:ADDRESS 将 HOST:PORT 强制解析到 ADDRESS
--retry NUM 出现问题时的重试次数
--retry-delay SECONDS 重试时的延时时长
--retry-max-time SECONDS 仅在指定时间段内重试
-S, --show-error 显示错误. 在选项 -s 中,当 curl 出现错误时将显示
-s, --silent Silent模式。不输出任务内容
--socks4 HOST[:PORT] 在指定的 host + port 上使用 SOCKS4 代理
--socks4a HOST[:PORT] 在指定的 host + port 上使用 SOCKSa 代理
--socks5 HOST[:PORT] 在指定的 host + port 上使用 SOCKS5 代理
--socks5-hostname HOST[:PORT] SOCKS5 代理,指定用户名、密码
--socks5-gssapi-service NAME 为gssapi使用SOCKS5代理服务名称
--socks5-gssapi-nec 与NEC Socks5服务器兼容
-Y, --speed-limit RATE 在指定限速时间之后停止传输
-y, --speed-time SECONDS 指定时间之后触发限速. 默认 30
--ssl 尝试 SSL/TLS (FTP, IMAP, POP3, SMTP)
--ssl-reqd 需要 SSL/TLS (FTP, IMAP, POP3, SMTP)
-2, --sslv2 使用 SSLv2 (SSL)
-3, --sslv3 使用 SSLv3 (SSL)
--ssl-allow-beast 允许的安全漏洞,提高互操作性(SSL)
--stderr FILE 重定向 stderr 的文件位置. - means stdout
--tcp-nodelay 使用 TCP_NODELAY 选项
-t, --telnet-option OPT=VAL 设置 telnet 选项
--tftp-blksize VALUE 设备 TFTP BLKSIZE 选项 (必须 >512)
-z, --time-cond TIME 基于时间条件的传输
-1, --tlsv1 使用 => TLSv1 (SSL)
--tlsv1.0 使用 TLSv1.0 (SSL)
--tlsv1.1 使用 TLSv1.1 (SSL)
--tlsv1.2 使用 TLSv1.2 (SSL)
--trace FILE 将 debug 信息写入指定的文件
--trace-ascii FILE 类似 --trace 但使用16进度输出
--trace-time 向 trace/verbose 输出添加时间戳
--tr-encoding 请求压缩传输编码 (H)
-T, --upload-file FILE 将文件传输(上传)到指定位置
--url URL 指定所使用的 URL
-B, --use-ascii 使用 ASCII/text 传输
-u, --user USER[:PASSWORD] 指定服务器认证用户名、密码
--tlsuser USER TLS 用户名
--tlspassword STRING TLS 密码
--tlsauthtype STRING TLS 认证类型 (默认 SRP)
--unix-socket FILE 通过这个 UNIX socket 域连接
-A, --user-agent STRING 要发送到服务器的 User-Agent (H)
-v, --verbose 显示详细操作信息
-V, --version 显示版本号并退出
-w, --write-out FORMAT 完成后输出什么
--xattr 将元数据存储在扩展文件属性中
-q .curlrc 如果作为第一个参数无效
文章参考: