参考:
1.下载
2.安装依赖
yum install -y bzip2 # 安装bzip2
yum install -y fontconfig freetype2
yum install bitmap-fonts bitmap-fonts-cjk
yum groupinstall "fonts" -y # 安装字体相关的依赖包
fc-cache # 刷新字体缓存
3.安装
tar -jxvf phantomjs-2.1.1-linux-x86\_64.tar.bz2
mv phantomjs-2.1.1-linux-x86\_64 /usr/local/src/phantomjs
ln -sf /usr/local/src/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
4.调试
查看版本
phantomjs -v # 测试版本号
测试脚本test.js (返回指定网页内第一个div的内容)
var page = require('webpage').create(),
system = require('system'),
address;
address = system.args\[1\];
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var ua = page.evaluate(function () {
return document.getElementsByTagName('div')\[0\].textContent;
});
console.log(ua);
}
phantom.exit();
});
执行脚本
phantomjs test.js http://xxx.com/test.html
5.在PHP中调用
exec("phantomjs /script/test.js http://xxx.com/test.html");