Skip to content

命令行下 CURL 的一些常用的参数

如下是 CURl 命令行的官方介绍,可以处理各种网络协议,这篇文章主要介绍 CURL 处理 HTTP 协议。

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, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.

php
<?php
// 设置 cookie
setcookie('test', 'hello world!');

参数-c可以将服务器获取的 cookie 写入到文件 cookie.jar,cat cookie.jar可以查看 cookie 的内容,结果是一种结构化的文件存储,一行一个 cookie 键值对。

phantom:~ ydl$ curl http://localhost:9999/b.php -v -c cookie.jar
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9999 (#0)
> GET /b.php HTTP/1.1
> Host: localhost:9999
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Host: localhost:9999
< Date: Tue, 15 Dec 2020 14:12:37 GMT
< Connection: close
< X-Powered-By: PHP/7.3.24
* Added cookie test="hello+world%21" for domain localhost, path /, expire 0
< Set-Cookie: test=hello+world%21
< Content-type: text/html; charset=UTF-8
<
* Closing connection 0
phantom:~ ydl$ cat cookie.jar
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

localhost	FALSE	/	FALSE	0	test	hello+world%21
php
<?php
// 打印 cookie
var_dump($_COOKIE);

可以看到请求的 header 里已经带上了 cookie:test=hello+world%21,用 PHP 程序打印 cookie,可以看到 test 的值。

phantom:~ ydl$ curl http://localhost:9999/d.php -b cookie.jar -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9999 (#0)
> GET /d.php HTTP/1.1
> Host: localhost:9999
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie: test=hello+world%21
>
< HTTP/1.1 200 OK
< Host: localhost:9999
< Date: Tue, 15 Dec 2020 14:17:28 GMT
< Connection: close
< X-Powered-By: PHP/7.3.24
< Content-type: text/html; charset=UTF-8
<
array(1) {
  ["test"]=>
  string(12) "hello world!"
}
* Closing connection 0

-d POST data 参数

PHP 程序打印服务器获取的 POST 值

php
<?php
var_dump($_POST);
root@debian:/tmp# curl -XPOST localhost:9912/a.php -d 'a=123&b=123'
array(2) {
  ["a"]=>
  string(3) "123"
  ["b"]=>
  string(3) "123"
}

@以文件的方式 POST 数据

bash
root@debian:/tmp# curl -XPOST localhost:9912/a.php -d @c.txt
array(2) {
  ["a"]=>
  string(3) "123"
  [123]=>
  string(3) "123"
}
# 查看POST数据的内容
root@debian:/tmp# cat c.txt
a=123&123=123

-s 静默模式

如下命令是不带 s 的,会打印出一些请求进度信息,耗时、下载速度等,加上-s参数就不会出现这些数据了

curl localhost:9912/c.php -o htllo.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 50017    0 50017    0     0  47.6M      0 --:--:-- --:--:-- --:--:-- 47.6M

如何提取如上参数?

--write-out FORMAT Use output FORMAT after completion 如下是一个查看http_code状态码200的案例

bash
curl -s -w '%{http_code}' http://localhost:9999/d.php -o /dev/null
200

-H 请求头参数,冒号:分隔

如下请求会在请求头中带上两个参数Content-typeUser-AgentUA是我们自定义的test

root@debian:/tmp# curl -H 'User-Agent:test' -H 'Content-type:application/json' localhost:9912/c.php -v -o /dev/null
* Expire in 0 ms for 6 (transfer 0x5653689e3a90)
* Expire in 1 ms for 1 (transfer 0x5653689e3a90)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Expire in 0 ms for 1 (transfer 0x5653689e3a90)
*   Trying ::1...
* TCP_NODELAY set
* Expire in 150000 ms for 3 (transfer 0x5653689e3a90)
* Expire in 200 ms for 4 (transfer 0x5653689e3a90)
* Connected to localhost (::1) port 9912 (#0)
> GET /c.php HTTP/1.1
> Host: localhost:9912
> Accept: */*
> User-Agent:test
> Content-type:application/json
>
< HTTP/1.1 200 OK
< Host: localhost:9912
< Date: Tue, 15 Dec 2020 10:51:30 GMT
< Connection: close
< X-Powered-By: PHP/7.3.21-1+0~20200807.66+debian10~1.gbp18a1c2
< Content-type: text/html; charset=UTF-8
<
{ [50015 bytes data]
100 50017    0 50017    0     0  47.6M      0 --:--:-- --:--:-- --:--:-- 47.6M
* Closing connection 0

-v (verbose) 调试模式,会将请求的头部信息,服务器返回的 Response 打印出来

案例如上

-F 文件上传

php
<?php
// 打印上传文件的信息
var_dump($_FILES);

@标记这是一个文件

bash
phantom:~ ydl$ curl -XPOST http://localhost:9999/f.php -F 'file=@f.php'
array(1) {
  ["file"]=>
  array(5) {
    ["name"]=>
    string(5) "f.php"
    ["type"]=>
    string(24) "application/octet-stream"
    ["tmp_name"]=>
    string(66) "/private/var/folders/1y/_xzwnncj0wz8wyssg1lz9k8h0000gn/T/phpHG6kth"
    ["error"]=>
    int(0)
    ["size"]=>
    int(26)
  }
}

-o 将相应结果保存到文件中

bash
# 保存到文件file.resp中
curl -XPOST http://localhost:9999/f.php -F 'file=@f.php' -o file.resp

# 保存到黑洞中,如果你不想看到结果下行
curl -XPOST http://localhost:9999/f.php -F 'file=@f.php' -o /dev/null

更多细节

输入命令man curl翻阅手册,可以看到更多的参数细节,熟能生巧,时不时翻阅 CURL 手册,可以发现更多的使用技巧,便利日常开发,会发现 CURL 更多强大的功能。