简体   繁体   English

用PHP解释JavaScript

[英]Interpreting JavaScript in PHP

I'd like to be able to run JavaScript and get the results with PHP and is wondering if there is a library for PHP that allows me to parse it out. 我希望能够运行JavaScript并使用PHP获得结果,并且想知道是否有一个PHP库允许我解析它。 My first thought was to use node.js, but since node.js has access to sockets, files and things I think I'd prefer to avoid that. 我的第一个想法是使用node.js,但由于node.js可以访问套接字,文件和东西,我认为我宁愿避免这种情况。

Rationale: I'm doing screen scraping in PHP and have encountered many scenarios where the data is being produced by JavaScript on the frontend, and I would like to avoid writing specialized filtering functions to act on the JavaScript on a per-case basis since that takes a lot of time. 理由:我正在使用PHP进行屏幕抓取,并且遇到了许多场景,其中数据是由前端的JavaScript生成的,我希望避免编写专门的过滤函数来基于每个案例对JavaScript进行操作,因为花了很多时间。 The more general case would be to parse the JavaScript directly. 更一般的情况是直接解析JavaScript。

Downvoting: I don't really see what's so controversial about this question, modern web crawlers are known to do it, the only difference is that they tend to not be written in PHP. Downvoting:我真的没有看到这个问题有什么争议,现代网络爬虫已经知道了,唯一的区别是它们往往不是用PHP编写的。 [1] [1]

[1] http://blogs.forbes.com/velocity/2010/06/25/google-isnt-just-reading-your-links-its-now-running-your-code/ [1] http://blogs.forbes.com/velocity/2010/06/25/google-isnt-just-reading-your-links-its-now-running-your-code/

It's an interesting question and the down-voters are being unimaginative about potential use-cases. 这是一个有趣的问题,而下行选民对潜在的使用案例缺乏想象力。 Page archiving tools, printing scripts, preview images - all valid reasons to want to manipulate a document with the JavaScript included within the page. 页面存档工具,打印脚本,预览图像 - 所有正当理由都希望使用页面中包含的JavaScript来操作文档。

I'm not aware of any existing PHP implementations, but you could probably adapt Mozilla's SpiderMonkey as a PHP module, or as a standalone tool to manipulate a DOMDocument and return the result. 我不知道任何现有的PHP实现,但你可能会将Mozilla的SpiderMonkey改编为PHP模块,或者作为操作DOMDocument并返回结果的独立工具。

I haven't had experience with server-side JavaScript, but some issues that I believe might need to be dealt with: 我没有使用服务器端JavaScript的经验,但我认为可能需要处理一些问题:

  • Host objects like document and window are not part of the ECMAScript specification (these are objects provided by the implementing browser) so you need to make sure that the library provides equivalent host objects. documentwindow等主机对象不是ECMAScript规范的一部分(这些是实现浏览器提供的对象),因此您需要确保库提供等效的主机对象。
  • You might have security issues around executing client side scripts within a server side environment. 您可能在服务器端环境中执行客户端脚本时遇到安全问题。 This is a lot like allowing the user to submit a PHP script to be evaluation, so you need to make sure the security sandbox is tight. 这很像允许用户提交PHP脚本进行评估,因此您需要确保安全沙箱是紧密的。

Another (perhaps) safer and easier to implement option might be to use a modified FireFox or WebKit instance that runs as a browser, loading up the target pages and returning the modified source to your application. 另一个(可能)更安全且更容易实现的选项可能是使用经过修改的FireFox或WebKit实例作为浏览器运行,加载目标页面并将修改后的源返回给您的应用程序。

From PHP 5.3 you can use V8JS extention from PHP . 从PHP 5.3开始,您可以使用PHP的V8JS扩展 It's a native library that uses the new Google V8 Javascript engine to execute JS and return the result. 它是一个本机库,它使用新的Google V8 Javascript引擎来执行JS并返回结果。

It's good because you can pass vars in PHP arrays and are interpreted very well 这很好,因为你可以在PHP数组中传递变量,并且解释得非常好

NodeJS (or some other derivative of google's v8) might actually be the best way to go here. NodeJS(或谷歌v8的其他衍生产品)实际上可能是最好的方式。 If you're concerned about the various things nodejs can do (eg. sockets, etc), you can probably "strip it down" by removing modules and/or addons -- I think even the built in stuff is ultimately implemented in such a way that it could be stripped out fairly easily. 如果你担心nodejs可以做的各种事情(例如套接字等),你可以通过删除模块和/或插件来“删除它” - 我认为即使内置的东西最终都是在这样的它很容易被剥离出来的方式。

An alternate approach might be to simply replace, override, or remove the require function from node.js . 另一种方法可能是从node.js简单地替换,覆盖或删除require函数。

There's also envjs which should make it easier to run js that was designed to run the browser. 还有一些envjs应该可以更容易地运行用于运行浏览器的js。

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

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