简体   繁体   English

是否可以从PHP脚本中包含并调用JavaScript函数?如何?

[英]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. 我有一个服务器端的PHP CLI脚本。 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. 我的PHP脚本需要调用JavaScript库中的函数并捕获返回值。 I need to find a way to run the JavaScript server-side. 我需要找到一种运行JavaScript服务器端的方法。 How can I achieve this? 我怎样才能做到这一点?

Solution: 解:

Using Rhino , you can run JavaScript as a command line script . 使用Rhino ,您可以将JavaScript作为命令行脚本运行 First, download and extract Rhino . 首先, 下载并提取Rhino Then, assuming your scripts look like this: 然后,假设您的脚本如下所示:

PHP: 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: 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或服务器端JavaScript,否则你所指的是无效的。

PHP is executed on the server side. PHP在服务器端执行。

JavaScript is executed on the client side. JavaScript在客户端执行。

One can't "call a method" on another because the PHP has already executed before the JavaScript can be called by the client. 人们无法在另一个上“调用方法”,因为PHP已经在客户端调用JavaScript之前执行了。

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. 制作类似工作的唯一方法是为JavaScript提供一些方法来执行,然后通过AJAX调用将方法的结果发回服务器。

Well technically you could do what you want. 从技术上讲,你可以做你想做的事。

  • Find a command line JS interpreter, this might be a good starting point 找一个命令行JS解释器,这可能是一个很好的起点
  • Make a JS script that accepts input and produces output 创建一个接受输入并生成输出的JS脚本
  • Start doing whatever you are doing in php 开始做你在php中做的任何事情
  • Throw a $output = system('./jsInterpreter -param value', $retval); 抛出$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. 在shell中执行的脚本可以访问顶级对象的一些其他属性。 arguments 参数

The arguments object is an array containing the strings of all the arguments given at the command line when the shell was invoked. arguments对象是一个数组,包含调用shell时在命令行中给出的所有参数的字符串。

I think this is what you are looking for http://devzone.zend.com/article/4704 . 我想这就是你要找的http://devzone.zend.com/article/4704 Using JavaScript in PHP with PECL and SpiderMonkey 在PHP中使用JavaScript与PECL和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: PHP只能执行预处理功能,因此我认为在页面中调用JavaScript函数的唯一方法是打印出JavaScript调用:

?>
<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. 在该functionToCall中,您可以使用AJAX函数或表单提交将数据发送回您的PHP代码进行处理。

PHP is executed on your server and the output is sent to the browser. PHP在您的服务器上执行,输出发送到浏览器。 The browser executes the script as provided by PHP. 浏览器执行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. 不,PHP无法调用JavaScript函数 - 它只能为包含浏览器将执行的JavaScript的浏览器提供页面。

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. 此脚本可能包含一个AJAX回调,它告诉浏览器向服务器PHP脚本发起新请求 - 但这构成了对服务器的单独且不同的请求。

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. 您可以编写一个在页面加载后运行并调用JS库的函数,然后通过XHR调用将返回值发送到您的脚本。 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. 如果你希望这个工作,那么你将不得不使用Ajax通过JavaScript发起调用。

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. 首先,创建一个PHP文件,完成您需要它完成的所有工作,然后让它回显一个简单的结果集。 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. 其次,您需要使用ajax进行javascript调用。 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. 如果需要,您甚至可以将其发送到另一个PHP页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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