查看带有 POST 数据的完整 cURL 请求标头

发布于 2024-11-27 13:04:34 字数 1562 浏览 1 评论 0原文

如何在 php 中使用 libcurl 查看完整的请求标头,包括发布数据

我正在尝试模拟页面的发布,当从浏览器完成并在实时 HTTP 标头中查看时,该页面看起来像这样:

https://###.com
POST /###/### HTTP/1.1
Host: ###.###.com
...snipped normal looking headers...
Content-Type: multipart/form-data; boundary=---------------------------28001808731060
Content-Length: 697
-----------------------------28001808731060
Content-Disposition: form-data; name="file_data"; filename="stats.csv"
Content-Type: text/csv
id,stats_id,scope_id,stat_begin,stat_end,value
61281,1,4,2011-01-01 00:00:00,2011-12-31 23:59:59,0
-----------------------------28001808731060
Content-Disposition: form-data; name="-save"
Submit
-----------------------------28001808731060--

所以我们很好地看到我正在上传的文件,它的内容,一切都在那里。但是,当我尝试从 php 发出相同的帖子(使用 CURLOPT_VERBOSECURLINFO_HEADER_OUT)时,我所有从 cURL 获取数据的尝试都显示缺少帖子数据的请求标头,像这样:

POST /###/### HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Host: ###.###.com
...snipped normal-looking headers...
Content-Length: 697
Content-Type: multipart/form-data; boundary=----------------------------e90de36c15f5

根据这里的内容长度,看起来一切进展顺利,但它确实有助于我的调试工作,以便能够看到完整的请求。我也很恼火,这很难,我应该能够看到整个事情;我知道我一定错过了什么。

--- 编辑 ---

我正在寻找的相当于 this

curl --trace-ascii debugdump.txt http://www.example.com/

这似乎可以通过 libcurl 中的选项 CURLOPT_DEBUGFUNCTION 获得,但没有在 php 中实现。嘘。

How can I view the full request headers, including post data, using libcurl in php?

I am trying to simulate the post of a page, which when done from a browser and viewed in Live HTTP Headers looks like this:

https://###.com
POST /###/### HTTP/1.1
Host: ###.###.com
...snipped normal looking headers...
Content-Type: multipart/form-data; boundary=---------------------------28001808731060
Content-Length: 697
-----------------------------28001808731060
Content-Disposition: form-data; name="file_data"; filename="stats.csv"
Content-Type: text/csv
id,stats_id,scope_id,stat_begin,stat_end,value
61281,1,4,2011-01-01 00:00:00,2011-12-31 23:59:59,0
-----------------------------28001808731060
Content-Disposition: form-data; name="-save"
Submit
-----------------------------28001808731060--

So we nicely see the file I'm uploading, it's content, everything's there. But all my attempts at getting data out of cURL when I try to make the same post from php (using CURLOPT_VERBOSE, or CURLINFO_HEADER_OUT) show request headers that lack the post data, like so:

POST /###/### HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Host: ###.###.com
...snipped normal-looking headers...
Content-Length: 697
Content-Type: multipart/form-data; boundary=----------------------------e90de36c15f5

Based on the Content-Length here, it appears things are going well, but it would really help my debugging efforts to be able to see the complete request. I am also irked that it is difficult, I should be able to see the whole thing; I know I must be missing something.

--- EDIT ---

What I'm looking for is the equivalent of this:

curl --trace-ascii debugdump.txt http://www.example.com/

which seems to be available with the option CURLOPT_DEBUGFUNCTION in libcurl, but isn't implemented in php. Boo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2024-12-04 13:04:34

我确实需要这样做,但我需要测试与银行的通信。

在这种情况下,使用 Fiddler2 非常容易,启用 HTTPS 流量解密,并让 cURL 使用 Fiddler2 作为调试代理:

$proxy = '127.0.0.1:8888';
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

I had a need to do precisely this, but I needed to test communication with a bank.

It is extremely easy to use Fiddler2, enable HTTPS traffic decryption, and have cURL use Fiddler2 as a proxy for debugging in this situation:

$proxy = '127.0.0.1:8888';
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
绿萝 2024-12-04 13:04:34

您正在发送multipart/formdata。我猜 cURL 基本上完整地显示了 HTTP 标头。 “问题”是 multipart/formdata 由多个部分组成。这超出了“第一级 HTTP 标头”,并且是“主要 HTTP 正文”正文的一部分。

我不知道你的环境,但你也可以使用 TCP 流量监控进行调试。为此,您可以使用 Wiresharktcpdump - Wireshark 也可以显示 tcpdump 创建的转储文件。

You are sending multipart/formdata. cURL basically shows the HTTP header completely I guess. The "problem" is that multipart/formdata consist of multiple parts. This is beyond "first level HTTP headers" and part of the body of the "main HTTP body".

I don't know your environment, but you can debug using TCP traffic monitoring as well. For this, you can use Wireshark or tcpdump - Wireshark can as well show dump files created by tcpdump.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文