首先先在腾讯云中开通这三项
IM中创建项目
云直播
解析推拉流地址:
$domain = $this->getConfig('anchor_push'); //推流地址$domainpull = $this->getConfig('anchor_pull'); //拉流地址$streamName = 'kangfuyuan'.$res; //直播间ID(唯一的)$key = $this->getConfig('anchor_push_key'); //推流key (平台上自己获取的)$keypull = $this->getConfig('anchor_pull_key'); //拉流key (平台上自己获取的)$time = date('Y-m-d H:i:s',time()+86400); // 生成推流地址$push_url = $this->getPushUrl($domain,$streamName,$key,$time);// 生成拉流地址$pull_url = $this->getpullUrl($domainpull,$streamName,$keypull,$time);// 录制视频(云点播,手动点击录制)$Taskre = $this->CreateRecordTaskLuzhi($streamName);// dump($Taskre);exit;// 创建直播间群组 IM创建群组$data_ = ['Type' => 'ChatRoom','Name' => $data['title'],'MaxMemberCount' => '1000','ApplyJoinOption' => 'NeedPermission'];// 创建群组$create_group = $this->common('create_group',$data_);if($create_group['ErrorCode'] !== 0){$result['code'] = 0;$result['msg'] = $create_group['ErrorInfo'];return_json_encode($result);}// 注册腾讯云账号(每个账号都需要注册腾讯云账号 建议放在注册接口中执行)controller('TencentIm')->account_import($id);// 加入群聊$add = $this->add_group_id($create_group['GroupId'],$id);// dump($add);exit;Db::name('broadcast')->where('id',$res)->update(array('push_url'=>$push_url,'pull_url'=>$pull_url,'TaskId'=>$Taskre['TaskId'],'RequestId'=>$Taskre['RequestId'],'group_id'=>$create_group['GroupId']));// Db::name('broadcast')->where('id',$res)->update(array('push_url'=>$push_url,'pull_url'=>$pull_url,'group_id'=>$create_group['GroupId']));$data_info = Db::name('broadcast')->where('id',$res)->find();$data_info['cover_img'] = setFilePath($data_info['cover_img']);$this->ApiReturn(1,'开播成功',$data_info);
生成推拉流地址
/*** 获取推流地址* 如果不传key和过期时间,将返回不含防盗链的url* @param domain 您用来推流的域名* streamName 您用来区别不同推流地址的唯一流名称* key 安全密钥* time 过期时间 sample 2016-11-12 12:00:00* @return String url*/public function getPushUrl($domain, $streamName, $key = null, $time = null){if($key && $time){$txTime = strtoupper(base_convert(strtotime($time),10,16));$txSecret = md5($key.$streamName.$txTime);$ext_str = "?".http_build_query(array("txSecret"=> $txSecret,"txTime"=> $txTime));}return "rtmp://".$domain."/live/".$streamName . (isset($ext_str) ? $ext_str : "");}public function getPullUrl($domain, $streamName, $key = null, $time = null){if($key && $time){$txTime = strtoupper(base_convert(strtotime($time),10,16));$txSecret = md5($key.$streamName.$txTime);$ext_str = "?".http_build_query(array("txSecret"=> $txSecret,"txTime"=> $txTime));}return "http://".$domain."/live/".$streamName .".m3u8". (isset($ext_str) ? $ext_str : "");}
录制视频(云点播,手动点击录制,官方有两种录制,一种是开直播自动录制)
//录制视频
public function CreateRecordTaskLuzhi($streamName){try {$cred = new Credential("AKIDoXd******0C8zaAaPyyg8H8fwveeRXty", "1p565rYx5C3dw******6rKsBD5fGbR1e");$httpProfile = new HttpProfile();$httpProfile->setEndpoint("live.tencentcloudapi.com");$clientProfile = new ClientProfile();$clientProfile->setHttpProfile($httpProfile);$client = new LiveClient($cred, "ap-guangzhou", $clientProfile);$req = new CreateRecordTaskRequest();$params = array("StreamName" => $streamName,"DomainName" => "kfytui.zzmzrj.com","AppName" => "live","StartTime" => time(),"EndTime" => time()+20*60*60,);$req->fromJsonString(json_encode($params));$resp = $client->CreateRecordTask($req);return json_decode($resp->toJsonString(),true);}catch(TencentCloudSDKException $e) {echo $e;}}//停止录制public function StopRecordTaskTzhi($TaskId){try {$cred = new Credential("AKIDoXd******0C8zaAaPyyg8H8fwveeRXty", "1p565rYx5C3dw******6rKsBD5fGbR1e");$httpProfile = new HttpProfile();$httpProfile->setEndpoint("live.tencentcloudapi.com");$clientProfile = new ClientProfile();$clientProfile->setHttpProfile($httpProfile);$client = new LiveClient($cred, "ap-guangzhou", $clientProfile);$req = new StopRecordTaskRequest();$params = array("TaskId" => $TaskId,);$req->fromJsonString(json_encode($params));$resp = $client->StopRecordTask($req);return json_decode($resp->toJsonString(),true);
// print_r($resp->toJsonString());}catch(TencentCloudSDKException $e) {echo $e;}}
// 腾讯云创建群组(临时聊天群)public function common($Interface,$data){
// $Interface = 'create_group';$sdkappid = '1400504***';$identifier = 'administrator';$usersig = '************B3mW2h15-rmNBCjKIIA22R4caayS6iyThUFL17pi3Pdw7nzfbb1LhLxQJmG8CmQyYhG00XGrgQNTXUaVXom-oPOlEVbUuCBZYLgOBy3x0b*WxJyd4R0QaAUTXVP-Ms7vOZ4*D-hcr*PzfjqLRw8zie1G5xOFclX0XpGrJM*V6o0xjtZLK8Jq-cDMM5*3wBrOI0ZQ__';$random = '99999999';$url = 'https://console.tim.qq.com/v4/group_open_http_svc/'.$Interface.'?sdkappid='.$sdkappid.'&identifier='.$identifier.'&usersig='.$usersig.'&random='.$random.'&contenttype=json';$request = curl_post($url, $data);return $request;}
// 增加群成员public function add_group_id($group_id,$uid){$Interface = 'add_group_member';$sdkappid = '14005******';$identifier = 'administrator';$usersig = '******4R0QaAUTXVP-Ms7vOZ4*D-hcr*PzfjqLRw8zie1G5xOFclX0XpGrJM*V6o0xjtZLK8Jq-cDMM5*3wBrOI0ZQ__';$random = '99999999';$url = 'https://console.tim.qq.com/v4/group_open_http_svc/'.$Interface.'?sdkappid='.$sdkappid.'&identifier='.$identifier.'&usersig='.$usersig.'&random='.$random.'&contenttype=json';$data = ['GroupId' => $group_id,'MemberList' => [['Member_Account' => "{$uid}"],],];$request = curl_post($url, $data);return $request;}
// 腾讯云注册账号public function account_import($usesrid='administrator'){$Interface = 'account_import';$sdkappid = '14005******';$identifier = 'administrator';$usersig = '********V6o0xjtZLK8Jq-cDMM5*3wBrOI0ZQ__';$random = '99999999';$url = 'https://console.tim.qq.com/v4/im_open_login_svc/'.$Interface.'?sdkappid='.$sdkappid.'&identifier='.$identifier.'&usersig='.$usersig.'&random='.$random.'&contenttype=json';$nick = Db::name('users')->field('nickname,headimgurl')->where('id',$usesrid)->find();$data = ['Identifier' => "{$usesrid}",'Nick' => $nick['nickname'],'FaceUrl' => setFilePath($nick['headimgurl']),];$request = curl_post($url, $data);return $request;}
//补全文件路径
function setFilePath($file = null){if(!$file){return '';}$file = str_replace("\\","/",$file);/*************************************************/if(strstr($file,"http")) return $file;if(strstr($file,"upload")) return 'http://'.$_SERVER['HTTP_HOST'].$file;return "http://47.92.85.75/upload/".$file;/*************************************************/$pa = "/upload/".$file;if(file_exists(".".$pa)){$path="http://".$_SERVER['HTTP_HOST'].$pa;return $path;}else{return '';}
}