89

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

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

 $result = curl($curl_exect) ;

Anyways, I would greatly appreciate any advice on how to view the full headers, thanks

Rick
  • 15,305
  • 33
  • 106
  • 160
  • Possible duplicate: http://stackoverflow.com/questions/866946/how-can-i-see-the-request-headers-made-by-curl-when-sending-a-request-to-the-ser –  Jan 30 '13 at 22:06

5 Answers5

169

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
Joseph Lust
  • 17,180
  • 7
  • 70
  • 72
  • 6
    How can one show the POST data as well? I.e. that added by curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); – Dan Dascalescu Nov 20 '13 at 10:09
  • @DanDascalescu Since you POSTed the data, when creating the Curl request, there is no need to capture it back. Note: POSTing form values are sent as "Form Data", not headers, hence why the above does not show them. If you really need to see what's on the wire, try WireShark. – Joseph Lust Nov 20 '13 at 19:31
84

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_HEADER in your 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.

Community
  • 1
  • 1
RobertPitt
  • 54,473
  • 20
  • 110
  • 156
  • 4
    I tried this but it just ouputs an array of the POST so its not showing the exact headers, only the POST and not exactly as the receiving server would see it so its not ideal for troubleshooting – Rick Jul 02 '10 at 15:57
  • 5
    The answer is not complete. Before the request is executed, you have to `curl_setopt($curl_exect, CURLINFO_HEADER_OUT, true);` – naitsirch Mar 09 '16 at 09:28
  • 1
    this only shows what the server sent back. it seems like curl_setopt($ch,CURLOPT_VERBOSE,true); will allow you to see what you send to the server. – Steven Teo May 19 '17 at 05:37
10

You can save all headers sent by curl to a file using :

$f = fopen('request.txt', 'w');
curl_setopt($ch,CURLOPT_VERBOSE,true);
curl_setopt($ch,CURLOPT_STDERR ,$f);
Nassim Aouragh
  • 194
  • 2
  • 6
7

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

Liutas
  • 4,743
  • 4
  • 19
  • 22
  • Thanks for the post, I guess I'm not fully understanding this as its a different library than curl, right? Is there a way to do this with just curl? If not, I will look into doing it this way, its just that I'm not familiar with fputs – Rick Jul 02 '10 at 16:11
  • This is actually native functionality of php – SSH This Apr 18 '12 at 21:31
  • 1
    This is a cool answer because it exposes kind of the raw request. I'd always been taught to use cURL which is a library that sometimes has to be installed and isn't always available. – Altimus Prime May 20 '19 at 01:52
2

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

Dan Dascalescu
  • 110,650
  • 40
  • 276
  • 363
greg0ire
  • 21,120
  • 15
  • 68
  • 95
  • 2
    WireShark runs on WinCap or another capturing systems, you have to have a atheros based chipset for it to work I think, why dont you just install chrome and press `Ctrl+Shift+J` then click the `XHR` Button and the file name and then see headers and data. – RobertPitt Jul 02 '10 at 10:07
  • I tried wireshark but it doesn't seem to pick up my outgoing Curl POST traffic – Rick Jul 02 '10 at 15:47
  • I assumed that you were working on a local server, is it the case? – greg0ire Jul 02 '10 at 16:01
  • Robert, I'm trying to do what you said in Chrome, but its not doing anything when I do Ctrl+Shift+3, I'm searching for info on this but can't seem to find something relevant, what feature of chrome is this accessing? Thanks – Rick Jul 02 '10 at 16:05
  • Ok, I found this regarding viewing the headers in Chrome: http://www.google.com/support/forum/p/Chrome/thread?tid=10d96c75638ff19f&hl=en I believe its Ctrl+Shift+i to access this – Rick Jul 02 '10 at 16:15
  • In Chrome, I tried that with the XHR button but its still not showing anything about my curl script, I'm running a local WAMP server so I'm not sure how to get it to pick this up... thanks for any advice – Rick Jul 02 '10 at 16:18
  • Do you see any traffic? If not check that you are monitoring the good network interface. If yes, perhaps you did not see the request. Use the filters to show only http traffic, to the host to which you are doing the request. – greg0ire Jul 02 '10 at 16:57
  • On wireshark? It shows traffic but not the normal POST traffic, I only have 1 network interface so I'm not sure what else can be done to make it show correctly – Rick Jul 02 '10 at 17:04
  • What do you mean by "normal"? When you refresh your page, do you get too much traffic? Can you filter out "abnormal" traffic? – greg0ire Jul 02 '10 at 17:19
  • Don't bother using Wireshark which is hard to understand, use Fiddler. It shows you the exact headers, post, etc – machineaddict Nov 18 '15 at 10:51