26

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

3 Answers 3

34

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.

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

3 Comments

How would you pass a directory into the php script?
@Michael, same way, just read it from the $argv array. Also, thread necromancy is bad.
I'm not sure if I did something wrong, or if the php has changed, but it is unnecessary to use the --. When I use $argv[1], it just reads the --.
6

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

1 Comment

You can pass GET query parameters if you use php-cgi :) See examples here: serverfault.com/questions/187025/…
6

-- 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"];

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.