php 显示 Curl POST 请求标头?有没有办法做到这一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3164405/
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
Show Curl POST Request Headers? Is there a way to do this?
提问by Rick
I'm building a Curl web automation app and am having some issue with not getting the desired outcome of my POST action, I am having some trouble figuring out how I can show the full POST request I am sending over (with headers), I have been searching on this but everything that comes up is the response headers, actually I want these too but also the request, which none of the posts I find on google seem to mention..
我正在构建一个 Curl Web 自动化应用程序,但在无法获得我的 POST 操作的预期结果时遇到了一些问题,我在弄清楚如何显示我发送的完整 POST 请求(带有标题)时遇到了一些麻烦,我一直在搜索这个,但出现的一切都是响应头,实际上我也想要这些,但也想要请求,我在谷歌上找到的帖子似乎都没有提到..
I know I can display the result of a curl request using something like this (forgive me if my syntax is off, I already shut down my virtual machine with my ide and code to refer to
我知道我可以使用这样的东西来显示 curl 请求的结果(如果我的语法关闭,请原谅我,我已经用我的 ide 和代码关闭了我的虚拟机以参考
$result = curl($curl_exect) ;
Anyways, I would greatly appreciate any advice on how to view the full headers, thanks
无论如何,我将不胜感激有关如何查看完整标题的任何建议,谢谢
采纳答案by RobertPitt
You can see the information regarding the transfer by doing:
您可以通过执行以下操作来查看有关传输的信息:
curl_setopt($curl_exect, CURLINFO_HEADER_OUT, true);
before the request, and
在请求之前,和
$information = curl_getinfo($curl_exect);
after the request
请求后
View: http://www.php.net/manual/en/function.curl-getinfo.php
You can also use the CURLOPT_HEADERin your curl_setopt
你也可以CURLOPT_HEADER在你的curl_setopt
curl_setopt($curl_exect, CURLOPT_HEADER, true);
$httpcode = curl_getinfo($c, CURLINFO_HTTP_CODE);
return $httpcode == 200;
These are just some methods of using the headers.
这些只是使用标题的一些方法。
回答by Joseph Lust
Here is all you need:
这是您需要的一切:
curl_setopt($curlHandle, CURLINFO_HEADER_OUT, true); // enable tracking
... // do curl request
$headerSent = curl_getinfo($curlHandle, CURLINFO_HEADER_OUT ); // request headers
回答by Nassim Aouragh
You can save all headers sent by curl to a file using :
您可以使用以下命令将 curl 发送的所有标头保存到文件中:
$f = fopen('request.txt', 'w');
curl_setopt($ch,CURLOPT_VERBOSE,true);
curl_setopt($ch,CURLOPT_STDERR ,$f);
回答by Liutas
You can make you request headers by yourself using:
您可以使用以下方法自行请求标头:
// open a socket connection on port 80
$fp = fsockopen($host, 80);
// send the request headers:
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Referer: $referer\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
// close the socket connection:
fclose($fp);
Like writen on how make request
就像写在如何提出请求上一样
回答by greg0ire
I had exactly the same problem lately, and I installed Wireshark (it is a network monitoring tool). You can see everything with this, except encrypted traffic (HTTPS).
我最近遇到了完全相同的问题,我安装了 Wireshark(它是一个网络监控工具)。除了加密流量 (HTTPS),您可以看到所有内容。

