<?php
/**
*notes:获取直播间列表 100000次/一天(与获取回放接口共用次数)
*/
public function getLiveInfo(){
$rid = Db::name('live_room')->max('roomid');
$total = Db::name('live_room')->order('id desc')->value('total');
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".getConfig('wx_appid')."&secret=".getConfig('wx_appsecret');
$token_str = _curl($token_url);
$token_res = json_decode($token_str, true);
$live_token = $token_res['access_token'];
$live_url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=".$live_token;
if($rid == 0){
$data = json_encode(["start"=>0,"limit"=>5]);
}else{
$data = json_encode(["start"=>0,"limit"=>$rid-$total]);
}
$live_str = _curl($live_url,$data);
$LiveInfo = json_decode($live_str,true);
if($LiveInfo['errcode'] == 0 && $LiveInfo['errmsg'] == 'ok'){
Db::startTrans();
try {
foreach ($LiveInfo['room_info'] as $room=>$info){
if($info['roomid'] > $rid){
$live_data = [
'roomid'=>$info['roomid'],
'name'=>$info['name'],
'cover_img'=>$info['cover_img'],
'share_img'=>$info['share_img'],
'live_status'=>$info['live_status'],
'start_time'=>$info['start_time'],
'end_time'=>$info['end_time'],
'anchor_name'=>$info['anchor_name'],
'live_type'=>$info['live_type'],
'close_like'=>$info['close_like'],
'close_goods'=>$info['close_goods'],
'close_comment'=>$info['close_comment'],
'close_kf'=>$info['close_kf'],
'close_replay'=>$info['close_replay'],
'total' => $LiveInfo['total'],
'createtime'=>time(),
'updatetime'=>time()
];
$live_id = Db::name('live_room')->insert($live_data);
if(!$live_id){
Db::rollback();
break;
}
}
}
Db::commit();
}catch (Exception $e) {
// 回滚事务
Db::rollback();
}
}
}
/**
*notes:获取回放地址
*/
public function get_replay(){
$roomid = Db::name('live_room')->field('id,roomid,updatetime')->where(['live_status'=>103,'finish'=>0])->select();
if($roomid){
foreach ($roomid as $re=>$play){
$replay = time()-$play['updatetime'];
if($replay > 600){//结束直播10分钟后获取回放地址
$replay_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".getConfig('wx_appid')."&secret=".getConfig('wx_appsecret');
$replay_str = _curl($replay_url);
$replay_res = json_decode($replay_str, true);
$replay_token = $replay_res['access_token'];
$action_url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=".$replay_token;
$action_data = json_encode(["action"=>"get_replay","room_id"=>$play['roomid'],"start"=>0,"limit"=>20]);
$action_str = _curl($action_url,$action_data);
$actionInfo = json_decode($action_str,true);
if($actionInfo['errcode'] == 0 && $actionInfo['errmsg'] == 'ok'){
Db::startTrans();
try {
foreach ($actionInfo['live_replay'] as $act=>$tion){
$media_data = [
'live_room_id'=>$play['id'],
'roomid'=>$play['roomid'],
'media_url'=>$tion['media_url'],
'expire_time'=>$tion['expire_time'],
'create_time'=>$tion['create_time'],
'createtime'=>time()
];
$media_id = Db::name('live_media')->insert($media_data);
if(!$media_id){
Db::rollback();
break;
}
}
$finish = Db::name('live_room')->where('id',$play['id'])->update(['finish'=>1]);
if($finish === false){
Db::rollback();
continue;
}
Db::commit();
}catch (Exception $e) {
// 回滚事务
Db::rollback();
}
}
}
}
}
}