Linuxcurl命令常⽤参数说明
准备⼯作
<?php
// curl-server.php 内容
print_r($_SERVER);
<?php
// 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)指定来源地址
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] => blog.csdn/xchenhao <=== 刚才命令所指定的信息
[REQUEST_TIME_FLOAT] => 1560263136.78
[REQUEST_TIME] => 1560263136
[argv] => Array
(
)
[argc] => 0
)
-f(或 --fail)失败时不显⽰ HTTP 错误
curl 127.0.0.1:7777/aaa.php # 没有该脚本
<!doctype html><html><head><title>404 Not Found</title><style>
body { background-color: #fcfcfc; color: #333333; margin: 0; padding:0; }
h1 { font-size: 1.5em; font-weight: normal; background-color: #9999cc; min-height:2em; line-height:2em; border-bottom: 1px inset black; margin: 0; }
h1, p { padding-left: 10px; }
code.url { background-color: #eeeeee; font-family:monospace; padding:0 2px;}
</style>
</head><body><h1>Not Found</h1><p>The requested resource <code class="url">/notfound.php</code> was not found on this server.</p></body></html >
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)把输出写到该⽂件中,保留远程⽂件的⽂件名
% 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 保存到⽂件中
# Netscape HTTP Cookie File
# curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
.baidu 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
curl是什么命令[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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论