I'm trying to run PhantomJS using PHP on a Linux web server. This will eventually run as a cron job but for now I'm running it through a PHP script that I am uploading via FTP to the server.
PHP script:
$cmd = '/usr/local/bin/phantomjs /home/accountname/public_html/myscript.js';
$result = exec($cmd);
var_dump($result);
(Note that I have changed the account name in the path above to "accountname" for privacy but this is correct in my real script - and is copied from a myriad of other PHP scripts which all work perfectly.)
myscript.js content:
console.log('Hello, world!');
phantom.exit();
The resulting output is:
string(0) ""
I have tried a number of examples from the PhantomJS website an I never get any output - either on the screen, in the console or for the examples that create screenshots the PNG files never appear in the right place - even though I am specifying exact paths.
I did try returning the PhantomJS version and this returned the correct version:
$cmd = '/usr/local/bin/phantomjs --version';
$result = exec($cmd);
var_dump($result);
Returned:
string(5) "1.9.2"
I know that "exec" is not a safe way of running things in PHP - but I'm just trying to test that PhantomJS works to get started, and then we'll adjust everything to work safely.
Any ideas? Thanks in advance.
Try use:
$cmd = 'unset DYLD_LIBRARY_PATH ;';
$cmd.= ' /usr/local/bin/phantomjs'; //where is isntalled phantomjs
$cmd.= ' /var/www/rasterize.js'; //where is your script
exec($cmd.' 2>&1', $output);
print_r($output);
This script was tested on Ubuntu 12.04 with php 5.4.