PhantomJS not executing when executing PHP

New to phantomjs decided to use it to print screenshots from web pages. Everything works fine from the terminal, but when executed with a PHP script using the shell_exec function, the render does not work.

this is only the part running phantom from php. Other commands executed with shell_exec work, not a render.

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

this is a phantom script that works great when executed in 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
source share
1 answer

This can lead to more predictable results -

For testing purposes, simply use:

  exec("phantomjs phantom.js"); 

Make sure phantomjs are running in the same folder as the php executable script.

Second, lose the $ output variable. I tried something similar to what you tried, and it won’t work - your phantom script will not return anything in its current state, and shell_exec is on the way to obsolescence for its unpredictable and unreliable nature. IMHO shell_exec is hacker and temporary at best.

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

As for the returned usable data (read: it can be used for several simultaneous users, since this is a slow and blocking operation) from the PhantomJs script to your PHP script .... well ... here's the problem: ...

+2
source

All Articles