本文介绍了在服务器上安装 PhantomJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 上使用 PhantomJS 并通过命令行访问文件.现在我想将它安装在我在本地主机上工作的服务器上,所以我希望将它安装在 Apache 上,以便我可以将 Phantom 与我的 html 和 PHP 集成在一起,以便在网页中使用.我使用 Windows 8.1 和 Apache 服务器作为本地主机.

I'm using PhantomJS on windows and accessing the files through command line. Now I want to install it on the server I'm working on local host so I want it to be installed on Apache, so that I may integrate Phantom with my html and PHP to be used within a web page. I'm using Windows 8.1 and Apache Server for localhost.

我真的可以这样做吗?现在如何在Web服务中使用它?如果我必须让我的网站在线,我将如何使用它?我不应该让我的托管服务提供商帮我把这个东西放在服务器上吗?

Can I actually do that? How to use it in web services now?How i will use it if I have to make my web online? should I have t ask my hosting provider to place this thing on server for me?

我正在使用 phantomJS 开发一个 Web 服务,该服务将 url 作为输入并返回网站屏幕截图的图像文件.

I'm using phantomJS to develop a web service which takes a url as input and returns an image file of the screen shot of the website.

推荐答案

PhantomJS 的安装"只是将其路径添加到 PATH 环境变量中.

The "installation" of PhantomJS is just adding its path to the PATH environment variable.

由于您使用的是 php,因此您可以从 php 脚本中调用 PhantomJS,如下所示 此处 您需要在此处提供 PhantomJS 可执行文件的完整路径.您还可以使用 putenv 直接从 php 扩展 PATH,如此处所示.
你可以在 exec$output 变量中从 PhantomJS 脚本返回一些东西,然后解析它.

Since you're using php, you would invoke PhantomJS from your php script like it is shown here where you need to provide the full path to the PhantomJS executable. You can also use putenv to extend the PATH directly from php as seen here.
You can return something from the PhantomJS script in the $output variable of exec and then parse it.

PhantomJS 提供了网络服务器模块.您可以编写一个脚本来监听请求,根据请求创建 page 并返回图像.您会以某种方式在启动时或与 apache 一起运行脚本.也可以编写一个包装器,以便您可以将其作为 Windows 服务运行.可以通过 Apache 传递请求,这样 PhantomJS 就不会完全对外开放.
虽然这个选项有点危险,因为 PhantomJS 可能会失败,然后你需要某种恢复机制.您的脚本也可能会遇到内存泄漏.

PhantomJS provides the web server module. You can write a script that listens for requests, creates the page on request and returns the image. You would somehow run the script at startup or together with apache. It's also possible to write a wrapper so that you can run it as windows service. It may be possible to pass the requests through Apache so that the PhantomJS is not completely open to the outside.
Although this option is a little treacherous, because PhantomJS may fail and then you would need some kind of reviving mechanism. Your script may also run into a memory leak.

以上两个选项都与您返回图像的方式无关.有几种可能性.

Both of the above options are agnostic to how you return the image. There are several possiblities.

  1. 根据要求,创建一个随机且唯一的文件名并render 页面进入文件.然后,您可以将文件名发送给客户端,客户端随后可能会请求它.这需要两个请求.
  2. 根据请求,通过 renderBase64 呈现页面 接收纯图像数据.现在您可以直接在响应中发送此图像数据,客户端可以通过 将其放入 DOM数据:-URI.

这篇关于在服务器上安装 PhantomJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:12