Send request parameters when invoking the command line PHP script through the command line

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 do the same through the command line without rewriting the script to accept argv instead of $_REQUEST . Is there a way to do something like this:

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

so that the displayed parameters are displayed in the $_REQUEST variable?

+6
source share
4 answers

No, there is no easy way to achieve this. The web server will split the query string and pass it to the PHP interpreter, which will then store it in the $_REQUEST .

If you run from the command line and want to accept similar parameters, you will have to disassemble them yourself. The command line has a completely different syntax for passing parameters than HTTP. You might want to learn getopt .

For brute force approach that doesn't take user errors 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 as follows:

 $ php test.php --param1=val1 --param2=val2 param1 = val1 param2 = val2 
+5
source

If you do not want to change the startup script, you can specify options using the -B option to specify the code to run before the input file. But in this case, you should also add the -F tag to indicate your input file:

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

I cannot take advantage of this, but I accepted this in my bootstrap file:

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

After executing the 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 searches.

+2
source

I wrote a short function to handle this situation - if the command line arguments are present and the $ _REQUEST array is empty (i.e. when you run the script from the command line and not through the 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, eg PHP .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; } } } 

An example is given in a comment on a function presented in a comment on a function:

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

which gives an output:

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

Source: https://habr.com/ru/post/947749/


All Articles