1、阿里雲直播官方文檔:https://help.aliyun.com/document_detail/87396.html?spm=a2c4g.11186623.6.609.19d83dd2Uejwx7
在線調試api工具:https://help.aliyun.com/document_detail/48207.html?spm=a2c4g.11186623.6.689.39f830f63Otms9
2、引入alibaba sdk for php SDK,git:https://github.com/aliyun/openapi-sdk-php
3、官方直播文檔,有句話很重要:https://help.aliyun.com/document_detail/87396.html?spm=a2c4g.11186623.6.609.19d83dd2Uejwx7
這句話什么意思?【就是說,你按步驟先配置域名、鑒權,直播地址隨便你怎么生成都是有效的,直接拿到能錄制直播和觀看直播的組件中進行使用就可以了。測試推薦:OBS推工具和LVS播工具】
4、自定義阿里雲直播 操作類(實現生成推流和播流地址、暫停直播、恢復直播、直播間人數顯示等功能),不廢話:
<?php namespace App\Service; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; class AliyunLive { private $error; const DOMAIN_NAME='alive.i***h.cn'; //你的加速域名,和推流域名一致。 const REGION_ID='cn-shanghai'; //區域 const ACCESS_KEY_ID='LTAI4*****ibc'; //阿里雲秘鑰 const ACCESS_KEY_SECRET='HPg2*****gnNOK'; //阿里雲秘鑰 const LIVE_HOST='live.aliyuncs.com'; //寫死,阿里雲直播CDN域名 const ALIVE_URL='alive.i***h.cn'; //推域名 const ALIVE_KEY='oR11sd***'; //推流鑒權KEY,后台拿 const TLIVE_URL='tlive.i***h.cn'; //關聯的播域名 const TLIVE_KEY='jdU1d***op'; //播流鑒權KEY,后台拿 /** * 創建簽名 * @param $path 播放地址 域名后面所有 * @param $exp //結束時間 * @param $key * @return string */ private static function createSign($path,$exp,$key) { $rand=0; $uid=0; $str=sprintf("%s-%s-%s-%s-%s",$path,(string)$exp,(string)$rand,(string)$uid,$key); $hashValue=md5($str); $authKey=sprintf("%s-%s-%s-%s",(string)$exp,(string)$rand,(string)$uid,$hashValue); return "auth_key=$authKey"; } /** * 創建是直播地址 * @param $appName 應用名稱 ,自定義 * @param $streamName 房間名稱,自定義,該應用下唯一 * @param $endTime 結束時間 * @return array|bool 播放流(觀看者):alive_url,推流(直播者):tlive_url */ public static function createdLive($appName,$streamName,$endTime) { if(!$appName || !$streamName || !$endTime || $endTime < time()){ return false; } //創建播流 $path="/$appName/$streamName"; $aliveUrl='rtmp://'.self::ALIVE_URL."$path?".self::createSign($path,$endTime,self::ALIVE_KEY); //創建推流 $tliveUrlRTMP= 'rtmp://'.self::TLIVE_URL."$path?".self::createSign($path,$endTime,self::TLIVE_KEY); // $tliveUrlFLV ='http://'.self::TLIVE_URL."$path.flv?".self::createSign($path,$endTime,self::TLIVE_KEY); // $tliveUrlM3U8='http://'.self::TLIVE_URL."$path.m3u8?".self::createSign($path,$endTime,self::TLIVE_KEY); return [ 'alive_url'=>$aliveUrl, 'tlive_url'=>$tliveUrlRTMP, ]; } //停止直播 public static function stopLive($appName,$streamName) { $query=[ 'RegionId' => self::REGION_ID, 'AppName' => $appName, 'StreamName' => $streamName, 'LiveStreamType' => "publisher", 'DomainName' => self::DOMAIN_NAME, // 'ResumeTime'=>'', ]; $_this=new static(); $result=$_this->request('ForbidLiveStream',$query); return true; } //恢復直播 public static function resumeLive($appName,$streamName) { $query=[ 'RegionId' => self::REGION_ID, 'LiveStreamType' => "publisher", 'AppName' => $appName, 'StreamName' => $streamName, 'DomainName' => self::DOMAIN_NAME, ]; $_this=new static(); $result=$_this->request('ResumeLiveStream',$query); return true; } //獲取直播在線人數 public static function getOnlineUserNum($appName,$streamNma) { $query=[ 'RegionId' => self::REGION_ID, 'DomainName' => self::DOMAIN_NAME, 'AppName'=>$appName, 'StreamName' =>$streamNma, ]; $_this=new static(); $result=$_this->request('DescribeLiveStreamOnlineUserNum',$query); return $result; } //日志 public function log($msg,$info='info') { $msg='直播接口:'.$msg.($info !='info'?var_export($info,1):''); log_write($msg); } //獲取錯誤 public static function getError() { return (new static())->error; } //請求 private function request($action,Array $query) { AlibabaCloud::accessKeyClient(self::ACCESS_KEY_ID, self::ACCESS_KEY_SECRET) ->regionId(self::REGION_ID) ->asDefaultClient(); try { $result = AlibabaCloud::rpc() ->product('live') // ->scheme('https') // https | http ->version('2016-11-01') ->action($action) ->method('POST') ->host(self::LIVE_HOST) ->options([ 'query' => $query, ]) ->request(); return $result->toArray(); } catch (ClientException $e) { $this->error=$e->getMessage(); return false; } catch (ServerException $e) { $this->error=$e->getMessage(); return false; } } }