酷笔记 > QueryList采集器开发皇冠体育是干嘛的|免费注册 > 4-1插件开发指导

插件开发指导



QueryList插件开发指导


讲解前先列出最后的整体目录结构,假定www目录为当前的项目目录:

www
├──?querylist
│???├──?Ext
│???│???└──?Hello.PHP
│???├──?QueryList.php
│???└──?vendor
│???????
└──?testHello.php

演示的所有源码打包下载:完整下载

一.下载QueryList项目到本地

安装querylist

Composer?create-project?jaeger/querylist

然后到querylist目录去执行下面命令安装AQuery,AQuery为所有插件的基类,插件必须继承AQuery并实现run()方法.

composer?require?jaeger/querylist-ext-aquery

二.在querylist目录下新建Ext目录

querylist/Ext目录可用于存放QueryList扩展

三.在querylist/Ext目录下新建扩展文件Hello.php

getInstance();
????????//设置QueryList对象的HTML属性
????????$ql->html?=?$this->getHtml($args['url']);
????????//返回QueryList对象
????????return?$ql;
????}
????/**
?????*?自定义一个抓取网页源码的方法
?????*/
????public?function?getHtml($url)
????{
??????return?file_get_contents($url);
????}
}
?'Http://www.baidu.com'
????]);
$data?=?$ql->setQuery([
????'title'=>['title','text']
????])->data;
print_r($data);
输出结果;
Array
(
????[0]?=>?Array
????????(
????????????[title]?=>?百度一下,你就知道
????????)
)

下面附加一些现有的插件源码来加强理解

下面是Request扩展的源码:


?*?@version?????????1.0
?*?网络操作扩展
?*/
class?Request?extends?AQuery
{
????protected?function?hq(array?$args)
????{
????????$args?=?array(
????????????'http'?=>?isset($args['http'])?$args['http']:$args,
????????????'callback'?=>?isset($args['callback'])?$args['callback']:'',
????????????'args'?=>??isset($args['args'])?$args['args']:''
????????????);
????????$http?=?$this->getInstance('QL\Ext\Lib\Http');
????????$http->initialize($args['http']);
????????$http->execute();
????????if(!empty($args['callback'])){
????????????$http->result?=?call_user_func($args['callback'],$http->result,$args['args']);
????????}
????????return?$http;
????}
????public?function?run(array?$args)
????{
????????$http?=?$this->hq($args);
????????$ql?=?$this->getInstance();
????????$ql->html?=?$http->result;
????????return?$ql;
????}
}

扩展之间还可以继承,下面的Login扩展继承了Request扩展并重写了run()方法:


?*?@version?????????1.0
?*?模拟登陆扩展
?*/
class?Login?extends?Request
{
????private?$http;
????public?$html;
????public?function?run(array?$args)
????{
????????$this->http?=?$this->hq($args);
????????$this->html?=?$this->http->result;
????????return?$this;
????}
????public?function?get($url,$callback?=?null,$args?=?null)
????{
????????$result?=?$this->http->get($url);
????????return?$this->getQL($result,$callback,$args);
????}
????public?function?post($url,$data=array(),$callback?=?null,$args?=?null)
????{
????????$result?=?$this->http->post($url,$data);
????????return?$this->getQL($result,$callback,$args);
????}
????private?function?getQL($html,$callback?=?null,$args?=?null)
????{
????????if(is_callable($callback)){
????????????$result?=?call_user_func($callback,$result,$args);
????????}
????????$ql?=?$this->getInstance();
????????$ql->html?=?$html;
????????return?$ql;
????}
}