I don't know how I am going to achieve this, but...
I have a server-side, PHP CLI script. It will be run from the command-line. My PHP script needs to call a function in a JavaScript library and capture the return value. I need to find a way to run the JavaScript server-side. How can I achieve this?
Solution:
Using Rhino, you can run JavaScript as a command line script. First, download and extract Rhino. Then, assuming your scripts look like this:
PHP:
<?php
# myscript.php
$pathToRhinoJar = './rhino1_7R2/js.jar';
$javascriptFile = './test.js';
$output = shell_exec("java -jar $pathToRhinoJar $javascriptFile command line arguments");
echo "Response from javascript:\n $output";
JavaScript:
/* test.js */
for (i in arguments) {
print(arguments[i])
}
This would be the result:
$ php ./myscript.php
Response from javascript:
command
line
arguments