版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wt1286331074/article/details/86083867
参考:https://blog.csdn.net/libsyc/article/details/78199850(win)
https://www.cnblogs.com/firstdream/p/5123034.html(linux)
http://phantomjs.org/screen-capture.html(小参考)
https://www.cnblogs.com/gaohj/p/3267692.html(php执行外部程序)
先来win版: 首先去官网下载http://phantomjs.org/download.html这个win版,然后把这个bin下的phantomjs.exe路径放到环境变量里面,然后在bin下新建个js文件
var page = require('webpage').create();
page.open("http://www.cnblogs.com/front-Thinking/", function(status) {
if ( status === "success" ) {
console.log(page.title);
page.render("front-Thinking.png");
} else {
console.log("Page failed to load.");
}
phantom.exit(0);
});
然后cmd到bin文件夹下执行phantomjs hello.js
出来front-Thinking - 博客园这个就成功了。
再来linux版,首先去那个官网下载,我这个服务器下载出现403,然后win电脑下了直接通过ftp传到服务器,解压
ln -s ~/bin/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
建立快捷连接,然后直接phantomjs -v,没反应
chmod +x /usr/local/bin/phantomjs // 设为可执行
安装依赖和字体库
yum install -y fontconfig freetype2 bitmap-fonts bitmap-fonts-cjk
最后新建js脚本a.js,需要传参(url,path)
var page = require('webpage').create(),
system = require('system'),
t, address;
if (system.args.length === 1) {
console.log('Usage: loadspeed.js <some URL>');
phantom.exit();
}
t = Date.now();
address = system.args[1];
path = system.args[2];// 设置的接受参数
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
page.render(path);
t = Date.now() - t;
console.log('Loading time ' + t + ' msec');
}
phantom.exit();
});
最后执行phantomjs a.js http://www.baidu.com /usr/local/phantomjs/phantomjs-2.1.1-linux-x86_64/bin/ha.jpg
然而在php里面执行的话需要先把php.ini里面的safe_mode设置为Off,我的win电脑因为几天没关,然后想试下带参数用exec函数访问,然后这个浏览器一直的打转,最后我输出333都不行,重启下电脑就好了,最后原来想试下
exec('C:/phantomjs-2.1.1-windows/bin/phantomjs hello.js http://www.baidu.com test/hahah.jpg',$out,$res);
print_r($res);
die;
// 尼玛上面这个里面不好放变量,还是要用shell_exec,下面这个是他的变体,搞死爸爸了,url不能这样写xxx.com?id=1,记得背景色设置白色
echo `$cmding`;