test.php:
Copy<?php
print_r($argv);
?>
Shell:
Copy$ php -q test.php foo bar
Array
(
[0] => test.php
[1] => foo
[2] => bar
)
Answer from schneck on Stack Overflowtest.php:
Copy<?php
print_r($argv);
?>
Shell:
Copy$ php -q test.php foo bar
Array
(
[0] => test.php
[1] => foo
[2] => bar
)
If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) - just try this
Copywget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1
where:
- « -O - » — (Letter "O", not zero!) redirect "downloaded html" to stdout
- « >/dev/null 2>&1 » — redirect stdout & stderr output to nowhere
- « -q » — quiet wget run
- « -t 1 » — just 1 try to connect (not like default 20)
In PHP's "exec" it'll be smth like this:
Copyfunction 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.
Script:
<?php
// number of arguments passed to the script
var_dump($argc);
// the arguments as an array. first argument is always the script name
var_dump($argv);
Command:
$ php -f test.php foo bar baz
int(4)
array(4) {
[0]=>
string(8) "test.php"
[1]=>
string(3) "foo"
[2]=>
string(3) "bar"
[3]=>
string(3) "baz"
}
Also, take a look at using PHP from the command line.
If you want to keep named parameters almost like var=3&foo=bar (instead of the positional parameters offered by $argv) getopt() can assist you.
Passing parameters to a shell script from PHP.
Its all about the "strings", and when to "double quote" for expansion.
<?php
/* exec("/csvexport.sh $table"); */
/* double quote here because you want PHP to expand $table */
/* Escape double quotes so they are passed to the shell because you do not wnat the shell to choke on spaces */
$command_with_parameters = "/path/csvexport.sh \"${table}\"";
$output_from_command = "";
$command_success = "";
/* double quote here because you want PHP to expand $command_with_parameters, a string */
exec("${command_with_parameters}", $output_from_command, $command_success);
/* or to keep it simple */
exec("/path/csvexport.sh \"${table}\"");
/* show me what you got */
echo"${command_success}\n${output_from_command}\n";
?>
BTW: I did not test this snippet.
I'm not a PHP guy, but it seems you'll need to do something like
exec(escapeshellcmd("/csvexport.sh \"$query\" $table"));
Does PHP have a function where you can call the command and pass the arguments separately?
some_exec_function("/csvexport.sh", $query, $table); # ???
Send $argv of serv.php from serv.php to update.php (see http://php.net/manual/en/reserved.variables.argv.php). I.e. in update.php you will have another $argv, so you have to send list of command line parameters in array with different name.
use exec or system call within your calling PHP Script. do mention full path of PHP CLI i.e. /user/bin/php or whatever path is there in your server.
Which PHP binary are you using? The CLI or CGI? I suspect you need a CGI version of the binary for PHP to properly handle accept the environment variables and POST data if you pass that.
The php-cgi binary implements the CGI interface, which allows you to pass parameters on the command line:
php-cgi -f index.php left=1058 right=1067 class=A language=English
Which end up in $_GET:
Array
(
[left] => 1058
[right] => 1067
[class] => A
[language] => English
)
You may want to read up on how CGI works so you can implement that in your web server.
Ref: RFC3875
According to Wikipedia, the answer is to set the QUERY_STRING environment variable:
QUERY_STRING='user=foo&pass=bar&left=1234' php-cgi index.php
or in separate steps:
export QUERY_STRING='user=foo&pass=bar&left=1234'
php-cgi index.php
This works if you use the php-cgi command, because this is part of the CGI specification. The ordinary php command ignores this variable, but your script might use it.
Presumably you're passing the arguments in on the command line as follows:
php /path/to/wwwpublic/path/to/script.php arg1 arg2
... and then accessing them in the script thusly:
<?php
// $argv[0] is '/path/to/wwwpublic/path/to/script.php'
$argument1 = $argv[1];
$argument2 = $argv[2];
?>
What you need to be doing when passing arguments through HTTP (accessing the script over the web) is using the query string and access them through the $_GET superglobal:
Go to http://yourdomain.example/path/to/script.php?argument1=arg1&argument2=arg2
... and access:
<?php
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
?>
If you want the script to run regardless of where you call it from (command line or from the browser) you'll want something like the following:
as pointed out by Cthulhu in the comments, the most direct way to test which environment you're executing in is to use the PHP_SAPI constant. I've updated the code accordingly:
<?php
if (PHP_SAPI === 'cli') {
$argument1 = $argv[1];
$argument2 = $argv[2];
}
else {
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
}
?>
$argv[0]; // the script name
$argv[1]; // the first parameter
$argv[2]; // the second parameter
If you want to all the script to run regardless of where you call it from (command line or from the browser) you'll want something like the following:
<?php
if ($_GET) {
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
} else {
$argument1 = $argv[1];
$argument2 = $argv[2];
}
?>
To call from command line chmod 755 /var/www/webroot/index.php and use
/usr/bin/php /var/www/webroot/index.php arg1 arg2
To call from the browser, use
http://www.mydomain.example/index.php?argument1=arg1&argument2=arg2