Linux系统的curl怎么用

发布时间:2022-02-19 11:22:29 作者:小新
来源:亿速云 阅读:206
# Linux系统的curl怎么用

## 一、curl简介

### 1.1 什么是curl
cURL(Client URL)是一个开源的命令行工具和库(libcurl),用于在终端中传输数据。它支持多种协议,包括:
- HTTP/HTTPS
- FTP/FTPS
- SCP/SFTP
- TFTP
- LDAP/LDAPS
- POP3/POP3S
- IMAP/IMAPS
- SMTP/SMTPS
- 等数十种协议

### 1.2 curl的发展历史
cURL项目始于1996年,由瑞典程序员Daniel Stenberg创建。最初命名为"httpget",后更名为"urlget",最终定名为"cURL"。

### 1.3 curl的主要特点
- 跨平台支持(Linux/Windows/macOS等)
- 支持HTTPS证书
- 支持HTTP POST/PUT等方法
- 支持文件上传/下载
- 支持cookie
- 支持代理
- 支持用户认证
- 支持断点续传

## 二、安装curl

### 2.1 检查是否已安装
```bash
curl --version

2.2 在不同Linux发行版中安装

Debian/Ubuntu

sudo apt update
sudo apt install curl

CentOS/RHEL

sudo yum install curl
# 或新版本
sudo dnf install curl

Arch Linux

sudo pacman -S curl

从源码编译安装

wget https://curl.se/download/curl-7.88.1.tar.gz
tar -xzvf curl-7.88.1.tar.gz
cd curl-7.88.1
./configure
make
sudo make install

三、curl基础用法

3.1 基本GET请求

curl https://example.com

3.2 保存输出到文件

curl -o output.html https://example.com
# 或
curl https://example.com > output.html

3.3 显示响应头信息

curl -I https://example.com

3.4 显示请求和响应头

curl -i https://example.com

3.5 跟随重定向

curl -L https://example.com

3.6 限制重定向次数

curl -L --max-redirs 3 https://example.com

四、高级HTTP请求

4.1 发送POST请求

curl -X POST -d "name=value" https://example.com/api

4.2 发送JSON数据

curl -X POST -H "Content-Type: application/json" \
-d '{"key1":"value1", "key2":"value2"}' \
https://example.com/api

4.3 发送表单数据

curl -X POST -F "username=user" -F "password=pass" \
https://example.com/login

4.4 上传文件

curl -X POST -F "file=@/path/to/file.jpg" \
https://example.com/upload

4.5 发送PUT请求

curl -X PUT -d "data=value" https://example.com/api/resource

4.6 发送DELETE请求

curl -X DELETE https://example.com/api/resource/123

五、HTTP认证

5.1 基本认证

curl -u username:password https://example.com

5.2 摘要认证

curl --digest -u username:password https://example.com

5.3 Bearer Token认证

curl -H "Authorization: Bearer token_string" \
https://example.com/api

5.4 OAuth 2.0

curl -H "Authorization: OAUTH-TOKEN" \
https://example.com/api

六、处理HTTPS

6.1 忽略证书验证(不推荐生产环境使用)

curl -k https://example.com

6.2 指定CA证书

curl --cacert /path/to/ca-bundle.crt https://example.com

6.3 使用客户端证书

curl --cert /path/to/client.crt --key /path/to/client.key \
https://example.com

七、使用代理

7.1 HTTP代理

curl -x http://proxy.example.com:8080 https://target.example.com

7.2 SOCKS代理

curl --socks5 proxy.example.com:1080 https://target.example.com

7.3 代理认证

curl -x http://user:pass@proxy.example.com:8080 \
https://target.example.com

八、cookie管理

8.1 保存cookie到文件

curl -c cookies.txt https://example.com

8.2 从文件加载cookie

curl -b cookies.txt https://example.com

8.3 直接发送cookie

curl -b "name=value" https://example.com

九、调试和故障排除

9.1 显示详细输出

curl -v https://example.com

9.2 显示更详细输出

curl --trace-ascii debug.txt https://example.com

9.3 限制连接超时

curl --connect-timeout 30 https://example.com

9.4 限制整个请求超时

curl -m 60 https://example.com

9.5 显示速度统计

curl -w "%{speed_download}\n" -o /dev/null https://example.com

十、高级功能

10.1 断点续传

curl -C - -O https://example.com/largefile.zip

10.2 多文件下载

curl -O https://example.com/file1.txt -O https://example.com/file2.txt

10.3 FTP操作

# 下载
curl -u ftpuser:ftppass -O ftp://ftp.example.com/file.txt

# 上传
curl -T localfile.txt -u ftpuser:ftppass ftp://ftp.example.com/

10.4 发送自定义HTTP头

curl -H "X-Custom-Header: value" https://example.com

10.5 速率限制

curl --limit-rate 200K https://example.com/largefile.iso

十一、curl与API交互示例

11.1 GitHub API示例

curl -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/users/octocat

11.2 Twitter API示例

curl -X GET "https://api.twitter.com/2/tweets/search/recent?query=curl" \
-H "Authorization: Bearer $TWITTER_BEARER_TOKEN"

11.3 Open API示例

curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPEN_API_KEY" \
-d '{"model": "text-davinci-003", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 7}'

十二、curl与shell脚本集成

12.1 解析JSON响应

response=$(curl -s https://api.example.com/data)
value=$(echo $response | jq -r '.key')

12.2 处理HTTP状态码

status_code=$(curl -s -o /dev/null -w "%{http_code}" https://example.com)
if [ $status_code -eq 200 ]; then
    echo "请求成功"
else
    echo "请求失败,状态码: $status_code"
fi

12.3 循环请求

for i in {1..10}; do
    curl -X POST -d "value=$i" https://example.com/api
done

十三、性能优化技巧

13.1 使用HTTP/2

curl --http2 https://example.com

13.2 启用压缩

curl --compressed https://example.com

13.3 保持连接

curl --keepalive-time 60 https://example.com

13.4 并行请求(使用xargs)

cat urls.txt | xargs -n1 -P4 curl -O

十四、安全注意事项

14.1 避免在命令行中暴露敏感信息

# 不推荐
curl -u username:password https://example.com

# 推荐
curl -u username https://example.com
# 然后交互式输入密码

14.2 使用.netrc文件存储凭证

# ~/.netrc 内容
machine example.com
login username
password secret

14.3 定期更新curl版本

# 检查已知漏洞
curl --version

十五、常见问题解答

15.1 如何解决”SSL certificate problem”错误?

# 方法1:更新CA证书
sudo apt update ca-certificates  # Debian/Ubuntu
sudo yum update ca-certificates  # CentOS/RHEL

# 方法2:指定证书路径
curl --cacert /path/to/cert.pem https://example.com

15.2 如何解决”Connection refused”错误?

15.3 如何解决”Timeout was reached”错误?

# 增加超时时间
curl --connect-timeout 60 -m 300 https://example.com

15.4 如何解决”Too many redirects”错误?

# 限制重定向次数
curl -L --max-redirs 5 https://example.com

十六、总结

cURL是Linux系统中功能强大且灵活的网络工具,几乎可以处理所有基于URL的数据传输需求。通过掌握cURL的各种选项和参数,您可以: - 测试和调试Web服务 - 自动化API交互 - 下载/上传文件 - 监控网站可用性 - 集成到shell脚本中

建议通过man curl查看完整手册,或访问官方文档获取更多信息。 “`

推荐阅读:
  1. 关于用php的curl批量抓取内容
  2. PHP的curl_multi函数怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux curl

上一篇:Linux系统的cp命令怎么用

下一篇:Linux窗口管理器Screen怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》