2

my problem is to create a simple php script that I want to call with params from command line or browser:

if(!empty($_GET)) {
    $parameters = $_GET;
} else if(isset($argv)) {
    $parameters = $argv;
}
var_dump($parameters);

this gives different result :

in browser : http://localhost/test/script.php?test=123

array(1) { ["test"]=> string(3) "123" }

in shell : php script.php test=123

array(2) {
  [0]=>
  string(10) "script.php"
  [1]=>
    string(8) "test=123"
}

I want to call the params by their names.

1
  • Look at argv's documentation and you will understand why the outputs are different. To generate the same output, you'll need to iterate over argv[1, ...], retrieve the different parameters, cut them on the operator '=' and insert the left operand as a key and the right operand as the value of this key in an associative array Commented Jul 29, 2015 at 12:55

5 Answers 5

2

You can achieve this using getopt (or getlongopt) (see same manual link)

array getopt ( string $options [, array $longopts ] )

Sign up to request clarification or add additional context in comments.

Comments

2

The idiomatic syntax for the command line is:

program -o val --opt value pos_value

Such syntax can be parsed in PHP using getopt.

$options = getopt("f:hp:");
var_dump($options);

Comments

1

If you are going to pass all parameters via the CLI in query string format (e.g. test=123&var=me) take a look at parse_str().

if(!empty($_GET)) {
    $parameters = $_GET;
} else if(!empty($argv[1])) {
    $parameters = parse_str($argv[1]);
}

If there are separated, then deceze has the answer.

Comments

1

The argv parameter's first element will always be the name of the script that has been executed. You can simply ignore that first argument and start from index 1.

Taken from the relevant documentation:

Note: The first argument $argv[0] is always the name that was used to run the script.

As others have mentioned, you can also use the getopt function that is specifically used to extract options, their names, and their valies from a CLI. The argv variable simply holds a list of arguments that were passed. Since the actual program that is being executed is the PHP interpreter, even the script name is an argument.

php my_script.php another_arg more_args
//  ^ argv[0].....^ argv[1]...^ argv[2]

As a side note, this behavior of argv including the executing script file is consistent across other platforms/technologies. For example, the same behavior can be seen with , , and many many more.

General explanation taken from this page

What is ARGV?
As a concept, ARGV is a convention in programming that goes back (at least) to the C language. It refers to the “argument vector,” which is basically a variable that contains the arguments passed to a program through the command line. It typically manifests as an array and can be manipulated in that way, meaning you can reference specific arguments by index or you can iterate through them in the standard ways.

Comments

1

I found that I cann call my script with php-cgi instead of php; CLI : php-cgi script.php test=123 and Browser : http://localhost/test/script.php?test=123 give the same result that I want. Thank you all

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.