Bash call php script with parameters

I have done a a bash script which run php script. It works fine without parameters but when I add parameters (id and url), there are some errors:

PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf                                                                                        .d/mcrypt.ini on line 1 in Unknown on line 0
Could not open input file: /var/www/dev/dbinsert/script/automatisation.php?                                                                                        id=1

I run php script from the bash like this:

php /var/www/dev/dbinsert/script/automatisation.php?id=19&url=http://bkjbezjnkelnkz.com

asked Jul 21, 2011 at 16:36

Call it as:

php /path/to/script/script.php -- 'id=19&url=http://bkjbezjnkelnkz.com'

Also, modify your PHP script to use parse_str():

parse_str($argv[1]);

If the index $_SERVER['REMOTE_ADDR'] isn't set.


More advanced handling may need getopt(), but parse_str() is a quick'n'dirty way to get it working.

Zuul

16.1k6 gold badges59 silver badges88 bronze badges

answered Jul 21, 2011 at 16:47

3

You can't pass GET query parameters to the PHP command line interface. Either pass the arguments as standard command line arguments and use the $argc and $argv globals to read them, or (if you must use GET/POST parameters) call the script through curl/wget and pass the parameters that way – assuming you have the script accessible through a local web server.

This is how you can pass arguments to be read by $argc and $argv (the -- indicates that all subsequent arguments should go to the script and not to the PHP interpreter binary):

php myfile.php -- argument1 argument2

answered Jul 21, 2011 at 16:54

1

-- Option 1: php-cgi --

Use 'php-cgi' in place of 'php' to run your script. This is the simplest way as you won't need to specially modify your php code to work with it:

php-cgi -f /my/script/file.php id=19 myvar=xyz

-- Option 2: if you have a web server --

If the php file is on a web server you can use 'wget' on the command line:

wget 'http://localhost/my/script/file.php?id=19&myvar=xyz'

OR:

wget -q -O - "http://localhost/my/script/file.php?id=19&myvar=xyz"

-- Accessing the variables in php --

In both option 1 & 2 you access these parameters like this:

$id = $_GET["id"];
$myvar = $_GET["myvar"];

answered Apr 22, 2015 at 2:11

BastionBastion

71110 silver badges9 bronze badges

0

I've recently received some comments on a previous article I wrote about scheduling PHP scripts using Cron that were asking how to pass parameters to the script. In this article I look at a number of different methods this can be done.

Note that if register_argc_argv is disabled (set to false) in php.ini, these methods will not work.

First lets see what will 100% not work. If you're used to passing command line parameters to a PHP script in a web browser using a query parameter (and then looking it up using $_GET), you may be trying to do something like this...

Of course PHP will promptly reject this with an error like this...

The reason that happens is because PHP is looking for a file called "script.php?param1=value1" but the script is actually called "script.php". The file system is not a web server so it doesn't know how to handle query parameters. A different approach is required.

Lets look at the 'classic' method to pass command line parameters to a script - using $argc and $argv.

Here's a PHP script that displays all of the command line arguments that were passed to it...

To pass command line arguments to the script, we simply put them right after the script name like so...

The output produced is...

Note that the 0th argument is the name of the PHP script that is run. The rest of the array are the values passed in on the command line. The values are accessed via the $argv array. This approach works, but it is very simplistic and doesn't play well if you're looking to transition from a query parameter way of passing in values to your script. With this approach there is no way to give names the the command line arguments being passed in.

If you want to be able to assign variable names for the values being passed in, getopt() is the way to do it. Lets look at a different version of the script now...

Lets run this script like this...

The output produced is...

There are some major differences here. First with getopt() you must specify which command line argument you want to retrieve. In the case of this script, it looks for the "-p" argument, that's specified by the "p:" value passed to getopt(). The colon (:) means that the parameter must have a value. If you're used to doing something like "script.php?p=value1" this is an equivalent for the command line.

It's possible to pass multiple values in as well e.g.

In this case, the value changes to an array, so it's important to check the value type before using it.

If you want to pass in multiple differently named parameters e.g. "p" and "q", you change the getopt() call like this...

Then running the script like this...

...produces this output...

The above way of using getopt() limits your to using single character parameter names, what if you wanted to use a parameter name like "name"? This is also possible, we just change getopt() call to this...

When using long parameter names, the way that the PHP script is run changes slightly, in this case we pass the parameter like so...

The output is then...

This can be expanded to multiple differently named parameters too. Passing the same named parameter multiple times also results in the value being of an array type, same as described earlier. It is also possible to pass values with a space in them by putting them in quotes.

This article doesn't cover all of the possibilities with getopt() but it gives a starting point and hopefully now you can convert your script that uses the usual query parameters with $_GET to something that uses one of the approaches outlined above.

-i