采集数据实例流程
1,加入队列消费queuePlayer($array)具体业务方法
2,业务方法说明:使用事务,处理采集数据,处理业务逻辑,
3,日志添加
补充,队列使用可以查看上一篇文章 my.oschina.net/owenzhang24…
private $user = ''; private $secret = ''; private $url = 'open.baiduapi.com/api/football/'; /* * @array 传入过来一个球队信息的数组 * @return bool 成功返回true ,失败返回false */ public function __construct() { $this->user = Env::get('nami.user'); $this->secret = Env::get('nami.secret'); } /* * 球员队列消费 */ public function queuePlayer($array) { if (empty($array)) { Log::error('球员数据不能为空:' . json_encode($array, JSON_UNESCAPED_UNICODE)); return false; } $http = new Http(); $playerRequest = $http->doGet($this->url . "?user=$this->user"); $playerData = json_decode($playerRequest, true); if (empty($playerData)) { Log::error("球员ID:{$array['id']}.信息异常,接口返回" . json_encode($playerRequest, JSON_UNESCAPED_UNICODE)); return false; } //走事务 Db::startTrans(); try { $playerInfo = $model->getPlayerInfo($array['id']); if (empty($playerInfo)) { //球员基本信息,添加 $data = [ 'nami_player_id' => $array['id'], 'nami_team_id' => $playerData['team_id'], ]; $model->InsertPlayerAll($data); } else { //更新数据--球员的基本信息 $data = [ 'nami_team_id' => $playerData['team_id'], 'name' => $playerData['name_zh'], ]; $model->UpdatePlayer($array['id'], $data); } //事务提交 Db::commit(); return true; } catch (Exception $e) { //错误日志提交 $error = json_encode([ 'msg' => $e->getMessage(), 'line' => $e->getLine(), ], JSON_UNESCAPED_UNICODE); Log::error("球员ID.{$array['id']}.采集错误!" . $error); //回滚事务 Db::rollback(); return false; } }