第六步:编写业务回调


01、编写 AppProducer 业务回调

<?php
class AppProducer
{
	public function onProducerStart($producer)
	{
		//设定业务规则
		$rule = array(        
            'time' => ['div#7d ul.t.clearfix h1',      'text', [], 'function($field_name, $data){
                return "具体日子: " . $data;
            }'],                
            'wea'  => ['div#7d ul.t.clearfix p.wea',   'text'],
            'tem'  => ['div#7d ul.t.clearfix p.tem',   'text'],
		);  

		//任务私有context,其上下文成员与全局context完全相同,最终会采用合并覆盖策略
		$private_task_context = [
			'cache_enabled'   => false,
			'cache_directory' => sys_get_temp_dir() . '/DownloadCache4PHPCreeper/',
			'allow_url_repeat' => true,
			'track_request_args' => true,
			'track_task_package' => true,
		];

		//创建一条任务
		$task = array(
            'url' => "http://www.weather.com.cn/weather/101010100.shtml",
            'rule' => $rule,  
            'rule_name' =>  '',     
            'refer'     =>  '', 
            'type'      =>  'text', 
            'method'    =>  'get',
            'context'   =>  $private_task_context,
		);  
		$producer->createTask($task);
	}

    public function onProducerStop($producer)
    {
    }

    public function onProducerReload($producer)
    {
    }
}

02、编写 AppDownloader 业务回调

<?php
class AppDownloader
{
    public function onDownloaderStart($downloader)
    {
    }

    public function onDownloaderStop($downloader)
    {
	}

	public function onDownloaderReload($downloader)
	{
	}

	public function onDownloaderMessage($downloader, $parser_reply)
	{
	}

	public function onDownloaderConnectToParser($connection)
	{
	}

	public function onDownloadBefore($downloader, $task)
	{
		//比如可以在这里改变发起http请求时的上下文参数
		//$downloader->httpClient->setConnectTimeout(3);
		//$downloader->httpClient->setTransferTimeout(10);
		//$downloader->httpClient->setProxy($proxy);
	}

	public function onDownloadStart($downloader, $task)
	{
	}

	public function onDownloadAfter($downloader, $download_data, $task)
	{
		//比如将下载后的源数据保存到某个文件:
		//file_put_contents("/path/to/downloadData.txt", $download_data);
	}

	public function onDownloadFail($downloader, $error, $task)
	{   
	} 

	public function onDownloadTaskEmpty($downloader)
	{   
	} 
} 

03、编写 AppParser 业务回调

<?php
class AppParser
{
	public function onParserStart($parser)
	{
	}

	public function onParserStop($parser)
	{
	}

	public function onParerReload($parser)
	{
	}

	public function onParerMessage($parser, $connection, $download_data)
	{
	}

	public function onParserFindUrl($parser, $url)
	{
		//比如当发现子URL时,可以在这里校验子url是否有效
		//if(!Tool::checkUrl($url)) return false;
	}

	public function onParserExtractField($parser, $download_data, $fields)
	{
		//比如打印提取到的业务数据
		//pprint($fields, __METHOD__);

		//比如将数据存到mysql、redis数据库等等
		//DB::save($fields);
	}
}
Free Web Hosting