Is it possible to pass an array as a command line

2019-02-08 04:39发布

I'm maintaining a PHP library that is responsible for fetching and storing incoming data (POST, GET, command line arguments, etc). I've just fixed a bug that would not allow it to fetch array variables from POST and GET and I'm wondering whether this is also applicable to the part that deals with the command line.

Can you pass an array as a command line argument to PHP?

9条回答
叼着烟拽天下
2楼-- · 2019-02-08 05:28

In case, if you are executing command line script with arguments through code then the best thing is to base encode it -

base64_encode(json_encode($arr));

while sending and decode it while receiving in other script.

json_decode(base64_decode($argv[1]));

That will also solve the issue of json receiving without quotes around the keys and values. Because without quotes, it is considered to be as bad json and you will not be able to decode that.

查看更多
Evening l夕情丶
3楼-- · 2019-02-08 05:36

After following the set of instructions below you can make a call like this:

phpcl yourscript.php _GET='{ "key1": "val1", "key2": "val2" }'

To get this working you need code to execute before the script being called. I use a bash shell on linux and in my .bashrc file I set the command line interface to make the php ini flag auto_prepend_file load my command line bootstrap file (this file should be found somewhere in your php_include_path):

alias phpcl='php -d auto_prepend_file="system/bootstrap/command_line.php"'

This means that each call from the command line will execute this file before running the script that you call. auto_prepend_file is a great way to bootstrap your system, I use it in my standard php.ini to set my final exception and error handlers at a system level. Setting this command line auto_prepend_file overrides my normal setting and I choose to just handle command line arguments so that I can set $_GET or $_POST. Here is the file I prepend:

<?php
// Parse the variables given to a command line script as Query Strings of JSON.
// Variables can be passed as separate arguments or as part of a query string:
//    _GET='{ "key1": "val1", "key2": "val2" }' foo='"bar"'
// OR
//    _GET='{ "key1": "val1", "key2": "val2" }'\&foo='"bar"' 
if ($argc > 1)
{
   $parsedArgs = array(); 

   for ($i = 1; $i < $argc; $i++)
   {
      parse_str($argv[$i], $parsedArgs[$i]);
   }

   foreach ($parsedArgs as $arg)
   {
      foreach ($arg as $key => $val)
      {
         // Set the global variable of name $key to the json decoded value.
         $$key = json_decode($val, true);
      }
   }

   unset($parsedArgs);
}
?>

It loops through all arguments passed and sets global variables using variable variables (note the $$). The manual page does say that variable variables doesn't work with superglobals, but it seems to work for me with $_GET (I'm guessing it works with POST too). I choose to pass the values in as JSON. The return value of json_decode will be NULL on error, you should do error checking on the decode if you need it.

查看更多
贼婆χ
4楼-- · 2019-02-08 05:37

The following code block will do it passing the array as a set of comma separated values:

<?php
  $i_array = explode(',',$argv[1]);
  var_dump($i_array);
?>

OUTPUT:

php ./array_play.php 1,2,3

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}
查看更多
登录 后发表回答