7

I'm using phantomJS for the purposes of programmatically taking screenshots of a webpage. My webserver runs on Linux 64 bit.

The Scenario

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.

The Issue

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:

  • Am I right in my assumption?
  • Or is it because of some restriction on my webserver which is blocking exec() from accessing the .js file through absolute URL?
403 Forbidden
  • 91
  • 1
  • 4

4 Answers4

3

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.

paul
  • 488
  • 5
  • 16
  • 1
    unsetting DYLD_LIBRARY_PATH worked for me too. If anyone knows WHY, i'm open for some explaining. But thanks for this! – Henrik Peinar Mar 10 '14 at 16:23
  • This was the only way I could get my project to run locally on OSX Mavericks (using MAMP). When running the same project remotely on an Ubuntu server, I didn't need any of this sorcery. – lewsid Oct 01 '14 at 20:38
  • This was also the solution for me on OSX Mavericks with MAMP. Interestingly it worked on Yosemite without unsetting DYLD_LIBRARY_PATH. Interested in the reasoning if anybody knows. – antriver Mar 21 '15 at 21:27
1

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.

Community
  • 1
  • 1
Preston S
  • 2,751
  • 24
  • 37
0

Try to place test.js in folder where test.php located (when calling exec('./phantomjs ./test.js', $o, $e);) or use full path.

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
  • As I pointed out in my post test.js and test.php are in the same folder already. Also I'm facing issues when I'm using full path. – 403 Forbidden Oct 24 '13 at 11:45
0

I found the issue to be with selinux (which is now disabled by default on all production server builds as standard).

in file /etc/selinux/config find line containing:

SELINUX=

and change it to this:

SELINUX=disabled

run command (to take immediate effect without reboot)

/usr/sbin/setenforce 0

ashgallery
  • 71
  • 1
  • 3