Installing PhantomJS on server

2019-01-23 21:29发布

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.

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?

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.

2条回答
聊天终结者
2楼-- · 2019-01-23 22:06

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

Running through php

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.

Running through the webserver module

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.

Returning the image

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

  1. On request, create a random and unique filename and render the page into the file. You can then send the name of the file to the client which might request it afterwards. This needs two requests.
  2. On request, render the page through renderBase64 to receive the plain image data. Now you can send this image data directly in the response and the client may put it into the DOM via data:-URI.
查看更多
做自己的国王
3楼-- · 2019-01-23 22:17
//throws a lot of errors because searching some libraries
$cmd = 'unset DYLD_LIBRARY_PATH ;';
$cmd.= ' /abs/path/to/phantomjs';
$cmd.= ' /abs/path/to/script.js';

//set environment variable to node source
putenv('PATH=/abs/path/to/node/bin/');

//now exec the cmd and pipe the errors to stdout
exec($cmd.' 2>&1', $output);

//and output the results
print_r($output);

here is the answer from the above url you suggested. I've changed it accordingly to mine paths

<?php

$cmd = ' ./ScreenShotWeb/phantomjs';
$cmd.= ' ./ScreenShotWeb/shot.js';

putenv('PATH=./ScreenShotWeb/');

exec($cmd.' 2>&1', $output);

print_r($output);
?>

I'm trying to run this but it gives the following out put

Array ( [0] => '.' is not recognized as an internal or external command, [1] => operable program or batch file. )

in my root folder (C:/xampp/htdocs) there is a folder ScreenShotWeb where I've placed EXE of PhantomJS + all the files.

查看更多
登录 后发表回答