Home » Php » php – View cURL request headers complete with POST data

php – View cURL request headers complete with POST data

Posted by: admin July 12, 2020 Leave a comment

Questions:

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://

Answer:

.com POST /

Answer:

/

Answer:

HTTP/1.1 Host:

Answer:

.

Answer:

.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 /

Answer:

/

Answer:

HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 Host:

Answer:

.

Answer:

.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.

How to&Answers:

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.

Answer:

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