Tuesday, April 5, 2011

Emulating HTTP POST using command line curl and exporting output to text file

How do I emulate an HTTP POST request using curl and capturing the result on a text file? I already have a script called dump.php:

<?php
  $var = print_r($GLOBALS, true);
  $fp = fopen('raw-post.txt','w');
  fputs($fp,$var);
  fclose($fp);
?>

I did a simple test by doing:

curl -d 'echo=hello' http://localhost/dump.php

but I didn't see the data I dumped in the output file. I was expecting it to appear in one of the POST arrays but it's empty.

[_POST] => Array
    (
    )

[HTTP_POST_VARS] => Array
    (
    )
From stackoverflow
  • You need to use $_GLOBALS rather than $GLOBALS.

    Additionally, you can do this instead of using output buffering:

    $var = print_r($_GLOBALS, true);
    

    Providing the true as a second parameter to print_r will return the result rather than automatically printing it.

    Francis : Change it to $_GLOBALS but got no output
  • If you're just trying to capture POST data, then do something like this for you dump.php file.

    <?php
        $data = print_r($_POST, true);
        $fp = fopen('raw-post.txt','w');
        fwrite($fp, $data);
        fclose($fp);
    ?>
    

    All POST data is stored in the $_POST variable. Additionally, if you need GET data as well, $_REQUEST will hold both.

    Francis : Replaced $_GLOBALS with $_REQUEST and the output is: Array ( )
    Francis : $_GLOBALS would have included both _POST and _GET arrays but it contains nothing when I do the curl command above.
  • Remove tick marks (') from the curl command line:

    curl -d hello=world -d test=yes http://localhost/dump.php
    

0 comments:

Post a Comment