phtomjs在PHP中执行时不渲染

PhantomJS not rendering when executed from PHP

本文关键字:执行 PHP phtomjs      更新时间:2023-09-26

phanttomjs新手决定用这个来打印网页截图。从终端一切工作正常,但当从PHP脚本执行shell_exec函数渲染不工作。

这只是从PHP执行phantom的部分。与shell_exec一起执行的其他命令可以工作,只是不包括渲染。

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

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

这可能产生更可预测的结果-

对于测试目的,只需使用:

 exec("phantomjs phantom.js");

确保你在文件夹中有一个可执行的phantomjs 和你正在执行的php脚本。

其次,丢掉$output变量。我尝试了与您所尝试的类似的方法,但它不起作用—您的幻影脚本在当前状态下不会返回任何内容,并且shell_exec因其不可预测和不安全的性质而逐渐被弃用。我的意思是,shell_exec充其量只是临时的。

第三,为了测试,CHMOD你的文件夹到"777"。或者将页面呈现的输出保存到具有写权限的文件夹中。

至于从PhantomJs脚本返回可用数据(读取:可用于多个并发用户,因为这是一个缓慢和阻塞的操作)到PHP脚本....嗯…