postcommand-line-interfacephphttp-post-vars

How can I pass parameters from the command line to $_POST in a PHP script?


I know this could sound a little weird, but I need to pass some parameters to the $_POST array. Similar to the way Apache does it, or any other web server.

Unfortunately I couldn't find libapache2-mod-php5 anywhere for my Ubuntu installation.


Solution

  • That's not easily doable. You can invoke the php-cgi binary and pipe a fake POST request in. But you'll need to set up a whole lot of CGI environment variables:

    echo 'var1=123&var2=abc' | REQUEST_METHOD=POST  SCRIPT_FILENAME=script.php REDIRECT_STATUS=CGI CONTENT_TYPE=application/www-form-urlencoded php-cgi 
    

    Note: Insufficient, doesn't work like that. But something like that...


    It's certainly easier if you just patch the script, and let it load the $_POST array from a predefined environment variable.

    $_POST = parse_url($_SERVER["_POST"]);
    

    Then you can invoke it like _POST=var=123 php script.php for simplicity.