当我向服务器发送请求时,我想看到curl所做的请求头。我要怎么检查呢?


当前回答

这个问题没有指定是指名为curl的命令行命令还是整个curl库。

下面使用cURL库的PHP代码使用第一个参数作为HTTP方法(例如:"GET", "POST", "OPTIONS")和第二个参数作为URL。

<?php
$ch = curl_init();
$f = tmpfile(); # will be automatically removed after fclose()
curl_setopt_array($ch, array(
    CURLOPT_CUSTOMREQUEST  => $argv[1],
    CURLOPT_URL            => $argv[2], 
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 0,
    CURLOPT_VERBOSE        => 1,
    CURLOPT_HEADER         => 0,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT        => 30,
    CURLOPT_STDERR         => $f,
));
$response = curl_exec($ch);
fseek($f, 0);
echo fread($f, 32*1024); # output up to 32 KB cURL verbose log
fclose($f);
curl_close($ch);
echo $response;

使用示例:

php curl-test.php OPTIONS https://google.com

注意,结果与下面的命令行几乎相同

curl -v -s -o - -X OPTIONS https://google.com

其他回答

您可以使用wireshark或tcpdump来查看任何网络流量(http也可以)。

你可以通过使用"-"作为文件名直接将头文件转储到标准输出,例如查看头文件和内容,并遵循重定向,所有这些都在一个输出中:

curl -L -D - -s [url]

唯一的方法,我设法看到我的外向头(卷曲与php)是使用以下选项:

curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

获取调试信息:

$data = curl_exec($ch);
var_dump($data);
var_dump(curl_getinfo($ch));

如下所示的命令将显示三个部分:请求头、响应头和数据(由CRLF分隔)。它避免了curl添加的技术信息和语法噪声。

curl -vs www.stackoverflow.com 2>&1 | sed '/^* /d; /bytes data]$/d; s/> //; s/< //'

该命令将产生如下输出:

GET / HTTP/1.1
Host: www.stackoverflow.com
User-Agent: curl/7.54.0
Accept: */*

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: https://stackoverflow.com/
Content-Length: 149
Accept-Ranges: bytes
Date: Wed, 16 Jan 2019 20:28:56 GMT
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: cache-bma1622-BMA
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1547670537.588756,VS0,VE105
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=e4b211f7-ae13-dad3-9720-167742a5dff8; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="https://stackoverflow.com/">here</a></body>

描述:

-vs -添加报头(-v)但删除进度条(-s) 2>&1 -将stdout和stderr合并为单个stdout 使用下面的命令由curl生成的Sed编辑响应 /^* /d -删除以“*”开头的行(技术信息) $/d -删除以'bytes data]结尾的行(技术信息) S /> // -删除'> '前缀 S /< // -删除'< '前缀

向https://http-tools.appspot.com/reflect-http-request/some-unique-id发送一个示例请求,并通过其对应的查找器url https://http-tools.appspot.com/reflect-http-request-finder/some-unique-id检查该请求包含什么(请求头、请求体、请求参数)。你可以使用任何字符串而不是某个唯一的id,查看https://http-tools.appspot.com了解更多细节。