NodeJs / Php - Extremely simple! Please help asap!!!
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I am running NodeJS on my server and I have test.js with:

console.log("Hello World");

Then on my php file I have:
$dir = dirname(__FILE__);
$result = exec("node $dir/test.js", $output);
print_r($result);
print_r($output)

If I run "node test.js" on SSH I get "Hello World".
However, when running the PHP file, no matter what I try I get an empty output.

I need to be able to read the result of the script and preferable it would be if I could have on test.js return a value instead of output via console.log: return "some value;"

In other words: I want to be able to run a js file through node and get the return value through PHP using exec() or shell_exec(). Thank you!

--
************************** UPDATE ****************************
I run this debug code as suggested by 5osxcwbf:
test.js
console.log("Hello World");
return "yes";

test.php

<?php
//debug;
echo 'get_current_user(): ' . get_current_user()."\n";
echo "whoami: ";
echo exec('whoami');
echo "\nProcess User: ";

$processUser = posix_getpwuid(posix_geteuid());
echo $processUser['name']."\n";

exec("ls -l", $outputA);
var_dump($outputA);


//real script
$dir = dirname(__FILE__);
$cmd = "node $dir/test.js";
$result = exec('(echo '.$cmd.') | /bin/bash', $outputB);

echo "result: ";
var_dump($result);
echo "\noutput: ";

var_dump($outputB);

if($outputB[0] == "Hello World")
    echo "Everything is okay. Now run function; thank_and_tip(5osxcwbf) \n";

$dir = dirname(__FILE__);
$exec = "node -v" ;
echo  $exec;
echo "\n--";
print_r( exec($exec, $value1, $value2));
print_r($value1);
print_r($value2);
echo exec("node -e  'console.log(4);'", $result);
print_r($result);`

The output is:

get_current_user(): userliv
whoami: userliv
Process User: userliv
array(8) {
  [0]=>
  string(8) "total 36"
  [1]=>
  string(57) "drwxr-xr-x  2 userliv userliv 4096 Oct  6 15:35 cgi-bin"
  [2]=>
  string(59) "-rw-r--r--  1 userliv userliv 8450 Mar 28 11:21 error_log"
  [3]=>
  string(58) "drwxr-xr-x 17 userliv userliv 4096 Oct  6 16:36 folder1"
  [4]=>
  string(55) "drwxr-xr-x 28 userliv userliv 4096 Mar 28 01:35 folder2"
  [5]=>
  string(59) "-rw-r--r--  1 root     root       63 Oct  6 16:23 index.php"
  [6]=>
  string(57) "-rwxrwxrwx  1 userliv userliv   42 Mar 28 01:45 test.js"
  [7]=>
  string(58) "-rwxrwxrwx  1 userliv userliv  796 Mar 28 11:23 test.php"
}
result: string(0) ""

output: array(0) {
}
node -v
--Array
(
)
127Array
(
 )
awarded to 5osxcwbf
Tags
PHP
nodejs

Crowdsource coding tasks.

3 Solutions


you need to add this line since $output will be an array
echo implode('', $output);

I am using print_r I get an empty array
georgefountain 5 months ago
I have tried on both windows and ubuntu and the output is printed, its just that i cant get an online code sharing tool that has both node and php, but i have it on codeanywhere(http://bou-webmaster958281.codeanyapp.com/), do you have a codeanywhere account? i can share with you, its working on ubuntu 14.04
mashtullah 5 months ago
I am on cloudlinux. This is not working..
georgefountain 5 months ago
open a codeanywhere.com account, you can signup free with a gmail,github account, quickly setup a sandbox.
mashtullah 5 months ago
I believe you but this doesn't solve the problem. I need this working on my environment not on a sandbox...
georgefountain 5 months ago
i suggest you try out a different cloud service, that has more flexibility, like digitalocean...
mashtullah 5 months ago

Make sure your node js file and php file is in the same directory.

<?php
$dir = dirname(__FILE__);
print_r($dir);
$result = exec("node $dir/test.js", $output);

print_r(sizeof($output)); //Check the size of the array is greater than 0

foreach ($output as $line) {
    print_r($line);
}

?>
I am using print_r I get an empty array.
georgefountain 5 months ago
Winning solution

All earlier answers are bullshit. What is this, amateur hour? You clearly have a permissions problem.
Please run this script, perhaps it will work out of the box, but at least it will give me some debug info.

<?php
//debug;
echo 'get_current_user(): ' . get_current_user()."\n";
echo "whoami: ";
echo exec('whoami');
echo "\nProcess User: ";

$processUser = posix_getpwuid(posix_geteuid());
echo $processUser['name']."\n";

exec("ls -l", $outputA);
var_dump($outputA);


//real script
$dir = dirname(__FILE__); 
$cmd = "node $dir/test.js";
$result = exec('(echo '.$cmd.') | /bin/bash', $outputB);

echo "result: ";
var_dump($result);
echo "\noutput: ";

var_dump($outputB);

if($outputB[0] == "Hello World")
    echo "Everything is okay. Now run function; thank_and_tip(5osxcwbf) \n";


?>

Edit;
Starting to think that you might have a path problem, not a permissions problem.
When you exec code using php, sometimes it executes from within a linux user account that does not have an enviroment set up, and because of that no PATH. So lets try using a full path;

With ssh,  find the direct node binary path by running for example (kinda depends on your distro); 

$) whereis node

It might be located at a position like; 
/usr/bin/node

so now from ssh try to run the version command;

/usr/bin/node -v

This should output proper version in your cli




Once you have confirmed the full path and know the absolute location of the binary create a script like;
<?php 
//this depends on your linux distro
// $cmd = "/usr/bin/node -v";
//  $cmd = "/bin/node -v";

exec($cmd, $output);
var_dump($output);

exec('(echo '.$cmd.') | /bin/bash', $outputB);
var_dump($outputB);

exec('(echo '.$cmd.') | /usr/bin/bash', $outputC);
var_dump($outputC);
?>

So return values only work with numbers, you want to use stderr and redirect it to stdout, then silence the original stdout if you don't want to debug your original code, like this;

#test.js:

console.log("Hello World");
console.error("something");

#test.php
<?php
$debug = false;

$append ="";
if(!$debug) $append = ">/dev/null";

exec("node test.js 2>&1 ".$append,$output);
var_dump($output);
?>
Did you test that bullshit to know its not working? i tested my solution before submitting...
mashtullah 5 months ago
print_r() that he used in his original code is perfectly capable of displaying the array returned by exec(), go waste someone else's time; http://php.net/manual/en/function.print-r.php#refsect1-function.print-r-examples
5osxcwbf 5 months ago
Oh and even op's original code tests fine in a normal environment where permissions aren't messed up; your inability to anticipate on how code executes differently across different environments makes you an amateur.
5osxcwbf 5 months ago
Thanks so much for the comprehensive answer 5os. Up to now the most complete and I really appreciate. However it doesn't work. Can you finish helping me? Thanks!
georgefountain 5 months ago
If you are willing to give me a ssh/webmin/directadmin/cpanel/plesk guest account i could take a look, just email the details to; alt.r2-5osxcwbf@yopmail.com, it is my anti spam email address. But you could also upload the debug output of the script i gave you in my post to http://gist.github.com or http://pastebin.com. That way i have some more information and i could write anotherr quick script to test some things; if you don't give me enough info i'm just taking shots in the dark and that will cost us both lots of time.
5osxcwbf 5 months ago
Thanks for the quick reply! I posted the output of your script on the bounty. See at the bottom I updated it :) Let me know if you want to run something else.
georgefountain 5 months ago
Thanks for the quick reply! I posted the output of your script on the bounty. See at the bottom I updated it :) Let me know if you want to run something else.
georgefountain 5 months ago
Any update? I emailed you
georgefountain 5 months ago
/usr/bin/node -v works from command line on SSH as sudo user. But when trying that from php I get nothing. I am on cloudlinux
georgefountain 5 months ago
https://www.cloudlinux.com/index.php/php-selector Your linux distro is causing this problem. I have no experience with it. I suggest using support channels related to the distro.
5osxcwbf 5 months ago
What do I exactly need to ask? They will tell me it is a coding issue if I don't mention something specific Thanks in advnace
georgefountain 5 months ago
That either due to a environmental path problem or a file permissions problem php is unable to exec(node -v) command. Even though the same code does work under mainstream OS's like debian & even macosx. If they are unwilling to provide support for this i suggest switching OS's since there will be lots of other tiny little flaws like that in the future that might seem simple to solve on first glance but really come down to strange OS architecture. Hope you are willing to accept my answer so i get the bounty, and perhaps a tip in return for the time i invested, since this problem certainly is not extremely simple. Please next time mention in the openings post that you are using a niche OS if you are having these kinds of problems; its a pretty important detail.
5osxcwbf 5 months ago
I am now using the full full path. Not the sysmlink. If I run on shell: /usr/local/node-v6.10.1-linux-x64/bin/node -v > ok2 ok2 gets the version number. If I run on php: exec("/usr/local/node-v6.10.1-linux-x64/bin/node -v > ok2"); ok2 now has: /usr/local/node-v6.10.1-linux-x64/bin/node -v Why is this? Is my test wrong?
georgefountain 5 months ago
I think I have the solution!!!! Check this out: drwxrwxr-x 2 500 500 4096 Mar 21 15:09 ./ drwxrwxr-x 6 500 500 4096 Mar 21 15:09 ../ -rwxrwxr-x 1 500 500 30497432 Mar 21 15:09 node* lrwxrwxrwx 1 500 500 38 Mar 21 15:09 npm -> ../lib/node_modules/npm/bin/npm-cli.js Are these the right permissions for node? Maybe this is the issue
georgefountain 5 months ago
Here it is more clear: https://pastebin.com/N7f6zSic
georgefountain 5 months ago
The issue was a CageFS permissions problem. Could you tell me if there is any way to get the return value of a script so I can use "return value;" instead of "console.log(value)". My scripts have lots of console.log() commands for debug purposes so I would have to delete all of them. Is it possible to get the return value instead of the console.log through exec() or any way through PHP? Please let me know. I will award the bounty to you. Thank you!
georgefountain 5 months ago
Check my edited answer, a tip to compensate for the time this bounty cost me would be nice.
5osxcwbf 5 months ago
The edited answer did not answer the extra question. I wasn't able to get the output of the script via "return". Most likely my question is unclear... I will post another bounty and let you know so I can clarify the question and award it to you, if the solution works. Thanks :)
georgefountain 5 months ago
Hey there ! I was told you are great to work with and you are a master of node.js. If you have time please take a look to my bounty (https://bountify.co/node-js-mysql-php-create-2-tables-save-records-part-2-of-9 ). It's simple and there are 8 more tasks after that one with the same complexity. Feel free to contact me at klau@vortilon.com Looking forward.
Vortilon 4 months ago
Sorry. My email address is klaus@vortilon.com
Vortilon 4 months ago
View Timeline