查看带有 POST 数据的完整 cURL 请求标头
如何在 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_VERBOSE
或 CURLINFO_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确实需要这样做,但我需要测试与银行的通信。
在这种情况下,使用 Fiddler2 非常容易,启用 HTTPS 流量解密,并让 cURL 使用 Fiddler2 作为调试代理:
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:
您正在发送multipart/formdata。我猜 cURL 基本上完整地显示了 HTTP 标头。 “问题”是 multipart/formdata 由多个部分组成。这超出了“第一级 HTTP 标头”,并且是“主要 HTTP 正文”正文的一部分。
我不知道你的环境,但你也可以使用 TCP 流量监控进行调试。为此,您可以使用 Wireshark 或 tcpdump - 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.