how to decode the encoded array passed from php - php

I have problem in decoding the array passed from PHP. My PHP code is
$checkedJson = json_encode($dynamic_species);
$tmp = exec("/Python33/arr_pass.py $pressure $temp $checkedJson");
return $tmp;
If i print $checkedJson i get
{"species1":"CH4","species2":"C2H6"} as print statement
My python code is
species_list = sys.argv[3]
species_list_data = json.loads(species_list)
print(species_list_data['species1'])
This python script returns empty string as output to php
I am working for first time on JSON can anyone please help me.
Thanks in advance

This is neither a JSON nor Python question. What you want is to figure out how to get output back when your PHP program runs something via exec(). Basically you're lacking the output parameter; RTM at php.net. You should try:
exec("/Python33/arr_pass.py $pressure $temp $checkedJson", $output);
After which $output will be an array of lines that your exec'd script sent to its output. In your code, $tmp is assigned to the last line that the exec'd thing prints, so probably an empty line.
Plus you have one more challenge; the way you pass $checkdJson to the Python script will fail. You will have to quote it to make it one commandline parameter, so you'll probably need
exec("/Python33/arr_pass.py $pressure $temp '$checkedJson'", $output);
(note the extra single quotes).

Related

How to pass PHP variable values to Python via shell_exec

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

How to run file in background by adding variable

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.

PHP to execute a Ruby script and capture the JSON result

I have to work with a Ruby script that is dependent on a Gem. When the script is executed a JSON object is returned.
I am successfully executing the script using the following code.
<?php
$ruby = 'ruby ruby/teams.rb';
$res = system($ruby);
var_dump(json_decode($res, true));
The following is an example response from ruby/teams.rb
{"status"=>"active", "teamId"=>"XPLFKS59PK" }
My problem is that this printed directly to the screen and is not capture in the variable $res. When using var_dump(json_decode($res, true)) I get null.
What I would like to be able to do is capture the JSON response in the $res variable so I can convert to an array and worth with the data.
Any ideas if this is possible?
My problem is that this printed directly to the screen and is not capture in the variable $res.
Most likely it sends its output to stderr instead stdout, so you need to redirect the streams yourself like this:
$ruby = 'ruby ruby/teams.rb 2>&1';
Here more about stream redirection: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
Or, use exec() instead of system()
system only returns the last line of the commands output. In your case, i would use exec. Your code would look something like below
$ruby = 'ruby ruby/teams.rb';
exec($ruby, $res);
var_dump(json_decode(implode("", $res), true));

php `exec` calls python script. how return strings from python?

I use exec function calling python script from PHP script. Python writes to standard output two strings which I need in PHP script. The problem is that in these strings could be end of line characters \n ( so formally there are many lines in output), and according to exec manual array $output will contain
each line in it. What is elegant way to escape \n characters so that $output will contain only two string I want and no post processing of these two string needed?
EDIT: I can change python script.
Print the output in an easily parsable format, such as JSON, and parse it from PHP. For example, instead of:
print foo
print bar
Use something like:
import json
print json.dumps([foo, bar])
You read the JSON output form PHP and decode it using json_decode($output) into the desired array.
There really is nothing you can do about this outside of changing the way the python script outputs.
It is really easy to clean the returned data though.
exec('yourcommand', $array);
array_walk($array, function($value, $key) {
return trim($value);
});

PHP string contains $ and incorrectly recognizes variable

Basically I'm sending a formatted string to a PHP script via POST
So say I have the string... "abcdef$ghikl" it will recognize $ghikl as a variable...
Is there any ways to tell PHP that no variables exist within this string?
I know how to hard code it, you just use "'", but since the string is being sent to the script, I don't know what to do...
Thanks guys
For the scope of the question, all the code that's really needed is this:
$string = $_POST['string'];
// a couple strcmp's here, just to see if the string == ""
$array = explode("+", $string);
Try escaping the $
"abcdef\$ghikl"
PHP won't see that as a variable unless you eval() it. Can you show us your code?

Categories