PHP、curl 和原始标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1828935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:58:24  来源:igfitidea点击:

PHP, curl, and raw headers

phpcurl

提问by Alan Storm

When using the PHP curl functions, is there anyway to see the exact rawheaders that curl is sendingto the server?

使用 PHP curl 函数时,是否可以查看curl发送到服务器的确切原始标头?

回答by Greg

You can use curl_getinfo:

您可以使用curl_getinfo

Before the call

通话前

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

After

$headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);

回答by angelfilm entertainment

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_exec($ch);
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
?>

Only available in php 5.1.3 http://php.net/manual/en/function.curl-getinfo.php

仅适用于 php 5.1.3 http://php.net/manual/en/function.curl-getinfo.php



You can verify that they are the same by using your console and hitting

您可以通过使用控制台并点击来验证它们是否相同

curl http://example.com/-I

curl http://example.com/-I

or

或者

curl --trace-ascii /file.txt http://example.com/

curl --trace-ascii /file.txt http://example.com/

回答by Daniel Stenberg

AFAIK, the PHP/CURL binding still lacks proper support for CURLOPT_DEBUGFUNCTION which is a callback from libcurl that can provide all those details.

AFAIK,PHP/CURL 绑定仍然缺乏对 CURLOPT_DEBUGFUNCTION 的适当支持,这是来自 libcurl 的回调,可以提供所有这些细节。

That's the primary reason why I recommend people to work out HTTP scripting things with the curl command line tool and its --trace-ascii option FIRST, then translate that into a PHP function.

这就是我建议人们首先使用 curl 命令行工具及其 --trace-ascii 选项来编写 HTTP 脚本的主要原因,然后将其转换为 PHP 函数。

回答by jlb

be sure to set the CURLINFO_HEADER_OUT option before making the curl_getinfo call

确保在调用 curl_getinfo 之前设置 CURLINFO_HEADER_OUT 选项

curl_setopt($c, CURLINFO_HEADER_OUT, true);

curl_setopt($c, CURLINFO_HEADER_OUT, true);