I'm using phantomJS for the purposes of programmatically taking screenshots of a webpage. My webserver runs on Linux 64 bit.
My test.php
file
exec('./phantomjs --version', $o, $e);
print_r($o);
echo $e;
I open test.php
in a browser. The out put I get is:
1.9.1 // version number
0 // exit code
This proves that I can run commands through exec()
and phantomJS
is working perfectly.
Now when I replace the above code with:
exec('./phantomjs http://mywebsite.com/test.js', $o, $e);
print_r($o);
echo $e;
The output is:
Array ( ) // empty output
139 // exit code which on investigating turned out to be segmentation fault
I also tried:
exec('./phantomjs ./test.js', $o, $e); // since phantomjs and test.js are in same folder
but the result was the same (segfault)
test.js code:
var page = require('webpage').create();
var url = 'http://www.rediff.com/';
page.open(url, function (status) {
phantom.exit();
});
This makes me believe that using the full path as the second argument of phantomJS
is causing it to crash. Thus, the things that I'm wondering are:
exec()
from accessing the .js
file through absolute URL?After a lot of searching and testing I got it to work with following additions:
//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);
I'm not the best server admin, so I can not explain everything in detail, but the lines above generate an pdf. Yeah.
I had a similar issue. PHP + PhantomJS Rasterize I found that phantomjs does not like running from an apache process. Try running your exec command from the command line:
php -r "exec('./phantomjs http://mywebsite.com/test.js', $o, $e); print_r($o); echo $e;"
If this works you have a few options:
1.) Some have suggested modifying sudoers to give no password sudo permissions for apache user to phantomjs binary
2.) Do like I did and run your script as a cron.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With