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


当前回答

唯一的方法,我设法看到我的外向头(卷曲与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 /< // -删除'< '前缀

这个问题没有指定是指名为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

将报头转储到一个文件中,将响应的有效负载转储到另一个文件中

curl -k -v -u user:pass  "url" --trace-ascii headers.txt >> response.txt

下面是我的http客户端在php使post查询包含cookie:

function http_login_client($url, $params = "", $cookies_send = "" ){

    // Vars
    $cookies = array();
    $headers = getallheaders();

    // Perform a http post request to $ur1 using $params
    $ch = curl_init($url);
    $options = array(   CURLOPT_POST => 1,
                        CURLINFO_HEADER_OUT => true,
                        CURLOPT_POSTFIELDS => $params,
                        CURLOPT_RETURNTRANSFER => 1,
                        CURLOPT_HEADER => 1,
                        CURLOPT_COOKIE => $cookies_send,
                        CURLOPT_USERAGENT => $headers['User-Agent']
                    );

    curl_setopt_array($ch, $options);

    $response = curl_exec($ch);

///调试信息 echo $响应; var_dump (curl_getinfo (ch)美元); ///

    // Parse response and read cookies
    preg_match_all('/^Set-Cookie: (.*?)=(.*?);/m', $response, $matches);

    // Build an array with cookies
    foreach( $matches[1] as $index => $cookie )
        $cookies[$cookie] = $matches[2][$index];

    return $cookies;
} // end http_login_client

我认为curl -verbose/-v是最简单的。它将输出请求头(以'>'为前缀的行),而无需写入文件:

$ curl -v -I -H "Testing: Test header so you see this works" http://stackoverflow.com/
* About to connect() to stackoverflow.com port 80 (#0)
*   Trying 69.59.196.211... connected
* Connected to stackoverflow.com (69.59.196.211) port 80 (#0)
> HEAD / HTTP/1.1
> User-Agent: curl/7.16.3 (i686-pc-cygwin) libcurl/7.16.3 OpenSSL/0.9.8h zlib/1.2.3 libssh2/0.15-CVS
> Host: stackoverflow.com
> Accept: */*
> Testing: Test header so you see this works
>
< HTTP/1.0 200 OK
...