Home » Php » Send request parameters when calling a PHP script via command line

Send request parameters when calling a PHP script via command line

Posted by: admin July 12, 2020 Leave a comment

Questions:

When you run a PHP script through a browser it looks something like

http://somewebsite.com/yourscript?param1=val1&param2=val2.

I am trying to achieve the same thing via command line without having to rewrite the script to accept argv instead of $_REQUEST. Is there a way to do something like this:

php yourscript.php?param1=val1&param2=val2 

such that the parameters you send show up in the $_REQUEST variable?

How to&Answers:

No, there is no easy way to achieve that. The web server will split up the request string and pass it into the PHP interpreter, who will then store it in the $_REQUEST array.

If you run from the command line and you want to accept similar parameters, you’ll have to parse them yourself. The command line has completely different syntax for passing parameters than HTTP has. You might want to look into getopt.

For a brute force approach that doesn’t take user error into account, you can try this snippet:

<?php
foreach( $argv as $argument ) {
        if( $argument == $argv[ 0 ] ) continue;

        $pair = explode( "=", $argument );
        $variableName = substr( $pair[ 0 ], 2 );
        $variableValue = $pair[ 1 ];
        echo $variableName . " = " . $variableValue . "\n";
        // Optionally store the variable in $_REQUEST
        $_REQUEST[ $variableName ] = $variableValue;
}

Use it like this:

$ php test.php --param1=val1 --param2=val2
param1 = val1
param2 = val2

Answer:

In case you don’t want to modify running script, you can specify parameters using in -B parameter to specify code to run before the input file. But in this case you must also add -F tag to specify your input file:

php -B "$_REQUEST = array('param1' => 'val1', 'param2' => 'val2');" -F yourscript.php

Answer:

I can’t take credit for this but I adopted this in my bootstrap file:

// Concatenate and parse string into $_REQUEST
if (php_sapi_name() === 'cli') {
    parse_str(implode('&', array_slice($argv, 1)), $_REQUEST);
}

Upon executing a PHP file from the command line:

php yourscript.php param1=val1 param2=val2

The above will insert the keys and values into $_REQUEST for later retrieval.

Answer:

I wrote a short function to handle this situation — if command line arguments are present and the $_REQUEST array is empty (ie, when you’re running a script from the command line instead of though a web interface), it looks for command line arguments in key=value pairs,

Argv2Request($argv);

print_r($_REQUEST);

function Argv2Request($argv) {
    /*
      When $_REQUEST is empty and $argv is defined,
      interpret $argv[1]...$argv[n] as key => value pairs
      and load them into the $_REQUEST array

      This allows the php command line to subsitute for GET/POST values, e.g.
      php script.php animal=fish color=red number=1 has_car=true has_star=false
     */


    if ($argv !== NULL && sizeof($_REQUEST) == 0) {
        $argv0 = array_shift($argv); // first arg is different and is not needed

        foreach ($argv as $pair) {
            list ($k, $v) = split("=", $pair);
            $_REQUEST[$k] = $v;
        }
    }
}

The sample input suggested in the function’s comment is:

php script.php animal=fish color=red number=1 has_car=true has_star=false

which yields the output:

Array
(
    [animal] => fish
    [color] => red
    [number] => 1
    [has_car] => true
    [has_star] => false
)