r - php exec() doesn't store the output in $output argument -
my index.php
:
<?php $n = 3; exec("rscript my_script.r $n", $out); var_dump($out); <?
my_script.r
:
#!/usr/bin/env rscript <- as.numeric(commandargs(true)) rootkea <- mongodbconnect('rootkea') print(i:10)
output:
array(0) { }
shell output:
$ rscript my_script.r 3 loading required package: rjava loading required package: methods [1] 3 4 5 6 7 8 9 10
interestingly php works expected if remove mongodbconnect
line my_script.r
modified my_script.r
:
#!/usr/bin/env rscript <- as.numeric(commandargs(true)) #rootkea <- mongodbconnect('rootkea') print(i:10)
output:
array(1) { [0]=> string(27) "[1] 3 4 5 6 7 8 9 10" }
shell output:
$ rscript my_script.r 3 loading required package: rjava loading required package: methods [1] 3 4 5 6 7 8 9 10
i need have database connection , other processing in my_script.r
before print result. how make exec
store output in $out
argument?
your code looks ok.
try ugly hack:
$temp = tempnam(null, null); $temp2 = tempnam(null, null); exec("rscript my_script.r $n > \"$temp\" 2> \"$temp2\""); $out = file_get_contents($temp); $err = file_get_contents($temp2); unlink($temp); unlink($temp2);
Comments
Post a Comment