周梦康 发表于 2014-04-10 7614 次浏览 标签 : casperjs
$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 test.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);

js代码

var casper = require('casper').create();
if(casper.cli.has(0)){
	var id = casper.cli.get(0);
	//casper.echo(id);
	casper.start("http://test.xxx.org/index.php?g=Contents&m=Index&a=screenshot&id="+id, function() {
		this.captureSelector(id+'.png', '.content');
	});
	casper.run();
}else{
	casper.exit();
}

命令行下,执行/usr/local/bin/casperjs test.js 124非常OK,但是通过php来执行就报错啦Fatal: [Errno 2] No such file or directory; did you install phantomjs?

通过不懈努力终于找到了问题所在,参考链接:http://stackoverflow.com/questions/21563640

这样写就没问题了

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

👇 下面是我的公众号,高质量的博文我会第一时间同步到公众号,给个关注吧!

评论列表