简体   繁体   English

我可以通过PHP执行js文件吗?

[英]Can I execute js files via php?

The situation is next: 情况如下:

I have php file, which parses a web-page. 我有php文件,它解析一个网页。 on that web-page is a phone number and it's digits are mixed with each other. 在该网页上是一个电话号码,它的数字是相互混合的。 The only way to put each digit on the correct place is to use some JS functions (on the client side). 将每个数字放在正确位置的唯一方法是使用一些JS函数(在客户端)。 So, when I execute that php file in linux console, it gives me all that I need, except js function's result (no wonder - JavaScript is not a server-side language). 所以,当我在linux控制台中执行那个php文件时,它给了我所需要的一切,除了js函数的结果(难怪 - JavaScript不是服务器端语言)。 So all I see from JS - only a code, that I have written. 所以我从JS看到的只是一个代码,我写的。

The question: can I execute js files via php and how? 问题:我可以通过php执行js文件吗?

Results of a quick google search (terms = javascript engine php ) 快速谷歌搜索的结果(terms = javascript engine php

  1. J4P5 -- not developed since 2005 [BAD](according to its News) J4P5 - 自2005年以来未开发[BAD](根据其新闻)
  2. PECL package spidermonkey PECL包spidermonkey
  3. a 2008 post by jeresig points to PHPJS but can't see when it was last updated. jeresig在2008年的一篇文章中指出了PHPJS,但是看不到它上次更新的时间。

I'm sure you'll find many more links on that google search. 我相信你会在google搜索上找到更多链接。

Alternatively: 或者:

you say that the digits are scrambled and you need to "unscramble" them using js. 你说数字是乱码的,你需要用js“解读”它们。 Can you code that unscrambling logic into a PHP function and just use it? 你可以将解扰逻辑编码到PHP函数中并使用它吗? Will sure save you a lot of trouble, but if learning to use js in php is what you're after, then its a whole different story... 肯定会为你省去很多麻烦,但是如果学习在php中使用js就是你所追求的,那么它就是一个完全不同的故事......

http://en.wikipedia.org/wiki/Comparison_of_Server-side_JavaScript_solutions http://en.wikipedia.org/wiki/Comparison_of_Server-side_JavaScript_solutions

Alternatively, PHP has simple functions for executing other programs and retrieving their output (I used this along with GPG once to create a PHP file manager which could live-encrypt files as you were uploading them and live-decrypt as you were downloading them) 或者,PHP具有执行其他程序和检索其输出的简单功能(我使用它与GPG一起创建一个PHP文件管理器,可以在上载文件时对文件进行实时加密,并在下载时实时解密)

Using those functions along with http://code.google.com/p/v8/ you should be able to interpret any javascript. 使用这些功能以及http://code.google.com/p/v8/,您应该能够解释任何JavaScript。

Not unless you know someone who's implemented a Javascript engine in PHP. 除非你知道有人在PHP中实现了Javascript引擎。

In other words, probably not. 换句话说,可能不是。

Without some sort of browser emulation or passing the unparsed js off to a server side implementation of javascript (maybe node.js?), you won't be able to execute it. 如果没有某种浏览器仿真或将未解析的js传递给javascript的服务器端实现(可能是node.js?),您将无法执行它。

However, does the page use the same js function to unscramble the phone number every time? 但是,页面是否每次使用相同的js函数来解读电话号码? You should be able to read the incorrect digits and shuffle them with PHP. 您应该能够读取错误的数字并使用PHP对其进行随机播放。

如果你准备做一些工作来构建你自己的JS运行时来使用它, Tim Whitlock用纯PHP编写了一个javascript标记器和解析器

node.js is server-side... but full JS :) no PHP in it so I don't it answer your needs... node.js是服务器端...但完整的JS :)没有PHP,所以我不回答你的需求...

Anyway, here is an example : a chat in JS both client & server-side : http://chat.nodejs.org/ 无论如何,这是一个例子:在客户端和服务器端的JS聊天: http//chat.nodejs.org/

Plus, not every host allows you to use the v8 engine... 此外,并非每个主机都允许您使用v8引擎......

If you have Javascript data objects, and you need to convert them to/from PHP arrays, that's quite easy using PHP's json_encode() and json_decode() functions. 如果您有Javascript数据对象,并且需要将它们转换为PHP数组或从PHP数组转换它们,那么使用PHP的json_encode()json_decode()函数就很容易了。

But actually running Javascript code? 但实际上运行Javascript代码? No. You can't. 不,你不能。 You might be able to find a JS interpreter written in PHP (a few other answers have pointed a links that may or may not help you here), or more likely execute the JS using a stand-alone JS interpreter on your server which you call out to from PHP. 您可能能够找到用PHP编写的JS解释器(其他一些答案指出了可能会或可能不会帮助您的链接),或者更可能使用您调用的服务器上的独立JS解释器来执行JS来自PHP。 However if the JS code includes references to the browser's DOM (which is highly likely), that's a whole other set of issues which will almost certainly make it impossible. 但是,如果JS代码包含对浏览器DOM的引用(很有可能),那么这是另外一组问题,这几乎肯定会使它变得不可能。

Given the way you describe the question, I'd say the easiest solution for you would just be to re-implement the JS code as PHP code; 鉴于您描述问题的方式,我认为最简单的解决方案就是将JS代码重新实现为PHP代码; it's unlikely that all the work arounds being suggested would be appropriate for what sounds like a fairly simple bit of utility code. 所提出的所有工作都不太可能适用于听起来像一个相当简单的实用程序代码。

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

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