4

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
7
  • 2
    Why do you need this, exactly? Commented Dec 7, 2010 at 20:27
  • Having a hard time finding a PHP equivalent of this particular JavaScript library. Commented Dec 7, 2010 at 20:28
  • 2
    OK. What is that particular JS library ? Commented Dec 7, 2010 at 20:30
  • JavaScript runs client-side, that is it's purpose. Unless you actually mean Java and not JavaScript, I think you're going to find it a better use of time to find or write some server-side code equivalent to the library you have. Commented Dec 7, 2010 at 20:42
  • 1
    JavaScript's purpose is not to run in a browser. Hell, it's a nicer language than PHP in many ways. I run event-driven servers that do image processing.. written in JavaScript. Commented Dec 7, 2010 at 20:48

7 Answers 7

3

Unless you're talking about some form of client side PHP or server side JavaScript, then what you're referring to won't work.

PHP is executed on the server side.

JavaScript is executed on the client side.

One can't "call a method" on another because the PHP has already executed before the JavaScript can be called by the client.

The only way to make something like that work would be to provide some way for the JavaScript to execute and then post the result of the method back to the server via an AJAX call.

Sign up to request clarification or add additional context in comments.

1 Comment

I am referring to running both PHP and JavaScript server-side, from a command line script.
2

Well technically you could do what you want.

  • Find a command line JS interpreter, this might be a good starting point
  • Make a JS script that accepts input and produces output
  • Start doing whatever you are doing in php
  • Throw a $output = system('./jsInterpreter -param value', $retval); or similar
  • Do something with data

EDIT: Rhino seems to be a perfect match for your needs:

Predefined Properties

Scripts executing in the shell have access to some additional properties of the top-level object. arguments

The arguments object is an array containing the strings of all the arguments given at the command line when the shell was invoked.

1 Comment

Okay...now we're getting somewhere. This is along the lines of what I will need to do.
1

I think this is what you are looking for http://devzone.zend.com/article/4704. Using JavaScript in PHP with PECL and SpiderMonkey

This example is not even close to tested, but I think should guide you in the right direction.

-- funcs.js
function add(a,b) {
  return a +b;
}

-- use-js-funcs.php
<?php

// create JavaScript context
$js = new JSContext();

$js_code = file_get_contents('func.js');

// define Script code
$js_code.= "\n add(5)";

// evaluate script and display result, 
// my guess is that evaluateScript returns the
// result of the last statement that was executed
echo $js->evaluateScript($script);
?>

1 Comment

thanks, but I didn't want to have to mess with PECL and compiling/installing. Although, your answer seems like a very appropriate solution.
0

PHP is executed on your server and the output is sent to the browser. The browser executes the script as provided by PHP. No, PHP cannot call a JavaScript function - it can only serve a page to the browser that contains JavaScript that the browser will execute.

This script could contain an AJAX callback that tells the browser to initiate a new request to a server PHP script - but this constitutes a separate and distinct request to the server.

1 Comment

both the php and javascript will need to be run server side. No client-side (browser) code will be generated.
0

Does the script need to run client-side? You could write a function that runs and calls the JS library as soon as the page loads, and then sends the return value to your script via an XHR call. You could also set a timeout to have the function run and do this after a specified period of time.

1 Comment

neither script will be run client-side
0

If you want this to work, you're going to have to initiate the call via JavaScript using Ajax.

Here's what I do:

First, create a PHP file that does all the work you need it to do and then have it echo out a simple result set. Something like:

echo 'true';

The key is to be simple. If you need more than just delineate each result with a space, pipe, or coma, that way you can break it into an array later.

Second, you need to make a javascript call with ajax. Here's what I do:

var result = null;
var scriptUrl = "your-php-page.php";
$.ajax({
   url: scriptUrl,
   type: 'get',
   dataType: 'html',
   async: false,
   success: function(data) {
       result = data;
   }
});
return result;

Then you can do whatever you need to with the returned data. You can even send it to another PHP page if you need to.

1 Comment

there's no client side in this question
0

PHP can only do pre-process functions so I believe the only way to call a JavaScript function in a page would be to print out the JavaScript call:

?>
<script>
 functionToCall();
</script>
<?PHP

In that functionToCall you can use an AJAX function or form submit to send data back to your PHP code to process.

2 Comments

both the php and javascript will need to be run server side. No client-side (browser) code will be generated.
Then you're looking for a server-side javascript implementation? Check out CommonJS: en.wikipedia.org/wiki/CommonJS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.