如何查看PHP cURL请求体(如标题的CURLINFO_HEADER_OUT)

时间:2015-12-18 03:31:51

标签: php curl

我可以使用以下命令查看使用php curl发送的请求的标头:

curl_getinfo($ch, CURLINFO_HEADER_OUT);

我希望看到正在发送的内容,但不能为我的生活找到任何方法。

4 个答案:

答案 0 :(得分:1)

在广泛搜索PHP cURL文档之后,我无法找到任何此类选项。

我的解决方案是使用网络代理工具Charles

  

Charles是一个HTTP代理/ HTTP监视器/反向代理,使开发人员能够查看其计算机和Internet之间的所有HTTP和SSL / HTTPS流量。这包括请求,响应和HTTP标头(包含cookie和缓存信息)。

答案 1 :(得分:0)

路由器.php

<?=file_get_contents('php://input');

启动嵌入式服务器

php -S 127.0.0.0:8080 ./router.php

测试卷曲:

<?php

$ch = curl_init("http://127.0.0.1:8080/");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  "Content-Type: application/json",
  "Expect: 100-Continue"
]);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

$fields = ["bri" => 255];
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode((object)$fields));

$response = curl_exec($ch);

print curl_getinfo($ch, CURLINFO_HEADER_OUT);
print $response;

答案 2 :(得分:-2)

将CURLOPT_HEADER设置为true将返回带有响应正文的标题,然后可以将其从响应中解析出来并单独处理。类似下面的内容应该可以打印输入和输出标题:

$url = "https://www.example.com";
$ch = curl_init();

// Configure cURL handle
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);

$x = curl_exec($ch);

print "\nHeaders:\n";

// Get the out headers, explode into an array, and remove any empty string entries
$outHeaders = explode("\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
$outHeaders = array_filter($outHeaders, function($value) { return $value !== '' && $value !== ' ' && strlen($value) != 1; });
print_r($outHeaders);

// Seperate in headers from body of response
list($inHeaders, $content) = explode("\r\n\r\n", $x, 2);

// Break in headers into array and print_r them
$inHeaders = explode("\n", $inHeaders);
print_r($inHeaders);

答案 3 :(得分:-3)

尝试此功能:

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}