简体   繁体   中英

Is it possible to include and call a JavaScript function from a PHP script? And how?

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

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.

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.

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);
?>

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM