首先,我安装了phantomjs
test.js
的代码是:
var casper = require('casper').create();
if(casper.cli.has(0)){
var id = casper.cli.get(0);
casper.start("http://test.com/index.php?a=screenshot&id="+id, function() {
this.captureSelector(id+'.png', '.content');
});
casper.run();
}else{
casper.exit();
}
当我跑这个
/usr/local/bin/casperjs test.js 124
在命令行上是Ok,并且在目录中生成了一个图像(124.png)
但是当我使用php来执行命令时,会出现错误
我的PHP代码:
$id = 124;
$handle = popen('/usr/local/bin/phantomjs --version 2>&1','r');
$read = stream_get_contents($handle);
echo $read;//1.9.7
pclose($handle);
$handle = popen('/usr/local/bin/casperjs screenshot.js '.$id.' 2>&1','r');
$read = stream_get_contents($handle);
echo $read;//Fatal: [Errno 2] No such file or directory; did you install phantomjs?
pclose($handle);
当我通过浏览器访问php脚本时,页面输出1.9.7 Fatal: [Errno 2] No such file or directory; did you install phantomjs?
答案 0 :(得分:0)
从php execution phantom js works but casperjs does not work permission denied获得答案
在执行命令之前,应添加一个环境变量。
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs")
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
$handle = popen('/usr/local/bin/casperjs --version 2>&1','r');
$read = stream_get_contents($handle);
echo $read;
pclose($handle);
没关系!