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));
Related
I am creating dynamic query through the loop as a string and wants to run that query and hold the result in a variable then return that result as json for ajax purpose.
But the problem is here if I echo the query that stop executing further and just return query itself.
$data = 'Share::all();';
ob_start();
echo $data; // stop executing here
$returnData = ob_get_contents();
ob_end_clean();
return $returnData;
Direct Eloquent Call
I think you might actually need this one line instead of all the code you wrote:
return Share::all()->toArray();
Printing a piece of php code (i.e. 'Share::all();' in your case) as string does not execute the code. And it certainly will not give you the result of executing that code.
Using Output Buffer
Or perhaps you want to use the dangerous eval to parse output of the code? In that case:
$data = 'echo "hello world";';
$returnData = (function ($code) {
ob_start();
eval($code); // stop executing here
return ob_get_clean();
})($data);
var_dump($returnData);
Please note that the return data can only be string. It can never be anything other than plain string. Thus it doesn't really make sense to me if you want to pass the "query result" with output buffer.
Simply eval
You are probably actually thinking about this.
$data = 'return Share::all()->toArray();';
$returnData = eval($data);
var_dump($returnData);
But you'd get wield result with modern namespaced PHP code. Plus running eval is really not recommended.
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.
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).
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);
});
I have a perl script that does a lot of config file parsing for me and creates a hash with all the information I need.
I want to call that script from PHP and have PHP get the hash to be able to work with the hash in php and not just returning some html code from the perl script.
Is that possible? Haven't found any way yet and just know that I am able to return lots of html code as output, but that's not what I want the perl script to do.
The simplest way, serialize this hash into json in perl and print resulting string to STDOUT.
In PHP it can be easily decoded into array or object...
If the platform that is executing the PHP allows for it, you can call the exec() function to execute external files like:
$result = exec( "/path_to/your_script.pl", $lines, $state);