阿里雲直播服務 sdk demo php
阿新 • • 發佈:2018-11-08
最近做直播平臺試用了一下阿里的直播服務,阿里的sdk寫的有點繞,自己建了一個基類去實現簽名提交和建立推拉流地址
請求和加簽名的基類:
對上面的簡單呼叫和幾個常用方法例子:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/12/8 0008 * Time: 11:05 */ class Aliyun{ private $accessKeyId = ""; //金鑰ID private $accessKeySecret = ""; //金鑰 public $version = "2014-11-11"; //API版本號 public $format = "JSON"; //返回值型別 private $domainParameters = ""; public $video_host=''; //推流域名 public $appName="test"; //應用名 public $privateKey=""; //鑑權 public $vhost=""; //加速域名 public $msg; /** * 訪問阿ali介面進行請求並返回ali返回值 * @param array $apiParams 介面自定義引數 * @param string $credential 傳值方式預設get * @param string $domain 請求地址 */ public function aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com") { date_default_timezone_set("GMT"); $apiParams['Format'] = $this->format; $apiParams['SignatureMethod'] = "HMAC-SHA1";//簽名演算法 $apiParams['SignatureNonce'] = rand(100000,999999);//隨機數 $apiParams['SignatureVersion'] = '1.0';//簽名演算法版本 $apiParams['TimeStamp'] =date('Y-m-d\TH:i:s\Z');//請求時間 $apiParams['Version'] = $this->version; $apiParams["AccessKeyId"]=$this->accessKeyId; $accessSecret = $this->accessKeySecret; $apiParams["Signature"] = $this->computeSignature($credential,$apiParams,$accessSecret); if($credential == "POST") { $requestUrl = "https://". $domain . "/"; foreach ($apiParams as $apiParamKey => $apiParamValue) { $this->putDomainParameters($apiParamKey,$apiParamValue); } $url= $requestUrl; } else { $requestUrl = "http://". $domain . "/?"; foreach ($apiParams as $apiParamKey => $apiParamValue) { $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&"; } $url= substr($requestUrl, 0, -1); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); //處理http證書問題 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $ret = curl_exec($ch); if (false === $ret) { $ret = curl_errno($ch); $this->message = 'curl方法出錯,錯誤號:'.$ret; return false; } curl_close($ch); if( $this->format == "JSON") return json_decode($ret,true); elseif($this->format =="XML"){ return $this->xmlToArray($ret); }else return $ret; } /** * 計算簽名 * @param $credential * @param $parameters * @param $accessKeySecret * @return string */ private function computeSignature($credential,$parameters, $accessKeySecret) { ksort($parameters); $canonicalizedQueryString = ''; foreach($parameters as $key => $value) { $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value); } $stringToSign = $credential.'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); $signature = $this->signString($stringToSign, $accessKeySecret."&"); return $signature; } /** * url編碼 * @param $str * @return mixed|string */ protected function percentEncode($str) { $res = urlencode($str); $res = preg_replace('/\+/', '%20', $res); $res = preg_replace('/\*/', '%2A', $res); $res = preg_replace('/%7E/', '~', $res); return $res; } /** * get請求時無用沒看 * @param $name * @param $value */ public function putDomainParameters($name, $value) { $this->domainParameters[$name] = $value; } /** * 對待加密字串加密 * @param $source * @param $accessSecret * @return string */ public function signString($source, $accessSecret) { return base64_encode(hash_hmac('sha1', $source, $accessSecret, true)); } /** * xml轉成陣列 * @param $xml * @return mixed */ function xmlToArray($xml){ //禁止引用外部xml實體 libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring),true); return $val; } }
<?php /** * Created by PhpStorm. * User: ForeverTime * Date: 2016/12/10 * Time: 16:27 */ class Ali_Lite{ protected $config; protected $aliLive; public function __construct() { include_once 'Aliyun.php'; $this -> aliLive = new Aliyun(); } /** * 查詢線上人數 * @param $domainName 直播域名 * @param $appName 應用名 * @param $streamName 推流名 */ public function describeLiveStreamOnlineUserNum($domainName,$appName,$streamName){ $apiParams = array( 'Action'=>'DescribeLiveStreamOnlineUserNum', 'DomainName'=>$domainName, 'AppName'=>$appName, 'StreamName'=>$streamName, ); return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com"); } /** * 獲取某個域名或應用下的直播流操作記錄 * @param $domainName 域名 * @param $appName 應用名 * @param $streamName 推流名 */ public function describeLiveStreamsControlHistory($domainName,$appName,$startTime,$endTime){ $apiParams = array( 'Action'=>'DescribeLiveStreamsControlHistory', 'DomainName'=>$domainName, 'AppName'=>$appName, 'StartTime'=>$startTime, 'EndTime'=>$endTime, ); return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com"); } /** * 檢視指定域名下(或者指定域名下某個應用)的所有正在推的流的資訊 * @param $domainName 域名 * @param $appName 應用名 * @return bool|int|mixed */ public function describeLiveStreamsOnlineList($domainName,$appName){ $apiParams = array( 'Action'=>'DescribeLiveStreamsOnlineList', 'DomainName'=>$domainName, 'AppName'=>$appName, ); return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com"); } /** * 查詢推流黑名單列表 * @param $domainName 域名 * @return bool|int|mixed */ public function describeLiveStreamsBlockList($domainName){ $apiParams = array( 'Action'=>'DescribeLiveStreamsBlockList', 'DomainName'=>$domainName, ); return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com"); } /** * 生成推流地址 * @param $streamName 使用者專有名 * @param $vhost 加速域名 * @param $time 有效時間單位秒 */ public function getPushSteam($streamName,$vhost,$time=3600){ $time = time()+$time; $videohost = $this->aliLive->video_host; $appName=$this->aliLive->appName; $privateKey=$this->aliLive->privateKey; if($privateKey){ $auth_key =md5('/'.$appName.'/'.$streamName.'-'.$time.'-0-0-'.$privateKey); $url =$videohost.'/'.$appName.'/'.$streamName.'?vhost='.$vhost.'&auth_key='.$time.'-0-0-'.$auth_key; }else{ $url = $videohost.'/'.$appName.'/'.$streamName.'?vhost='.$vhost; } return $url; } /** * 生成拉流地址 * @param $streamName 使用者專有名 * @param $vhost 加速域名 * @param $type 視訊格式 支援rtmp、flv、m3u8三種格式 */ public function getPullSteam($streamName,$vhost,$time=3600,$type='rtmp'){ $time = time()+$time; $appName=$this->aliLive->appName; $privateKey=$this->aliLive->privateKey; $url=''; switch ($type){ case 'rtmp': $host = 'rtmp://'.$vhost; $url = '/'.$appName.'/'.$streamName; break; case 'flv': $host = 'http://'.$vhost; $url = '/'.$appName.'/'.$streamName.'.flv'; break; case 'm3u8': $host = 'http://'.$vhost; $url = '/'.$appName.'/'.$streamName.'.m3u8'; break; } if($privateKey){ $auth_key =md5($url.'-'.$time.'-0-0-'.$privateKey); $url = $host.$url.'?auth_key='.$time.'-0-0-'.$auth_key; }else{ $url = $host.$url; } return $url; } /** * 禁止推流介面 * @param $domainName 您的加速域名 * @param $appName 應用名稱 * @param $streamName 流名稱 * @param $liveStareamName 用於指定主播推流還是客戶端拉流, 目前支援”publisher” (主播推送) * @param $resumeTime 恢復流的時間 UTC時間 格式:2015-12-01T17:37:00Z * @return bool|int|mixed */ public function forbid($streamName,$resumeTime,$domainName='www.test.com',$appName='xnl',$liveStreamType='publisher'){ $apiParams = array( 'Action'=>'ForbidLiveStream', 'DomainName'=>$domainName, 'AppName'=>$appName, 'StreamName'=>$streamName, 'LiveStreamType'=>$liveStreamType, 'ResumeTime'=>$resumeTime ); return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com"); } /** * 恢復直播流推送 * @param $streamName 流名稱 * @param string $appName 應用名稱 * @param string $liveStreamType 用於指定主播推流還是客戶端拉流, 目前支援”publisher” (主播推送) * @param string $domainName 您的加速域名 */ public function resumeLive($streamName,$domainName='www.test.top',$appName='xnl',$liveStreamType='publisher'){ $apiParams = array( 'Action'=>'ResumeLiveStream', 'DomainName'=>$domainName, 'AppName'=>$appName, 'StreamName'=>$streamName, 'LiveStreamType'=>$liveStreamType, ); return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com"); } }