这是我在sf上的第一篇文章。写基于swoole、Mixphp和CodeIgniter开发的失踪儿童信息平台。php
在2017年的时候,关注到有一个“团圆系统”,它是公安部专门为了快速扩散失踪儿童消息的平台,可是网上并无找到这个平台的地址。固然,偶然在淘宝平台上找到一个接口,这个接口能够取得失踪儿童的数据,后来这个接口提示不能访问了。那么若是我想去作这样的一个失踪儿童信息平台的话,就要从另外的地方获取数据了。mysql
团圆系统的全称应该是公安部儿童失踪信息紧急发布平台,在新浪微博上有一个官方的微博帐号,经过这个微博帐号发布儿童失踪信息。所以在实现“失踪儿童信息平台”的第一步就是从微博上去获取数据,而且这个获取数据的程序最后能在后台程序自动运行,这样就不用人工干预了。sql
我对php语言要熟悉一些,所以在打算本身动手实现这个想法的时候,就选择了php,另外以前也关注到swoole,swoole拓展了PHPer的能力范围,再也不局限于Web开发。进而找了一个比较容易上手的基于swoole的框架MixPHP,所以,在从微博上抓取数据的程序是基于Mixphp写的,工程结构以下:数据库
在apps/daemon/commands目录下新建一个控制器IndexController.php,控制器部分代码以下:api
<?php /** * Created by PhpStorm. * User: luoam * Date: 2018/4/20 * Time: 22:28 */ namespace apps\daemon\commands; use mix\client\Http; use mix\console\Controller; use mix\swoole\Process; use apps\daemon\commands\SaeTClientV2; use apps\daemon\commands\SaeTOAuthV2; use apps\daemon\commands\Sae; class IndexController extends Controller { // 启动 public function actionStart() { // 重复启动处理 if ($pid = Process::getMasterPid(self::PID_FILE)) { return "mix-daemon '{$this->processName}' is running, PID : {$pid}." . PHP_EOL; } // 启动提示 echo "mix-daemon '{$this->processName}' start successed." . PHP_EOL; // 蜕变为守护进程 if ($this->d) { Process::daemon(); } // 写入 PID 文件 Process::writePid(self::PID_FILE); // 修改进程名称 Process::setName("mix-daemon: {$this->processName}"); // 开始工做 $this->startWork(); } public function startWork() { try { while (true) { //实时采集一次 $sql = "select `itemid` from items where status=:status"; $rows = \mix::app()->rdb->createCommand($sql)->bindParams([ 'status' => '失踪' ])->queryAll(); if (!empty($rows)) { foreach ($rows as $row) { //采集一次失踪的, $this->actionDetail($row['itemid']); sleep(5); //再增量采集一次 $this->actionIncrementOnce(); } } } } catch (\Exception $e) { \Mix::app()->error->exception($e); sleep(10); // 休息一会,避免 cpu 出现 100% $this->startWork(); } } }
哦,须要提醒的是,Mixphp须要一些准备条件,在官方文档中写的很是详细。安装好Mixphp以后就能够直接运行上面的后台程序,命令如:swoole
./mix-daemon index/start -d
-d 表示这个进程将在后台运行。
数据的抓取分为几个部分,app
前台展现,至关于作一个网站,使用的是CodeIgniter框架,这个框架用来作网站,很方便,所以我也一直在用。
整个网站只显示那些仍然处于失踪状态的儿童的信息。页面的效果大体是这样的:框架
我在微博开发平台注册了一个应用,能够调用微博的api,所以在数据抓取的程序里,在数据存到数据库中的同时会发一条微博。调用的是share接口。使用微博的api的过程是这样的,先使用oauth获取到access_token,另外从微博开发平台找到key和secret,部分代码以下:codeigniter
$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $access_token); $sus = \mix::app()->rdb->insert('items', $dataArray)->execute(); $insertId = \Mix::app()->rdb->getLastInsertId(); $affectedRowss = \Mix::app()->rdb->getRowCount(); if ($affectedRowss > 0) { $texts = "" . $dataArray['name'] . "," . $dataArray['agesex'] . "," . mb_substr($dataArray['detail'], 0, 110, 'utf-8') . '...' . "若有线索,请迅速与警方联系:" . $dataArray['policetel'] . "http://www.anman.org/index/baobei/" . $containerid; if ($weibopic) { $weibopic = $this->get_img($weibopic, \mix::app()->getRuntimePath()); } $weiboRep = $c->share($texts, $weibopic); if (isset($weiboRep['id'])){ $sinaweibos = array('itemid'=>$containerid,'weiboid'=>$weiboRep['id']); \mix::app()->rdb->insert('sinaweibos',$sinaweibos)->execute(); } if ($weibopic){ unlink($weibopic); } echo date("Y-m-d H:i:s") . ' ' . $containerid . '微博发送成功!' . PHP_EOL; echo date("Y-m-d H:i:s") . ' ' . $containerid . '插入数据操做成功' . PHP_EOL; \mix::app()->log->info(date("Y-m-d H:i:s") . ' ' . $containerid . '插入数据操做成功'); } else { \mix::app()->log->error(date("Y-m-d H:i:s") . ' ' . $containerid . '插入操做出错'); }
微博的效果以下:网站
整个实现过程当中使用到了swoole,codeigniter框架,微博开发平台,mysql。后台程序从微博中抓取数据到本地,而后根据规则经过微博api在发送微博。有一个专门的网站去展现这些信息。后面我打算试着去对这些数据作一些分析。