Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters from bash to php script?

Tags:

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 
like image 731
user420574 Avatar asked Jul 21 '11 16:07

user420574


2 Answers

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.

like image 70
Tino Didriksen Avatar answered Oct 06 '22 16:10

Tino Didriksen


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

like image 30
Patrick Cavanaugh Avatar answered Oct 06 '22 15:10

Patrick Cavanaugh