A small, simple PHP class for executing Node.js files.
In certain scenarios, you may need to execute a Node.js script from a PHP environment. While it's common to have a Node.js server running, setting one up might require significant effort. This class offers a straightforward solution.
- Verifies if PHP can execute Node.js and stores the version.
- Executes a script, returning the output and the exit code. (Refer to Node.js Process Exit Codes for details.)
require 'NodeJSExecutor.php';
$executor = new NodeJSExecutor();
$executor->run("test.js");
echo "Exit Code: " . $executor->exitCode . "<br>";
echo "Result: " . $executor->result . "<br>";
The class can trigger two exceptions:
- Node.js is not found (checks the result of
node --version). - The specified JavaScript file is not found.
If you appreciate this project, feel free to use it, refactor it, enjoy it, or even expand on it. Why not send some tip love?