4

New to phantomjs decided to use this to print screenshots from webpages. From the terminal everything works fine but when executing from PHP script with shell_exec function the render doesn’t work.

this is just the part that is executing phantom from PHP. Other commands executed with shell_exec work, just not the render.

$output = shell_exec("phantomjs phantom.js");
echo $output;

this is the phantom script that works fine when executed on the shell

var page = require('webpage').create();
page.open( "http://www.google.co.uk" , function(s){
    var title = page.evaluate(function(){
        var main = document.getElementsByTagName("center");
        main[0].style.visibility = "hidden";
        return document.title;
    });
    console.log("rendering now");
    page.render("title" + ".png");
phantom.exit();
});
4
  • Try adding absolute paths to your shell_exec call. Commented Apr 19, 2013 at 16:39
  • it didn't change anything :/ Commented Apr 19, 2013 at 19:05
  • Could you add the updated command to your question, please? And just to check - are you running your PHP from the command line (so it's running with your user permissions), or are you running it via a webserver? Commented Apr 19, 2013 at 19:07
  • Have in mind that on debian for example, bash php calls and apache php calls load different php.ini's. You might have blocked shell_exec / paths / safe_modes in /etc/php5/apache2/php.ini, but not in /etc/php5/cli/php.ini Commented Jun 27, 2013 at 10:59

1 Answer 1

2

This may produce more predictable results-

For testing purposes just use:

 exec("phantomjs phantom.js");

Make sure you have phantomjs execuatable in the same folder as your executing php script.

Secondly, lose the $output variable. I tried something similar to what you tried and it wouldn't work- Your phantom script won't return anything in its current state, and shell_exec is on its way to becoming deprecated for its unpredictable and insecure nature. IMHO shell_exec is hackish and temporary at best.

Thirdly, CHMOD your folder to "777" for the sake of testing. Or save the output of the page render to a folder with write permissions.

As far as returning usable data (read: usable for several concurrent users as this is a slow and blocking operation) from the PhantomJs script to your PHP script.... well... herein lies the challenge...

Sign up to request clarification or add additional context in comments.

3 Comments

I don't see the challenge "as far as returning usable data from the PhantomJs script to your PHP script". Use PHP's tmpfile or tempnam to generate a temp file, pass it as an argument to the phantomjs script and modify the script to write output to that file. When the phantomjs script returns, open, read and delete the file.
I guess I was more concerned about the expense of the above process when I answered this. Yes, what you've described is easy to do, but it is very expensive, and basically useless without drumming up a process to make this non-blocking. If you are generating a fairly large screenshot that takes 30 secs to render, watch what happens to your resources when you have more than 5 people doing the same thing. Executing an exec command will work for quick prototyping and personal projects just fine, though.
Well a screenshot image inherently is a file, sure it might be streamed back under other circumstances, but having an actual file that can be re-fetched without the generation step might be nice. I'm facing a similar dilemna but it's not a screenshot but rather the generated DOM output, in which case I would like to just stream it back, but am instead going to go for unnecessarily heavy temp file approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.