第六步:编写业务回调

1、编写 AppProducer 业务回调:
public function onProducerStart($producer)
{
    //比如可以在这里手动新增一条其他任务
    /*$task = array(
         'url' => array(
             'r1' => 'https://baike.baidu.com/item/%E5%8C%97%E4%BA%AC/128981?fr=aladdin',
         ),
         'rule' => array(
             'r1' => array(
                 '飞机场' => ['dl.basicInfo-right dd.basicInfo-item.value:eq(5)', 'text'],
                 '火车站' => ['dl.basicInfo-right dd.basicInfo-item.value:eq(6)', 'text'],
             ),
         ),
    );
    $producer->newTaskMan()->createMultiTask($task);*/
}

public function onProducerStop($producer)
{
}

public function onProducerReload($producer)
{
}
2、编写 AppDownloader 业务回调:
public function onDownloaderStart($downloader)
{
}

public function onDownloaderStop($downloader)
{
}

public function onDownloaderReload($downloader)
{
}

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

public function onBeforeDownload($downloader, $task)
{
    //比如可以在这里重新操作 $task 数组,然后务必return返回。
    //$task = [...];
    //return $task;

    //比如可以在这里改变发起http请求时的上下文参数
    //$downloader->httpClient->setConnectTimeout(3);
    //$downloader->httpClient->setTransferTimeout(10);
    //$downloader->httpClient->setProxy('http://180.153.144.138:8800');
}

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

public function onAfterDownload($downloader, $download_data, $task)
{
    //比如将下载后的源数据保存到某个文件:
    //file_put_contents("/path/to/downloadData.txt", $download_data);
}
3、编写 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)
{
    //比如打印提取到的业务数据
    //var_dump($fields, __METHOD__);

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

results matching ""

    No results matching ""