I have two php files.
first one execute the second one
I want to send a params with the exec
I have no idea how to get the params i sent in the second file
first.php:
$params="hello";
shell_exec('php file.php $params > /dev/null 2>/dev/null &')
file.php:
echo $params;
Thanks!
Use the $argv array
In your case
echo $argv[1];
$argv contains any arguments passed to the script.
See http://php.net/manual/en/reserved.variables.argv.php
Try echo $argv[1]
Take a look at this page from the documentation
Related
I have the following which call the exec to run the script test.php in the background.
exec("/home/gooffers/test.php?one=one &");
Script test.php contains the following
$test = $_GET['one'];
echo $test;
However this is creating an infinite loop (infinite number of processes) which is crashing the server. Why is this happening.
$_GET is not availible when you are running a script via commandline (php-cli).
See here on how to pass arguments to a command line script in php: How do I pass parameters into a PHP script through a webpage?
Basically, it's
exec("/home/gooffers/test.php arg1 arg2");
and then fetching them via
$argument1 = $argv[1];
$argument2 = $argv[2];
I dont know what is happening, but i think it should be
exec("php /home/gooffers/test.php?one=one &");
Works:
php -q /home/site/public_html/cron/file.php
Doesn't work:
php -q /home/site/public_html/cron/file.php?variable=1
Any suggestions? I need to send the variable as $_GET (or not)
do it something like this
curl http://hostname/cron/file.php?variable=1
and in the file.php you will be managing the code to get the $_GET[variable]
this woould behave as a simple browser call but only in your shell/terminal
Hope this helps
Command Line arguments are passed in $argv instead of the normal $_GET/$_POST-Arrays
Of course this does not work with URI-style parameters (that ?variable=1-part). So you have to call it like: php -q /path/to/script.php 1.
As an alternative you could use getopt:
<?php
$shortopts = implode("", array(
"v:"
));
$longopts = array(
"variable:", // Required value
);
$options = getopt($shortopts, $longopts);
var_dump($options);
And call it like php -q /path/to/script.php --variable=1.
The easiest way to work around this (assuming public_html, is, well, public WWW), is to have cron call wget or curl to access the PHP file, so URL variables are processed as normal.
-q means no head, so there is no space for the get-fields i assume, at least i hope so :D
Greetz
I working in Rails and I need to call to an PHP-script.
I can connect to the script like this:
system('php public/myscript.php')
But I need to send some parameters with it.
How do I do that?
Thanks
You can provide command-line arguments to your PHP script:
system('php public/myscript.php arg1 arg2')
They will be available from your PHP code like this:
echo $argv[0]; // public/myscript.php
echo $argv[1]; // arg1
echo $argv[2]; // arg2
You can just specify the parameters on the command line, such as system('php -f public/myscript.php argument1 argument2 [...]') and they will be available in the $argv[] array, starting from $argv[1]. See the doc page here for more info.
Yes its right way to use system('php public/myscript.php arg1 arg2') as SirDarius answered .
but system command will return the true or false in that case.
system() will return TrueClass or FalseClass and display output, try it on console .
I suggest , You can use the open method on any URL to call it, so you can call your PHP script using that:
require 'open-uri'
open('YOUR PHP SCRIPT PATH WITH PARAMETER') do |response|
content = response.read
end
I'm making a function for my users where they can upload large XML files to synchronize with my database.
When a user uploads a file to upload.php, I want to start processing the data in the background with process.php, preferably from a shell command, and redirect the user to status.php, which shows the process of the synchronization.
I need to pass some variables to the process.php script while executing it, either at least one variable with the user id and put the other variables into a text file, (Would probably prefer this so I wont have to put to much data into the exec() command.) or the user id and a bunch of $_POST variables.
One solution I had in mind is executing the PHP script like this:
exec("php -f ./process.php > /dev/null 2>/dev/null &");
This allows me to lock away process.php from http access, which is good since it's a process taking script. The only thing I need here is to pass a variable somehow, but i don't know how to do it.
So my main question is:
How do i pass a variable in the above solution?
Or do any of you have a better solution to doing this? Possibly one where i wont have to go through exec()? Keep in mind that i do not want the user to wait for the script to execute, and i need to pass at least one variable.
Update: For future reference, remember to use escapeshellarg() when passing arguments through exec() or likewise functions.
You test use it
exec("php -f ./process.php var1 var2 > /dev/null 2>/dev/null &");
And if you like get these variables values can acces with global variable $argv. If you print this var show same:
print_r($argv);
Array
(
[0] => process.php
[1] => var1
[2] => var2
)
You can pass parameters like the following.
// call process.php
exec("php -f ./process.php foo=bar bar=foo > /dev/null 2>/dev/null &");
// process.php
if ($argc > 0) {
for ($i=1;$i < $argc;$i++) {
parse_str($argv[$i],$tmp);
$_REQUEST = array_merge($_REQUEST, $tmp);
}
}
var_dump($_REQUEST);
I don't really understood your goal, but to pass an argument to an PHP-script works similar to any other shell scripts. See: http://www.php.net/manual/en/features.commandline.usage.php (Example #2)
"When a user uploads a file […], I want to start processing the data in the background" - You can't access an upload before it is finished, in PHP using CGI.
Here is my solution.
The advantage is that you can use this script for command line usage as well as for regular web usage. In case you call it from cmd the $argv variable is set, so the parse_str() part extracts the variables and puts them into the $_GET array. In case you call it from the web the $argv is not set so the values come from the url.
// executingScript.php
// You have to make percent escaping yourself
exec("php -f ./executedScript.php foo=bar%20foo bar=foo%20bar > /dev/null 2>/dev/null &");
// executedScript.php
// The if-statement avoids crashing when calling the script from the web
if (isset($argv)) {
parse_str(implode('&', array_slice($argv, 1)), $_GET);
}
This will make you able to access the variables as usual:
echo $_GET["foo"] // outputs "bar foo"
echo $_GET["bar"] // outputs "foo bar"
I am wanting to call a php file using exec.
When I call it I want to be able to pass a variable through (an id).
I can call echo exec("php /var/www/unity/src/emailer.php"); fine, but the moment I add anything like echo exec("php /var/www/unity/src/emailer.php?id=123"); the exec call fails.
How can I do this?
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.
If you want to pass a GET parameter to it, then it's mandatory to provide a php-cgi binary for invocation:
exec("QUERY_STRING=id=123 php-cgi /var/www/emailer.php");
But this might require more fake CGI environment variables. Hencewhy it is often advisable to rewrite the called script and let it take normal commandline arguments and read them via $_SERVER["argv"].
(You could likewise just fake the php-cgi behaviour with a normal php interpreter and above example by adding parse_str($_SERVER["QUERY_STRING"], $_GET); on top of your script.)
this adapted script shows 2 ways of passing parameters to a php script from a php exec command:
CALLING SCRIPT
<?php
$fileName = '/var/www/ztest/helloworld.php 12';
$options = 'target=13';
exec ("/usr/bin/php -f {$fileName} {$options} > /var/www/ztest/log01.txt 2>&1 &");
echo "ended the calling script";
?>
CALLED SCRIPT
<?php
echo "argv params: ";
print_r($argv);
if ($argv[1]) {echo "got the size right, wilbur! argv element 1: ".$argv[1];}
?>
dont forget to verify execution permissions and to create a log01.txt file with write permissions (your apache user will usually be www-data).
RESULT
argv params: Array
(
[0] => /var/www/ztest/helloworld.php
[1] => 12
[2] => target=13
)
got the size right, wilburargv element 1: 12
choose whatever solution you prefer for passing your parameters, all you need to do is access the argv array and retrieve them in the order that they are passed (file name is the 0 element).
tks #hakre
I know this is an old thread but it helped me solve a problem so I want to offer an expanded solution. I have a php program that is normally called through the web interface and takes a long list of parameters. I wanted to run it in the background with a cron job using shell_exec() and pass a long list of parameters to it. The parameter values change on each run.
Here is my solution: In the calling program I pass a string of parameters that look just like the string a web call would send after the ?. example: sky=blue&roses=red&sun=bright etc. In the called program I check for the existence of $argv[1] and if found I parse the string into the $_GET array. From that point forward the program reads in the parameters just as if they were passed from a web call.
Calling program code:
$pars = escapeshellarg($pars); // must use escapeshellarg()
$output = shell_exec($php_path . ' path/called_program.php ' . $pars); // $pars is the parameter string
Called program code inserted before the $_GET parameters are read:
if(isset($argv[1])){ // if being called from the shell build a $_GET array from the string passed as $argv[1]
$args = explode('&', $argv[1]); // explode the string into an array of Type=Value elements
foreach($args as $arg){
$TV = explode('=', $arg); // now explode each Type and Value into a 2 element array
$_GET[$TV[0]] = $TV[1]; // set the indexes in the $_GET array
}
}
//------------------------
// from this point on the program processes the $_GET array normally just as if it were passed from a web call.
Works great and requires minimal changes to the called program. Hope someone finds it of value.