Linux curl 命令常用参数说明

准备工作


// curl-server.php 内容

print_r($_SERVER);

// req.php 内容

print_r([
  'POST' => $_POST,
  'GET' => $_GET,
]);
vim curl-server.php  # 复制上述代码
vim req.php  # 复制上述代码
php -S 0.0.0.0:7777 # 启动服务器

参数说明

主要参数

参数 作用
-A/–user-agent 指定 User-Agent 信息
-b/–cookie 指定 cookie
-D/–dump-header 将 header 信息写入文件中
-e/–referer 指定来源地址
-f/–fail 失败时不显示 HTTP 错误
-o/–ouput 将结果输出到文件中
-O/–remote-name 把输出写到该文件中,保留远程文件的文件名
-c/–cookie-jar 将响应中的 Cookie 保存到文件中
-H/–header 自定义头信息
-d/–data 以 POST 方式请求(默认以 GET 请求:-G/–get)
-X/–request 以指定方法请求(GET/POST/PUT/DELETE/HEAD 等)
-I/–head 只显示文档信息
-i/–include 输出时包括 protocol 头信息
-V/–version 显示版本信息
-v/–verbose 显示请求过程的详细信息
-w/–write-out 按指定格式输出
-h/–help 查看命令帮助(或 man curl)

详细说明

  • -A (或 --user-agent)指定 User-Agent 信息

curl -A ‘Hey~~~’ 127.0.0.1:7777/curl-server.php

Array
(
    [DOCUMENT_ROOT] => /Users/js/test
    [REMOTE_ADDR] => 127.0.0.1
    [REMOTE_PORT] => 52672
    [SERVER_SOFTWARE] => PHP 5.6.33 Development Server
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_NAME] => 0.0.0.0
    [SERVER_PORT] => 7777
    [REQUEST_URI] => /curl-server.php
    [REQUEST_METHOD] => GET
    [SCRIPT_NAME] => /curl-server.php
    [SCRIPT_FILENAME] => /Users/js/test/curl-server.php
    [PHP_SELF] => /curl-server.php
    [HTTP_HOST] => 127.0.0.1:7777
    [HTTP_USER_AGENT] => Hey~~~                           <=== 刚才命令所指定的信息
    [HTTP_ACCEPT] => */*
    [REQUEST_TIME_FLOAT] => 1560261885.06
    [REQUEST_TIME] => 1560261885
    [argv] => Array
        (
        )
    [argc] => 0
)
  • -b(或 --cooke)指定 Cookie

curl -b ‘token=123’ 127.0.0.1:7777/curl-server.php

Array
(
    [DOCUMENT_ROOT] => /Users/js/test
    [REMOTE_ADDR] => 127.0.0.1
    [REMOTE_PORT] => 53944
    [SERVER_SOFTWARE] => PHP 5.6.33 Development Server
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_NAME] => 0.0.0.0
    [SERVER_PORT] => 7777
    [REQUEST_URI] => /curl-server.php
    [REQUEST_METHOD] => GET
    [SCRIPT_NAME] => /curl-server.php
    [SCRIPT_FILENAME] => /Users/js/test/curl-server.php
    [PHP_SELF] => /curl-server.php
    [HTTP_HOST] => 127.0.0.1:7777
    [HTTP_USER_AGENT] => curl/7.54.0
    [HTTP_ACCEPT] => */*
    [HTTP_COOKIE] => token=123                              <=== 刚才命令所指定的信息
    [REQUEST_TIME_FLOAT] => 1560262194.51
    [REQUEST_TIME] => 1560262194
    [argv] => Array
        (
        )
    [argc] => 0
)
  • -D(或 --dump-header)将 header 信息写入到文件中

curl -D ‘./header.log’ 127.0.0.1:7777/curl-server.php
cat ./header.log

HTTP/1.1 200 OK
Host: 127.0.0.1:7777
Connection: close
X-Powered-By: PHP/5.6.33
Content-type: text/html; charset=UTF-8
  • -e(或 --referer)指定来源地址

curl -e ‘https://blog.csdn.net/xchenhao’ 127.0.0.1:7777/curl-server.php

Array
(
    [DOCUMENT_ROOT] => /Users/js/test
    [REMOTE_ADDR] => 127.0.0.1
    [REMOTE_PORT] => 57791
    [SERVER_SOFTWARE] => PHP 5.6.33 Development Server
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_NAME] => 0.0.0.0
    [SERVER_PORT] => 7777
    [REQUEST_URI] => /curl-server.php
    [REQUEST_METHOD] => GET
    [SCRIPT_NAME] => /curl-server.php
    [SCRIPT_FILENAME] => /Users/js/test/curl-server.php
    [PHP_SELF] => /curl-server.php
    [HTTP_HOST] => 127.0.0.1:7777
    [HTTP_USER_AGENT] => curl/7.54.0
    [HTTP_ACCEPT] => */*
    [HTTP_REFERER] => https://blog.csdn.net/xchenhao         <=== 刚才命令所指定的信息
    [REQUEST_TIME_FLOAT] => 1560263136.78
    [REQUEST_TIME] => 1560263136
    [argv] => Array
        (
        )

    [argc] => 0
)
  • -f(或 --fail)失败时不显示 HTTP 错误

curl 127.0.0.1:7777/aaa.php # 没有该脚本

404 Not Found

Not Found

The requested resource /notfound.php was not found on this server.

curl -f 127.0.0.1:7777/aaa.php # 添加 -f 参数后

curl: (22) The requested URL returned error: 404 Not Found
  • -o(或 --output)将结果输出到文件中

curl -o ‘./result.log’ 127.0.0.1:7777/curl-server.php

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   682    0   682    0     0  92249      0 --:--:-- --:--:-- --:--:-- 97428

cat result.log

Array
(
    [DOCUMENT_ROOT] => /Users/js/test
    [REMOTE_ADDR] => 127.0.0.1
    [REMOTE_PORT] => 59291
    [SERVER_SOFTWARE] => PHP 5.6.33 Development Server
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_NAME] => 0.0.0.0
    [SERVER_PORT] => 7777
    [REQUEST_URI] => /curl-server.php
    [REQUEST_METHOD] => GET
    [SCRIPT_NAME] => /curl-server.php
    [SCRIPT_FILENAME] => /Users/js/test/curl-server.php
    [PHP_SELF] => /curl-server.php
    [HTTP_HOST] => 127.0.0.1:7777
    [HTTP_USER_AGENT] => curl/7.54.0
    [HTTP_ACCEPT] => */*
    [REQUEST_TIME_FLOAT] => 1560263509.3
    [REQUEST_TIME] => 1560263509
    [argv] => Array
        (
        )
    [argc] => 0
)
  • -O(或 --remote-name)把输出写到该文件中,保留远程文件的文件名

curl -O https://www.baidu.com/favicon.ico

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16958  100 16958    0     0   334k      0 --:--:-- --:--:-- --:--:--  331k

ls favicon.ico

favicon.ico
  • -c(–cookie-jar)将响应中的 Cookie 保存到文件中

curl -c cookie.log https://www.baidu.com
cat cookie.log

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

.baidu.com      TRUE    /       FALSE   1560351411      BDORZ   27315
  • -H(–header)自定义头信息

curl -H ‘Key: Value’ -H ‘Hello: World’ 127.0.0.1:7777/curl-server.php

Array
(
    [DOCUMENT_ROOT] => /Users/js/test
    [REMOTE_ADDR] => 127.0.0.1
    [REMOTE_PORT] => 52216
    [SERVER_SOFTWARE] => PHP 5.6.33 Development Server
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_NAME] => 0.0.0.0
    [SERVER_PORT] => 7777
    [REQUEST_URI] => /curl-server.php
    [REQUEST_METHOD] => GET
    [SCRIPT_NAME] => /curl-server.php
    [SCRIPT_FILENAME] => /Users/js/test/curl-server.php
    [PHP_SELF] => /curl-server.php
    [HTTP_HOST] => 127.0.0.1:7777
    [HTTP_USER_AGENT] => curl/7.54.0
    [HTTP_ACCEPT] => */*
    [HTTP_KEY] => Value            <=== 刚才命令所指定的信息
    [HTTP_HELLO] => World          <=== 刚才命令所指定的信息
    [REQUEST_TIME_FLOAT] => 1560265768.02
    [REQUEST_TIME] => 1560265768
    [argv] => Array
        (
        )

    [argc] => 0
)
  • -d(或 --data)以 POST 方式请求

curl -d ‘name=xchenhao’ 127.0.0.1:7777/req.php

Array
(
    [POST] => Array
        (
            [name] => xchenhao
        )

    [GET] => Array
        (
        )
)
  • -X(或 --request)以指定方法请求

curl -v -X DELETE 127.0.0.1:7777/curl-server.php

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 7777 (#0)
> DELETE /curl-server.php HTTP/1.1    <==== 请求方式为 DELETE
> Host: 127.0.0.1:7777
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Host: 127.0.0.1:7777
< Connection: close
< X-Powered-By: PHP/5.6.33
< Content-type: text/html; charset=UTF-8
< 
Array
(
    [DOCUMENT_ROOT] => /Users/js/test
    [REMOTE_ADDR] => 127.0.0.1
    [REMOTE_PORT] => 55027
    [SERVER_SOFTWARE] => PHP 5.6.33 Development Server
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_NAME] => 0.0.0.0
    [SERVER_PORT] => 7777
    [REQUEST_URI] => /curl-server.php
    [REQUEST_METHOD] => DELETE
    [SCRIPT_NAME] => /curl-server.php
    [SCRIPT_FILENAME] => /Users/js/test/curl-server.php
    [PHP_SELF] => /curl-server.php
    [HTTP_HOST] => 127.0.0.1:7777
    [HTTP_USER_AGENT] => curl/7.54.0
    [HTTP_ACCEPT] => */*
    [REQUEST_TIME_FLOAT] => 1560270505.46
    [REQUEST_TIME] => 1560270505
    [argv] => Array
        (
        )
    [argc] => 0
)
  • -I(或 --head)只显示文档信息

curl -I 127.0.0.1:7777/curl-server.php

HTTP/1.1 200 OK
Host: 127.0.0.1:7777
Connection: close
X-Powered-By: PHP/5.6.33
Content-type: text/html; charset=UTF-8
  • -i(或 --include) 输出时包括 protocol 头信息

curl -i 127.0.0.1:7777/curl-server.php

HTTP/1.1 200 OK
Host: 127.0.0.1:7777
Connection: close
X-Powered-By: PHP/5.6.33
Content-type: text/html; charset=UTF-8

Array
(
    [DOCUMENT_ROOT] => /Users/js/test
    [REMOTE_ADDR] => 127.0.0.1
    [REMOTE_PORT] => 51869
    [SERVER_SOFTWARE] => PHP 5.6.33 Development Server
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_NAME] => 0.0.0.0
    [SERVER_PORT] => 7777
    [REQUEST_URI] => /curl-server.php
    [REQUEST_METHOD] => GET
    [SCRIPT_NAME] => /curl-server.php
    [SCRIPT_FILENAME] => /Users/js/test/curl-server.php
    [PHP_SELF] => /curl-server.php
    [HTTP_HOST] => 127.0.0.1:7777
    [HTTP_USER_AGENT] => curl/7.54.0
    [HTTP_ACCEPT] => */*
    [REQUEST_TIME_FLOAT] => 1560269744.36
    [REQUEST_TIME] => 1560269744
    [argv] => Array
        (
        )

    [argc] => 0
)
  • -V(或 --version)显示版本信息

curl -V

curl 7.54.0 (x86_64-apple-darwin18.0) libcurl/7.54.0 LibreSSL/2.6.5 zlib/1.2.11 nghttp2/1.24.1
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz HTTP2 UnixSockets HTTPS-proxy
  • -v(或 --verbose)显示请求过程的详细信息

curl -v -I 127.0.0.1:7777/curl-server.php

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 7777 (#0)
> HEAD /curl-server.php HTTP/1.1
> Host: 127.0.0.1:7777
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Host: 127.0.0.1:7777
Host: 127.0.0.1:7777
< Connection: close
Connection: close
< X-Powered-By: PHP/5.6.33
X-Powered-By: PHP/5.6.33
< Content-type: text/html; charset=UTF-8
Content-type: text/html; charset=UTF-8

< 
* Closing connection 0
  • -w(或 --write-out)按指定格式输出信息

curl -s -w %{http_code} -o /dev/null 127.0.0.1:7777/curl-server.php

200

参考

  • https://www.cnblogs.com/duhuo/p/5695256.html Linux curl命令详解
  • https://blog.csdn.net/b1303110335/article/details/78213281 curl模拟delete/put/post/get请求
  • https://blog.csdn.net/weifangan/article/details/80741981 CURL -w 参数详解

编辑记录

06/12/2019 00:05 周三 创建

你可能感兴趣的:(Linux curl 命令常用参数说明)