Cannot use PHP exec() to call Phantomjs from Browser


#1

Have you ever spent a ridiculous amount of time on a problem while knowing that you alone must be doing something wrong because of the lack of anyone else having your issue anywhere on the interwebs?

I think this falls into that category.

Problem: I am trying to execute an external call to phantomjs from a php script. When the command or the php script are ran from the command line, it works like a newly oiled engine. When the php script is called from a browser, phantomjs crashes.

I have downloaded both the 32-bit and 64-bit latest versions (1.8.2) to verify it was not simply a problem with either on the DH server.

The test command is: /home/myusername/my.com/manual/phantomjs /home/myusername/my.com/manual/hello.js

“2>&1” is appended to the end when called from the php script to display errors.

The PHP test script is simply:

[php]<?PHP
$cmd = “/home/myusername/my.com/manual/phantomjs /home/myusername/my.com/manual/hello.js 2>&1”;
exec($cmd);
?>[/php]

  1. When the test command is called directly from the command line, it works perfectly.
  2. When the php script is called from the command line (php php.test), it works perfectly.
  3. When the php script is navigated to from the browser, I get:

Intriguing facet: If the command for simply requesting the version number is called (phantomjs -v), all three types of calls work.

I have tried the following in attempting to figure out the cause:

  • Redownloaded the binaries
  • Set the permissions to 777 for all files and directories involved.
  • Used both relative and absolute paths.
  • Verified through exec(“whoiam”) that it is indeed using my primary user.
  • Turned off Extra Web Security and FastCGI
  • Sacrificed to multiple deities.

This fine young person had a different problem when trying to do the same, but it’s worth noting: https://discussion.dreamhost.com/thread-137268.html?highlight=phantomjs

I would be happy, or even quite ecstatic, if you could offer any help about what I am clearly, and probably foolishly, doing wrong. Thanks!


#2

Solved: For the sad single soul that spent to much time tearing (both meanings) at this troublesome problem, I can offer sort of a simple solution.

First, Phantomjs needs to be the 32-bit version even though the server itself runs in 64-bit. The DH increased security and FastCGI do not matter. Absolute paths do not matter. Besides that, a mix of the above might be necessary.

Best of luck!


#3

For me the 64 bit version resulted in segfaults and core dumps when trying to load via browser with PHP. The 32 bit version returned stdout when prefixing a relative path, e.g. passthru(’./phantomjs hello.js’)


#4

@juicio, I’m very frustrated with this issue. I’m very close to finishing an application and all the phantomjs scripts I built work fine from the command line but crash through php exec through the browser and I get the exact same error message as you. I tried your solve, but unfortunately I get a new error message when attempting to run phantomjs in 32 bit:

I tried installing libfreetype.so.6 to no avail, so now I’m assuming it’s because I’m trying to run a 32 bit app on a 64 bit OS. Regardless, that doesn’t solve the problem for me.

Any ideas from here? Is there ANY sort of work around for this? I even went back to running 64 bit phantomjs and tried running a php script from the command line through exec(), but it threw the same original error:

I think it also told me I was not l337 enough to use the app too, while simultaneously pouring sugar in my gas tank. But that could have been the onset hallucinations from staying up way past my bedtime on this…


#5

#6
/bin/ls: cannot access /usr/lib32/libfreetype*: No such file or directory

# locate libfreetype
/usr/lib64/libfreetype.so
/usr/lib64/libfreetype.so.6
/usr/lib64/libfreetype.so.6.3.22
/usr/local/cpanel/3rdparty/lib/libfreetype.a
/usr/local/cpanel/3rdparty/lib/libfreetype.la
/usr/local/cpanel/3rdparty/lib/libfreetype.so
/usr/local/cpanel/3rdparty/lib/libfreetype.so.6
/usr/local/cpanel/3rdparty/lib/libfreetype.so.6.8.1

Looks like it’s there… why would phantomjs 32 bit still fail?


#7

Looks like you only have the 64b version. And CPanel ?? =\

Try grabbing/making a 32b version and put it in your PATH.


#8

zoos, that sounds pretty convincingly like you’re not on DreamHost. Contact support for your host, whoever that is.


#9

Yea, I’m trying to trick you guys into thinking I’m on DreamHost LOL

It’s true I’m not, but I am looking for help with getting phantomjs to work through PHP so I thought I’d give it a shot, and since I got here through a google search I figured someone else with the same problem might benefit from whatever solution we came up with here.

Thanks for the help, sXi!