[ACCEPTED]-How can I pass parameters from the command line to $_POST in a PHP script?-http-post-vars

Accepted answer
Score: 65

Just insert the following lines at the beginning 5 of your script:

/* If started from the command line, wrap parameters to $_POST and $_GET */
if (!isset($_SERVER["HTTP_HOST"])) {
  parse_str($argv[1], $_GET);
  parse_str($argv[1], $_POST);

This small piece of code 4 does the trick (you may decide if you want 3 to use $_GET or $_POST or, like I needed it, both.

After 2 changing your script, you can call it from 1 the command line passing your arguments:

php yourscript.php 'arg1=x&arg2=y'
Score: 19

That's not easily doable. You can invoke 8 the php-cgi binary and pipe a fake POST request 7 in. But you'll need to set up a whole lot 6 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 5 work like that. But something like that...

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

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

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

Score: 0
curl --data "name=ii" "param1=value1&param2=value2" http://test.com/sample.php


More Related questions