By Matthew


2010-09-21 03:18:53 8 Comments

I'd like to see what the post fields in the request are before I send it. (For debugging purposes).

The PHP library (class) I am using is already made (not by me), so I am trying to understand it.

As far as I can tell, it uses curl_setopt() to set different options like headers and such and then it uses curl_exec() to send the request.

Ideas on how to see what post fields are being sent?

6 comments

@mario 2018-12-23 20:59:17

Another (crude) option is to utilize netcat for dumping the full request:

nc -l -p 8000 -w 3 | tee curldbg.txt

And of course sending the failing request to it:

curl_setup(CURLOPT_URL, "http://localhost/testytest");

Notably that will always hang+fail, since netcat won't ever construct a valid HTTP response. It's really just for inspecting what really got sent. The better option, of course, is using a http request debugging service.

@netom 2010-09-21 12:35:35

You can enable the CURLOPT_VERBOSE option:

curl_setopt($curlhandle, CURLOPT_VERBOSE, true);

When CURLOPT_VERBOSE is set, output is written to STDERR or the file specified using CURLOPT_STDERR. The output is very informative.

You can also use tcpdump or wireshark to watch the network traffic.

@Mike 2014-12-29 18:59:11

It is worth noting that this will not work if you have set CURLINFO_HEADER_OUT to TRUE. So far as I can tell...

@Andrew 2017-02-10 11:25:22

To just get the info of a CURL request do this:

$response = curl_exec($ch);

$info = curl_getinfo($ch);
var_dump($info);

@TurqSpl 2018-02-28 19:12:34

For "after" response the curl_getinfo its excelent but for malformed urls for example the getinfo returns non info, the opt CURLOPT_VERBOSE its a better for example

@Michaël R 2016-02-12 13:17:08

Here is an even simplier way, by writing directly to php error output

curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, fopen('php://stderr', 'w'));

@hakre 2013-01-21 10:41:42

You can enable the CURLOPT_VERBOSE option and log that information to a (temporary) CURLOPT_STDERR:

// CURLOPT_VERBOSE: TRUE to output verbose information. Writes output to STDERR, 
// or the file specified using CURLOPT_STDERR.
curl_setopt($handle, CURLOPT_VERBOSE, true);

$verbose = fopen('php://temp', 'w+');
curl_setopt($handle, CURLOPT_STDERR, $verbose);

You can then read it after curl has done the request:

$result = curl_exec($handle);
if ($result === FALSE) {
    printf("cUrl error (#%d): %s<br>\n", curl_errno($handle),
           htmlspecialchars(curl_error($handle)));
}

rewind($verbose);
$verboseLog = stream_get_contents($verbose);

echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";

(I originally answered similar but more extended in a related question.)

More information like metrics about the last request is available via curl_getinfo. This information can be useful for debugging curl requests, too. A usage example, I would normally wrap that into a function:

$version = curl_version();
extract(curl_getinfo($handle));
$metrics = <<<EOD
URL....: $url
Code...: $http_code ($redirect_count redirect(s) in $redirect_time secs)
Content: $content_type Size: $download_content_length (Own: $size_download) Filetime: $filetime
Time...: $total_time Start @ $starttransfer_time (DNS: $namelookup_time Connect: $connect_time Request: $pretransfer_time)
Speed..: Down: $speed_download (avg.) Up: $speed_upload (avg.)
Curl...: v{$version['version']}
EOD;

@Vignesh 2015-07-16 14:29:34

great answer +1

@iisisrael 2015-08-13 16:49:02

fopen option should be 'w+'

@hakre 2015-08-14 07:39:08

@iisisrael: You are right. Corrected. Thanks for the hint.

@Andrew Odendaal 2015-12-02 15:18:11

works well, thanks! +1

@Hengky Mulyono 2018-02-17 21:58:00

save my life thanks!

@TheSatinKnight 2018-02-28 21:28:01

Before you jump into debugging, however: Try checking the apache log in case an error was tossed (like curl php not being installed, so the function failed!)

@JoeSlav 2019-03-04 16:55:08

@Mario S 2014-10-28 15:10:03

Here is a simpler code for the same:

   curl_setopt($ch, CURLOPT_VERBOSE, 1);
   curl_setopt($ch, CURLOPT_STDERR, $fp);

where $fp is a file handle to output errors. For example:

   $fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');

( Read on http://curl.haxx.se/mail/curlphp-2008-03/0064.html )

@Jono 2018-11-25 13:49:45

I would have liked to use the answer of @Michaël-R- but I didnt get verbose in the PHP log. Logging to a new file like this worked tho.

Related Questions

Sponsored Content

17 Answered Questions

7 Answered Questions

[SOLVED] Getting only response header from HTTP POST using curl

  • 2012-04-08 03:12:04
  • Jonathan Allard
  • 428136 View
  • 466 Score
  • 7 Answer
  • Tags:   post curl http-headers

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

35 Answered Questions

[SOLVED] How do I debug Node.js applications?

35 Answered Questions

[SOLVED] PHP: Delete an element from an array

  • 2008-12-15 20:28:55
  • Ben
  • 2191889 View
  • 2187 Score
  • 35 Answer
  • Tags:   php arrays

32 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

15 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara Uchiha
  • 198017 View
  • 2337 Score
  • 15 Answer
  • Tags:   php mysql database

8 Answered Questions

[SOLVED] How to send a header using a HTTP request through a curl call?

  • 2008-12-10 16:38:57
  • gagneet
  • 1293158 View
  • 1200 Score
  • 8 Answer
  • Tags:   curl http-headers

1 Answered Questions

[SOLVED] RAW POST using cURL in PHP

  • 2009-05-16 01:28:27
  • The Unknown
  • 141269 View
  • 107 Score
  • 1 Answer
  • Tags:   php post curl put

Sponsored Content