Executing Nodejs Script File in PHP Using Exec()

Executing nodejs script file in PHP using exec()

I got it finally. Its just ignorng the NODE_PATH variable for reasons unkown :(

In the Nodejs File I hade to give the absolute path of the module like this:

var request = require("/usr/lib/node_modules/request");

Call Node script with PHP exec and return data to PHP before finally method

Use proc_open to run a process asynchronously. Let node output data as soon as possible. This data can be read without waiting for node to finish:

// foo.js
process.stdout.write(JSON.stringify({"foo":"bar"})+"\n");

// simulate cleanup
setTimeout(() => {}, 3000);


// foo.php
<?php

$io = [
0 => ['pipe', 'r'], // node's stdin
1 => ['pipe', 'w'], // node's stdout
2 => ['pipe', 'w'], // node's stderr
];

$proc = proc_open('node foo.js', $io, $pipes);

$nodeStdout = $pipes[1]; // our end of node's stdout
echo date('H:i:s '), fgets($nodeStdout);

proc_close($proc);
echo date('H:i:s '), "done\n";

Sample output:

$ php foo.php 
14:59:03 {"foo":"bar"}
14:59:06 done

proc_close will wait for the node process to exit, but you can defer that waiting until you're done with other things in PHP.

If you don't call proc_close, you may end up with zombie processes, depending on your environment. Docker and the PID 1 zombie reaping problem contains a good explanation. It refers to docker a lot, because this is where zombies frequently occur, but it is still generally applicable.

Some Server APIs have provisions for running code "invisibly" for the client. For instance, fcgi has fastcgi_finish_request, which you can call before proc_close. Then HTTP clients wouldn't notice the waiting time.

If you can't tolerate eventually waiting for node, consider making the node thing a webservice instead, which is managed independently of PHP.

Run server-side js from php through exec()

I found a way to make it work! I just wrote the full directory where node.js is installed in the exec() call. Simple as that:

<?php exec('/home/bin/node myscript.js >/dev/null/ 2>&1 &'); ?>

Trying to execute node.js command from PHP page

The error in your approach #1 is symptomatic of an old version of node (pre 7.6) that doesn't recognize the 'async' keyword. Try adding:

console.log(process.version)

to the-node-command to double check this. If it is, in fact an old version of node, update it.

With approach #2, you are trying to run app.js as a shell script. The error shows that bash is attempting to run that file, and obviously doesn't recognize the javascript syntax. You should instead be using:

$command = 'export PATH=$PATH:path-to-node-executable; node app.js';

Once you have either of these two approaches working, you should indeed be able to achieve your ideal case by following the answer here: php exec command (or similar) to not wait for result

execute node with php

Windows does not work well with spaces as you need to tell it it's all part of the same command, you will need to enclose the full command path within double quotes (singles don't work), then replace your backward slashes for forward slashes, is always better to use forward within your scripts, also, provide the full path to the node script as well, else it will assume the script exists on whatever directory your php script or php binary itself is running, so is better to avoid confusions, this would work:

exec("\"C:/Program Files (x86)/nodejs/node.exe\" \"C:/path/to/script/welcometonode.js\"");

How to run node.js file from PHP file

You are using the wrong path to Node.js. Try this:

<?php
$sendSmsPath = "send-sms.js";
exec('/c/Program Files/nodejs/node '.$sendSmsPath);

node_modules is for the dependencies of your script, not the actual Node.js executable.

If you'd like to run your script on another machine make sure it has Node.js installed globally and you know the path of the executable (can be found with which node)

How to execute a PHP program in the form of a string inside node.js?

You can use -r flag of PHP cli for that.

const runner = require('child_process');
const phpString = `'echo "hi";'`
runner.exec('php -r ' + phpString, (err, stdout, stderr) => {
console.log(stdout) // hi
});

Although I would use execFile/spawn instead, to avoid scaping the arguments

const runner = require('child_process');
const phpString = `echo "hi";` // without <?php
runner.execFile('php', ['-r', phpString], (err, stdout, stderr) => {
console.log(stdout) // hi
});

If you want to use <?php tags, you should use spawn and write to stdin. This is the best approach in my opinion.

const php = runner.spawn('php');
const phpString = `<?php echo "hi";?>` // you can use <?php

// You can remove this if you want output as Buffer
php.stdout.setEncoding('utf8')
php.stdout.on('data', console.log)
php.stderr.on('data', console.error)

php.stdin.write(phpString)
php.stdin.end()

Have in mind that allowing users to execute code on your server is not recommended.

how to run a nodejs Script in php page

You want to execute a js file which is not possible without an interpreter. Your exec code should look like this:

exec('node write.js')

Here node is the executable of you node instance.



Related Topics



Leave a reply



Submit