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

Just insert the following lines at the beginning 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 does the trick (you may decide if you want to use $_GET or $_POST or, like I needed it, both.

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

php yourscript.php 'arg1=x&arg2=y'

Leave a Comment