天天看點

php對接騰訊雲直播,聊天,im,雲錄制産生回放

首先先在騰訊雲中開通這三項

php對接騰訊雲直播,聊天,im,雲錄制産生回放

IM中建立項目

php對接騰訊雲直播,聊天,im,雲錄制産生回放

雲直播

解析推拉流位址:

php對接騰訊雲直播,聊天,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 '';
    }
}