nhenrique
nhenrique

Reputation: 894

PhantomJS not rendering when executed from PHP

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();
});

Upvotes: 4

Views: 5243

Answers (1)

FredTheWebGuy
FredTheWebGuy

Reputation: 2586

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...

Upvotes: 2

Related Questions