I'm trying to set up an html form that passes variables to a php script, that then passes them to a bash script.
I'm able to successfully pass variables from the html form to the php script, and I'm able to make the following pass variables to my bash script:
<?php
shell_exec('./bashscript.sh testarg1 testarg2 testarg3 testarg4');
?>
But, when I use the following:
<?php
shell_exec('./bashscript.sh $_POST[arg1] $_POST[arg2] $_POST[arg3] $_POST[arg4]');
?>
I end up with:
[arg1]
[arg2]
[arg3]
[arg4]
This is the first time I've tried passing from php to bash. The bracketed arguments post to a db with no prob. How would I change this to successfully pass the arguments to my bash script? thx.
Variables are expanded inside double-quoted string, not inside single-quoted strings.
shell_exec("./bashscript.sh $_POST[arg1] $_POST[arg2] $_POST[arg3] $_POST[arg4]");
This is basic PHP syntax, having nothing to do with using the shell.
You should also use escape_shell_arg to escape each argument before substituting it, in case it contains shell metacharacters.
Related
I have some variables defined in a PHP file. I then call a python script from the PHP with these variables as arguments. HOWEVER the values of the variables do not carry over to my python script, it seems as though they as passed as strings and not as variables:
$first = "doggy";
$second = "kitty";
$command = escapeshellcmd('python ./script.py $first $second');
$output = shell_exec($command);
The above code produces not "doggy" and "kitty" respectively in my Python script, but literally "$first" and "$second". I want doggy and kitty.
For example when I print in Python:
print sys.argv[1];
>>$first
is the output I am receiving.
My Python script is NOT outputting anything, the script interacts with an API that I wish to use these variables with.
I have tried these previous posts which seem to be near what I am asking. I am either not understanding them, or they are not working. Some answers are too technical or too vague.
Passing value from PHP script to Python script
PHP <-> Python variable exchange
If shell_exec is not the best way for this, I am open to new ideas. Any and all help is appreciated. Thanks for taking the time to look at my post.
A single quoted string will be displayed literally, while a double quoted string will interpret things like variables and new line sequences (\n).
Therefore, change from single to double quotes:
$command = escapeshellcmd("python ./script.py $first $second");
Read more about strings in the PHP manual: http://se1.php.net/manual/en/language.types.string.php
Hey PHP developers I am newbie.
Today I want to run my process.php file in the background because it takes too much time to load... Here is the code that I want to use.
$proc=new BackgroundProcess();
$proc->setCmd('exec php <BASE_PATH>/process.php hello world');
$proc->start();
And I want to add this ids=$postid&reaction=$reaction variable instead of hello world.
And want to receive it with post in process.php file like this
$id =$_POST['ids'];
$type = $_POST['reaction'];
I am using this GitHub file
https://github.com/pandasanjay/php-script-background-processer/blob/master/README.md
Before doing downvote answer me I am a newbie in PHP.
You can try exec() for this. If you want to pass parameters then try like this.
//it will store logs to log_data.log
exec("php process.php $id $type >log_data.log &");
Hope this will work for you :)
Try like this
function execInBackground() {
//this will run in background
exec("php process.php $id $type > /dev/null &");
}
As soon as it is not HTTP request at all, you cannot access $_GET and $_POST superglobals. The right way to receive arguments in this case, is to access the array $argv. See official documentation:
http://php.net/manual/en/reserved.variables.argv.php
UPD: And, well, if you really want to pass $_GET/$_POST params to this script executed via shell, here is a dirty trick:
$get_params_as_string = base64_encode(json_encode($_GET));
$proc=new BackgroundProcess();
$proc->setCmd("exec php <BASE_PATH>/process.php {$get_params_as_string}");
$proc->start();
And in your process.php access it like this:
$get_params = json_decode(base64_decode($argv[1]), true);
So, we are just created JSON from $_GET array. Then, as we know that JSON string contains special characters(like ", {, }, etc), and to avoid dealing with problems of escaping and unescaping, we simply encode this string as base64. It guarantees us absence of special characters in result string. Now we can use this string as a single argument, which we will pass to shell command (your BackgroundProcess). And finally, in process.php we can access this string from $args[1], then decode from base64, then decode from JSON to a regular PHP array. Here we go.
This solution is provided only for educational purpose, please don't ever do it in real life.
You have a string, which contains a snippet of PHP code:
$run_me = "echo ('Hello World!!');";
How can you get PHP to run the code, contained in $run_me?
You can't do:
include ($run_me);
That would include the path $run_me, so what's the solution?
One way is to use eval(). This will execute the string you enter as PHP code.
There is a web application written in PHP and HTML. What I want is to filter a users input for a variety of cases and sanitize it. For example, I want to compare the input from a form (string) with a list of allowed strings and depending if it is right or wrong to trigger the suitable PHP function to handle this.
My question is how to bind the user input with the python script and then the outcome of this python script as an input for PHP?
thanks
You can call the Python script from your PHP file as a shell command, passing it JSON-formatted arguments. Then have the Python script output the response (also JSON encoded) and have the PHP file capture that. Here's an example I used recently, cobbled together from the links below:
PHP file:
$py_input = ... // Your data goes here.
// Call the Python script, passing it the JSON argument, and capturing the result.
$py_output = shell_exec('python script.py ' . escapeshellarg(json_encode($py_input)));
$py_result = json_decode($py_output);
Python file:
import json
php_input = json.loads(sys.argv[1]) # The first command line argument.
# Do your thing.
php_output = ... # Whatever your output is.
print json.dumps(php_output) # Print it out in JSON format.
Passing a Python list to php
executing Python script in PHP and exchanging data between the two
copy('https://graph.facebook.com/$fbid/picture?type=large', 'images/$fbid.jpg');
i am using the above code to store the image locally ..
the above code works without the variable. As it does not executes php in it so it is useless with links containing php variables....
The code works with a definite url is provided...
i wanna use the above url of source and destination respectively to get image...
please suggest me any other workaround or way that allows the links with variables to be executed ....
Your strings are wrapped in ' ', to use variable interpolation, you need to wrap yours strings in " ", so copy("https://graph.facebook.com/$fbid/picture?type=large", "images/$fbid.jpg"); will work.
Also, to make it clearer, it's possible to wrap your variables in { }, so "Hello {$world}" will, assuming $world contains "World", print "Hello World".
There's a few other gotchas, so have a look over the PHP manual page for strings I've put at the bottom of this post.
Ref: http://php.net/manual/en/language.types.string.php