8
Windows 7 x64
PHP 7.2.2 x64

I am trying to view a simple request payload so I have created a PHP file per https://docstore.mik.ua/orelly/webprog/pcook/ch11_07.htm

<?php
$c = curl_init('https://www.google.com');
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, 'monkey=uncle&rhino=aunt');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);

but I get absolutely no output unlike the example which shows that I can expect something like:

* Connected to www.example.com (10.1.1.1)
> POST /submit.php HTTP/1.1
Host: www.example.com
Pragma: no-cache
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Content-Length: 23
Content-Type: application/x-www-form-urlencoded

monkey=uncle&rhino=aunt* Connection #0 left intact
* Closing connection #0

$page contains a Google 404 page so I know the request is succeeding.

Does anyone know if there are other settings which I need to address or if I am doing something blatantly wrong? In particular, I am interested in the body of the payload.

Yes, I am aware that curl'ing Google isn't particularly useful; I am trying to get this simple example to work so that I can debug an API interaction which is mysteriously failing.

Updates, per comment request

I cannot do curl -v but print_r( curl_version() ); produces:

Array
(
    [version_number] => 473344
    [age] => 4
    [features] => 2428829
    [ssl_version_number] => 0
    [version] => 7.57.0
    [host] => x86_64-pc-win32
    [ssl_version] => OpenSSL/1.1.0g
    [libz_version] => 1.2.11
    [protocols] => Array
        (
            [0] => dict
            [1] => file
            [2] => ftp
            [3] => ftps
            [4] => gopher
            [5] => http
            [6] => https
            [7] => imap
            [8] => imaps
            [9] => ldap
            [10] => pop3
            [11] => pop3s
            [12] => rtsp
            [13] => scp
            [14] => sftp
            [15] => smb
            [16] => smbs
            [17] => smtp
            [18] => smtps
            [19] => telnet
            [20] => tftp
        )

)
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • Works ok for me, what happens if you `curl -v ...` from the command line? – Alex Howansky Apr 04 '18 at 19:26
  • @AlexHowansky I've updated my question with your request. – MonkeyZeus Apr 04 '18 at 19:46
  • `-v` isn't version, it's verbose, for the command line tool. I was just wondering if you could get verbose output (and thus, an actual good request) from the same machine via the shell. – Alex Howansky Apr 04 '18 at 19:47
  • This says output goes to stderr https://stackoverflow.com/questions/3757071/php-debugging-curl - so have a look wherever your PHP is sending stderr, such as the web server's error log if you're calling this via a web server. Or set a specific file location to capture it with `CURLOPT_STDERR` – Michael Berkowski Apr 04 '18 at 19:51
  • @AlexHowansky I believe Windows PHP uses `php_curl.dll` from the `ext` folder. Are you suggesting that this implies there is a `curl.exe` somewhere on my system? From the `cmd` I get `'curl' is not recognized as an internal or external command...` – MonkeyZeus Apr 04 '18 at 19:55
  • @MichaelBerkowski I will take a look at my logs and see if the output is being sent there. I will report back shortly. – MonkeyZeus Apr 04 '18 at 19:56
  • Oops my bad, didn't notice the Windows tag. – Alex Howansky Apr 04 '18 at 19:56
  • @AlexHowansky No worries. I assume it would work if I used a dedicated executable/binary but I'm trying to figure out why PHP won't "just work" as expected. I tagged `Windows` in the question's tags :) – MonkeyZeus Apr 04 '18 at 19:59
  • @MichaelBerkowski So, PHP was not writing the verbose info to any of my log files: PHP nor Apache and I even checked the event viewer for the heck of it. Adding `curl_setopt($c, CURLOPT_STDERR, fopen('/curl.txt', 'w+'));` does successfully write to the `/curl.txt` file, fun... I have one more issue now though. The payload body, `monkey=uncle&rhino=aunt`, is not recorded in the log, I just get `* Connection #0 to host www.google.com left intact` at the end and that is it. – MonkeyZeus Apr 04 '18 at 20:12
  • @MonkeyZeus I'm afraid I have no idea why the POST body isn't there. I've never worked with the verbose output in php. – Michael Berkowski Apr 04 '18 at 20:22
  • @MichaelBerkowski Oh ok, well thank you for all of your help. – MonkeyZeus Apr 05 '18 at 11:31

1 Answers1

11

tl;dr

CURLOPT_STDERR must be set to something specific.

Setting curl_setopt($c, CURLOPT_STDERR, fopen('/curl.txt', 'w+')); fixed my issue.


As it turns out curl_setopt($c, CURLOPT_VERBOSE, 1); is not printing the output to STDERR for some reason which I have not uncovered. I did not find the output in any of my PHP, Apache, nor Event Viewer logs.

After setting curl_setopt($c, CURLOPT_STDERR, fopen('/curl.txt', 'w+'));, I was able to see the output in the curl.txt file.

I am not sure if this is specific to Windows environments.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77