使用PHP 执行casperjs脚本

Phantomjs & Casperjs安装

本文所述均在ubuntu环境下。

预装:npm(sudo apt-get install nodejs)

npm预装:java(?)
---------------------------------------------------------------------------

下载或者安装路径放到/usr/local/lib/下,否则可能会有权限问题,

file_exists('/usr/local/bin/phantomjs')

is_executable('/usr/local/bin/phantomjs')

这两行代码可以查看是否配合成功,如果成功返回true,否则返回false

---------------------------------------------------------------------------
 

安装Casperjs

  • npm install -g phantomjs(需要先sudo apt-get install nodejs-legacy)
  • npm install -g casperjs

打开命令行,输入:

  • phantomjs -v
  • casperjs --version

输出版本号,即安装成功

---------------------------------------------------------------

安装方法2:phantomjs下载:http://phantomjs.org/download.html

     casperjs下载:https://github.com/casperjs/casperjs

下载完成后,执行ln -s /phnatomjs/bin/phantomjs /usr/local/bin/phantomjs

ln -s /casperjs/bin/casperjs /usr/local/bin/casperjs

 

----------------------------------------------------------------

 

测试Casperjs

  • 新建文件test.js
  • 将下面代码复制到文件中:
    var casper = require('casper').create();
    casper.start('http://www.baidu.com/');
    casper.then(function() {
    this.echo('First Page: ' + this.getTitle());
    });
    casper.run();

     

  • $ casperjs sample.js
  • 输出“百度一下 你就知道”

使用php(7.0)执行casperjs

本人再使用php执行phantomjs命令的时候报错“QXcbConnection: Could not connect to display”, 本人才疏学浅,google两周未果,遂使用casperjs,直接执行成功,所以使用php执行phantomjs失败的时候, 不要埋头苦寻问题根源,不妨先试试casperjs

php代码:

 

 

 putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");//引入phantomjs
 exec("/usr/local/bin/casperjs /dir/test.js");//此处我使用的都是绝对路径

 

 

 

 

 

如果执行成功,$op输出返回值;如果执行失败,$op输出错误信息;$er输出命令执行状态,为ubuntu命令行执行状态, 例如134,127,如果此类报错,并且代码无误,我认为需从ubuntu执行命令行返回status入手解决问题。

casperjs输入错误信息:

在脚本中加入如下代码:

 

// http://docs.casperjs.org/en/latest/events-filters.html#remote-message
casper.on("remote.message", function(msg) {
    this.echo("Console: " + msg);
});

http://docs.casperjs.org/en/latest/events-filters.html#page-error
casper.on("page.error", function(msg, trace) {
    this.echo("Error: " + msg);
    // maybe make it a little fancier with the code from the PhantomJS equivalent
});

// http://docs.casperjs.org/en/latest/events-filters.html#resource-error
casper.on("resource.error", function(resourceError) {
    this.echo("ResourceError: " + JSON.stringify(resourceError, undefined, 4));
});

// http://docs.casperjs.org/en/latest/events-filters.html#page-initialized
casper.on("page.initialized", function(page) {
    // CasperJS doesn't provide `onResourceTimeout`, so it must be set through 
    // the PhantomJS means. This is only possible when the page is initialized
    page.onResourceTimeout = function(request) {
        console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
    };
});


调整完毕记得注释该段代码

 

欢迎交流cauyylAT163.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值