用PHP解释JavaScript [英] Interpreting JavaScript in PHP

查看:121
本文介绍了用PHP解释JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够运行JavaScript并使用PHP获得结果,并且想知道是否有一个PHP库允许我解析它。我的第一个想法是使用node.js,但由于node.js可以访问套接字,文件和东西,我认为我宁愿避免这种情况。



基本原理:我正在使用PHP进行屏幕抓取,并且遇到了很多场景,其中数据是由前端的JavaScript生成的,我想避免编写专门的过滤函数来对每个JavaScript进行操作因为这需要很多时间。更一般的情况是直接解析JavaScript。



Downvoting:我真的没有看到这个问题有什么争议,现代已知网络爬虫可以做到这一点,唯一的区别是它们往往不是用PHP编写的。 [1]



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

解决方案

这是一个有趣的问题,而且选民对潜在的使用案例缺乏想象力。页面存档工具,打印脚本,预览图像 - 所有正当理由都希望使用页面中包含的JavaScript来操作文档。



我不知道有任何现有的PHP实现,但您可以将Mozilla的 SpiderMonkey 作为PHP模块或作为独立工具进行调整操作DOMDocument并返回结果。



我没有使用服务器端JavaScript的经验,但我认为可能需要处理一些问题:




  • 主机对象如文件窗口不是ECMAScript规范的一部分(这些是实现浏览器提供的对象),因此您需要确保该库提供等效的主机对象。

  • 您可能遇到安全问题围绕在服务器端环境中执行客户端脚本。这很像允许用户提交PHP脚本进行评估,所以你需要确保安全沙箱很紧。



<另一个(可能)更安全且更容易实现的选项可能是使用经过修改的FireFox或WebKit实例作为浏览器运行,加载目标页面并将已修改的源返回给您的应用程序。 / p>

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. 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.

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. The more general case would be to parse the JavaScript directly.

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. [1]

[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.

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.

I haven't had experience with server-side JavaScript, but some issues that I believe might need to be dealt with:

  • 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.
  • 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.

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.

这篇关于用PHP解释JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆