插件开发指导
安装querylist
composer create-project jaeger/querylist
querylist/Ext
目录可用于存放QueryList
扩展
<?php
/**
* QueryList的Hello扩展演示
*/
namespace QL\Ext;
class Hello extends AQuery
{
/**
* 必须要实现run()方法
*/
public function run(array $args)
{
//getInstance()方法用于获取任意类的实例,默认获取QueryList实例
$ql = $this->getInstance();
//设置QueryList对象的html属性
$ql->html = $this->getHtml($args['url']);
//返回QueryList对象
return $ql;
}
* 自定义一个抓取网页源码的方法
*/
public function getHtml($url)
{
return file_get_contents($url);
}
}
输出结果:
Array
(
[0] => Array
(
[title] => 百度一下,你就知道
)
)
扩展之间还可以继承,下面的Login
扩展继承了Request
扩展并重写了run()
方法:
<?php
namespace QL\Ext;
/**
* @Author: Jaeger <hj.q@qq.com>
* @version 1.0
* 模拟登陆扩展
*/
{
private $http;
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;
}