使用插件

使用插件实现无限可能。


QueryList使用use()方法来注册插件。

目前收录的一些QueryList插件: https://github.com/jae-jae/QueryList-Community

用法

有两种用法

1
注册单个插件,可携带安装参数。

  1. $ql = QueryList::getInstance();
  2. $ql->use(My\MyPlugin::class);
  3. //或者,带安装参数
  4. $ql->use(My\MyPlugin::class,$arg1,$arg2,$arg3);

2
同时注册多个插件,不能携带安装参数。

  1. $ql = QueryList::getInstance();
  2. $ql->use([
  3. My\MyPlugin::class,
  4. My\MyPlugin2::class,
  5. Other\OtherPlugin::class
  6. ]);

例子

PhantomJS插件用于采集Javascript动态渲染的网页内容,插件项目地址:https://github.com/jae-jae/QueryList-PhantomJS

安装插件

在QueryList项目中执行composer命令:

  1. composer require jaeger/querylist-phantomjs

使用插件

此插件注册时需要携带2个参数,一个参数是PhantomJS二进制文件路径,另一个可选参数是注册的函数名称。

  1. use QL\QueryList;
  2. use QL\Ext\PhantomJs;
  3. $ql = QueryList::getInstance();

注册插件,使用默认函数名称,此插件的默认函数名称为browser

  1. $ql->use(PhantomJs::class,'/usr/local/bin/phantomjs');
  2. // 使用插件
  3. $html = $ql->browser('https://m.toutiao.com')->getHtml();
  4. print_r($html);

注册插件,使用自定义函数名称chrome

  1. $ql->use(PhantomJs::class,'/usr/local/bin/phantomjs','chrome');
  2. // 使用插件
  3. $html = $ql->chrome('https://m.toutiao.com')->getHtml();
  4. print_r($html);