简体   繁体   English

显示 Curl POST 请求标头? 有没有办法做到这一点?

[英]Show Curl POST Request Headers? Is there a way to do this?

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无论如何,我将不胜感激有关如何查看完整标题的任何建议,谢谢

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

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查看: http : //www.php.net/manual/en/function.curl-getinfo.php

You can also use the CURLOPT_HEADER in your curl_setopt您还可以在curl_setopt使用CURLOPT_HEADER

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.这些只是使用标题的一些方法。

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);

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就像写在如何提出请求上一样

I had exactly the same problem lately, and I installed Wireshark (it is a network monitoring tool).我最近遇到了完全相同的问题,我安装了 Wireshark(它是一个网络监控工具)。 You can see everything with this, except encrypted traffic (HTTPS).除了加密流量 (HTTPS),您可以看到所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM