I need to execute a php file with parameters through shell.
here is how I would run the php file:
php -q htdocs/file.php
In addition to the other answers (Which are quite correct), you can also pass arguments as environment parameters, like this:
FOO=42 BAR=quux php test.php
They will then be available in the superglobal $_ENV
.
If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) - just try this
wget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1
where:
In PHP's "exec" it'll be smth like this:
function exec_local_url($url) {
exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/'
. addslashes($url) . '" >/dev/null 2>&1'
);
}
// ...
exec_local_url("file.php?show=show_name");
exec_local_url("myframework/seo-readable/show/show_name");
So, you don't need to change your scripts to handle argc/argv, and may use $_GET as usually do.
If you want jobs runned in background - see for ex. Unix/Windows, Setup background process? from php code
I use approach with wget in my cron jobs; hope it helps.
If you are using it from a PHP file then you can use popen() and do something like this:
$part = $show_name; //or whatever you want with spaces
$handle = popen("php -q nah.php -p=". escapeshellarg($part) . " 2>&1", "r");
This uses the escapeshellarg() function in order to wrap the $part
variable in quotes (and escape any quotes inside it), so that it can be used as a shell argument safely.
test.php:
<?php
print_r($argv);
?>
Shell:
$ php -q test.php foo bar
Array
(
[0] => test.php
[1] => foo
[2] => bar
)
You need to read command line parameters from $argc and $argv.
Using a question mark is something you do in a URL and has nothing to do with executing PHP from a command line.
See also: http://www.sitepoint.com/php-command-line-1/