test.php:
<?php
print_r($argv);
?>
Shell:
$ php -q test.php foo bar
Array
(
[0] => test.php
[1] => foo
[2] => bar
)
Answer from schneck on Stack Overflowtest.php:
<?php
print_r($argv);
?>
Shell:
$ 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
wget -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:
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.
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); # ???
Your call is failing because you're using a web-style syntax (?parameter=value) with a command-line invokation. I understand what you're thinking, but it simply doesn't work.
You'll want to use $argv instead. See the PHP manual.
To see this in action, write this one-liner to a file:
<?php print_r($argv); ?>
Then invoke it from the command-line with arguments:
php -f /path/to/the/file.php firstparam secondparam
You'll see that $argv contains the name of the script itself as element zero, followed by whatever other parameters you passed in.
try echo exec("php /var/www/unity/src/emailer.php 123"); in your script then read in the commandline parameters.
You're starting your command with . - that will be interpreted as shell-source command, which is not what you want, obviously. Instead specify full path and do like:
$result = shell_exec('/bin/bash /full/path/to/script.sh param1 param2');
//var_dump($result);
-also, make sure your php user have permission to execute your sh-script and that PHP can use functions like exec() (they could be disabled by configuration)
You need to use quotes to send arguments, try this :
$command_result = shell_exec('script.sh "'.$param1.'" "'.$param2."');
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.
If you want to execute this script from PHP as well as from Java, then I'd take this approach:
- put your code which does the work into a function within the file "myfunc.php" (give it a more meaningful name). Make the function take whatever arguments it needs, but it shouldn't try to accss $_GET parameters because these will only exist when called through the web.
- now create a PHP file which includes "myfunc.php" and calls the function. This file can access $_GET and other web-specific variables, passing them as parameters to your function.
- To also be able to execute from Java, create a 3rd file called "mycommand.php", which you can execute from the command line. If you need to pass command line arguments, see documentation on $argv. This script should include "myfunc.php" and call the function, passing any necessary parameters.
Develop and test this last script by running your script from the command line:
$ php5 mycommand.php <args go here>
After you get it working like this, then you can invoke it from java by using this solution.
There is no reason to invoke a PHP command as a subprocess from a PHP script - using "include" as I've described above is easier and more efficient. However here is an example for testing purposes:
test.php (invoke this through the browser):
<?
exec("php5 test2.php", $ret);
foreach ($ret as $line) {
print $line . '<br>';
}
?>
This script invokes a second PHP script called "test2.php" (in the same directory), and prints the output from that command.
test2.php simply produces some output:
<?
print "foo\n";
print "bar\n";
print "glorp\n";
?>
If you are feeding the php exec command with a variable, and that variable is coming from some form of user input, you can get yourself into a heap of trouble. A user could input a command that really messes with your system.
As for the &&2>&1, see
In the shell, what does " 2>&1 " mean?
These are redirect commands used in a bourne shell (not javascript that I know of) to send the stdout or stderr to a designated, not-normal place.
R
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
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.
When calling a PHP script from the command line you can use $argc to find out how many parameters are passed and $argv to access them. For example running the following script:
<?php
var_dump($argc); //number of arguments passed
var_dump($argv); //the arguments passed
?>
Like this:-
php script.php arg1 arg2 arg3
Will give the following output
int(4)
array(4) {
[0]=>
string(21) "d:\Scripts\script.php"
[1]=>
string(4) "arg1"
[2]=>
string(4) "arg2"
[3]=>
string(4) "arg3"
}
See $argv and $argc for further details.
To do what you want, lets say
php script.php arg1=4
You would need to explode the argument on the equals sign:-
list($key, $val) = explode('=', $argv[1]);
var_dump(array($key=>$val));
That way you can have whatever you want in front of the equals sign without having to parse it, just check the key=>value pairs are correct. However, that is all a bit of a waste, just instruct the user on the correct order to pass the arguments.
I use this fairly concise method:
if($argc>1)
parse_str(implode('&',array_slice($argv, 1)), $_GET);
Which would handle a call such as:
php script.php item1=4 item2=300
By sending it into $_GET you automatically handle web or CLI access.
For commentary, this is doing the following:
- If the count of arguments is greater than one (as first item is the name of the script) then proceed
- Grab the arguments array excluding first item
- Turn it into a standard query string format with ampersands
- use
parse_strto extract to the$_GETarray
In case you don't want to modify running script, you can specify parameters using in -B parameter to specify code to run before the input file. But in this case you must also add -F tag to specify your input file:
php -B "\$_REQUEST = array('param1' => 'val1', 'param2' => 'val2');" -F yourscript.php
I can't take credit for this but I adopted this in my bootstrap file:
// Concatenate and parse string into $_REQUEST
if (php_sapi_name() === 'cli') {
parse_str(implode('&', array_slice($argv, 1)), $_REQUEST);
}
Upon executing a PHP file from the command line:
php yourscript.php param1=val1 param2=val2
The above will insert the keys and values into $_REQUEST for later retrieval.