阿里云直播服务 sdk demo php

[php]
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Administrator 
  5.  * Date: 2016/12/8 0008 
  6.  * Time: 11:05 
  7.  */  
  8.   
  9. class Aliyun{  
  10.     private $accessKeyId = "";          //密钥ID  
  11.     private $accessKeySecret = "";      //密钥  
  12.     public  $version = "2014-11-11";    //API版本号  
  13.     public  $format = "JSON";           //返回值类型  
  14.     private $domainParameters = "";  
  15.     public  $video_host='';                //推流域名  
  16.     public  $appName="test";            //应用名  
  17.     public  $privateKey="";             //鉴权  
  18.     public  $vhost="";                  //加速域名  
  19.     public  $msg;  
  20.   
  21.     /** 
  22.      * 访问阿ali接口进行请求并返回ali返回值 
  23.      * @param array $apiParams 接口自定义参数 
  24.      * @param string $credential 传值方式默认get 
  25.      * @param string $domain 请求地址 
  26.      */  
  27.     public function aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com")  
  28.     {  
  29.         date_default_timezone_set("GMT");  
  30.         $apiParams['Format'] = $this->format;  
  31.         $apiParams['SignatureMethod'] = "HMAC-SHA1";//签名算法  
  32.         $apiParams['SignatureNonce'] = rand(100000,999999);//随机数  
  33.         $apiParams['SignatureVersion'] = '1.0';//签名算法版本  
  34.         $apiParams['TimeStamp'] =date('Y-m-d\TH:i:s\Z');//请求时间  
  35.         $apiParams['Version'] = $this->version;  
  36.         $apiParams["AccessKeyId"]=$this->accessKeyId;  
  37.         $accessSecret = $this->accessKeySecret;  
  38.         $apiParams["Signature"] = $this->computeSignature($credential,$apiParams,$accessSecret);  
  39.         if($credential == "POST") {  
  40.             $requestUrl = "https://". $domain . "/";  
  41.             foreach ($apiParams as $apiParamKey => $apiParamValue)  
  42.             {  
  43.                 $this->putDomainParameters($apiParamKey,$apiParamValue);  
  44.             }  
  45.             $url= $requestUrl;  
  46.         }  
  47.         else {  
  48.             $requestUrl = "http://". $domain . "/?";  
  49.   
  50.             foreach ($apiParams as $apiParamKey => $apiParamValue)  
  51.             {  
  52.                 $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&";  
  53.             }  
  54.             $url= substr($requestUrl, 0, -1);  
  55.         }  
  56.         $ch = curl_init();  
  57.         curl_setopt($ch, CURLOPT_URL, $url);  
  58.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题  
  59.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  60.         $ret = curl_exec($ch);  
  61.         if (false === $ret) {  
  62.             $ret =  curl_errno($ch);  
  63.             $this->message = 'curl方法出错,错误号:'.$ret;  
  64.             return false;  
  65.         }  
  66.         curl_close($ch);  
  67.         if( $this->format == "JSON")  
  68.             return json_decode($ret,true);  
  69.         elseif($this->format =="XML"){  
  70.             return $this->xmlToArray($ret);  
  71.         }else  
  72.             return $ret;  
  73.     }  
  74.   
  75.     /** 
  76.      * 计算签名 
  77.      * @param $credential 
  78.      * @param $parameters 
  79.      * @param $accessKeySecret 
  80.      * @return string 
  81.      */  
  82.     private function computeSignature($credential,$parameters, $accessKeySecret)  
  83.     {  
  84.         ksort($parameters);  
  85.         $canonicalizedQueryString = '';  
  86.         foreach($parameters as $key => $value)  
  87.         {  
  88.             $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);  
  89.         }  
  90.         $stringToSign = $credential.'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));  
  91.         $signature = $this->signString($stringToSign, $accessKeySecret."&");  
  92.   
  93.         return $signature;  
  94.     }  
  95.   
  96.     /** 
  97.      * url编码 
  98.      * @param $str 
  99.      * @return mixed|string 
  100.      */  
  101.     protected function percentEncode($str)  
  102.     {  
  103.         $res = urlencode($str);  
  104.         $res = preg_replace('/\+/', '%20', $res);  
  105.         $res = preg_replace('/\*/', '%2A', $res);  
  106.         $res = preg_replace('/%7E/', '~', $res);  
  107.         return $res;  
  108.     }  
  109.   
  110.     /** 
  111.      * get请求时无用没看 
  112.      * @param $name 
  113.      * @param $value 
  114.      */  
  115.     public function putDomainParameters($name, $value)  
  116.     {  
  117.         $this->domainParameters[$name] = $value;  
  118.     }  
  119.   
  120.     /** 
  121.      * 对待加密字符串加密 
  122.      * @param $source 
  123.      * @param $accessSecret 
  124.      * @return string 
  125.      */  
  126.     public function signString($source, $accessSecret)  
  127.     {  
  128.         return  base64_encode(hash_hmac('sha1', $source, $accessSecret, true));  
  129.     }  
  130.   
  131.     /** 
  132.      * xml转成数组 
  133.      * @param $xml 
  134.      * @return mixed 
  135.      */  
  136.     function xmlToArray($xml){  
  137.   
  138.         //禁止引用外部xml实体  
  139.   
  140.         libxml_disable_entity_loader(true);  
  141.   
  142.         $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);  
  143.   
  144.         $val = json_decode(json_encode($xmlstring),true);  
  145.   
  146.         return $val;  
  147.   
  148.     }  
  149. }  

对上面的简单调用和几个常用方法例子:

[php]
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: ForeverTime 
  5.  * Date: 2016/12/10 
  6.  * Time: 16:27 
  7.  */  
  8. class Ali_Lite{  
  9.     protected  $config;  
  10.     protected  $aliLive;  
  11.   
  12.     public function __construct()  
  13.     {  
  14.         include_once 'Aliyun.php';  
  15.         $this -> aliLive = new Aliyun();  
  16.     }  
  17.   
  18.     /** 
  19.      * 查询在线人数 
  20.      * @param $domainName  直播域名 
  21.      * @param $appName     应用名 
  22.      * @param $streamName  推流名 
  23.      */  
  24.     public function describeLiveStreamOnlineUserNum($domainName,$appName,$streamName){  
  25.         $apiParams = array(  
  26.             'Action'=>'DescribeLiveStreamOnlineUserNum',  
  27.             'DomainName'=>$domainName,  
  28.             'AppName'=>$appName,  
  29.             'StreamName'=>$streamName,  
  30.         );  
  31.         return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com");  
  32.     }  
  33.   
  34.   
  35.     /** 
  36.      * 获取某个域名或应用下的直播流操作记录 
  37.      * @param $domainName      域名 
  38.      * @param $appName         应用名 
  39.      * @param $streamName      推流名 
  40.      */  
  41.     public function describeLiveStreamsControlHistory($domainName,$appName,$startTime,$endTime){  
  42.         $apiParams = array(  
  43.             'Action'=>'DescribeLiveStreamsControlHistory',  
  44.             'DomainName'=>$domainName,  
  45.             'AppName'=>$appName,  
  46.             'StartTime'=>$startTime,  
  47.             'EndTime'=>$endTime,  
  48.         );  
  49.         return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com");  
  50.     }  
  51.   
  52.     /** 
  53.      * 查看指定域名下(或者指定域名下某个应用)的所有正在推的流的信息 
  54.      * @param $domainName       域名 
  55.      * @param $appName          应用名 
  56.      * @return bool|int|mixed 
  57.      */  
  58.     public function describeLiveStreamsOnlineList($domainName,$appName){  
  59.         $apiParams = array(  
  60.             'Action'=>'DescribeLiveStreamsOnlineList',  
  61.             'DomainName'=>$domainName,  
  62.             'AppName'=>$appName,  
  63.         );  
  64.         return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com");  
  65.     }  
  66.   
  67.     /** 
  68.      * 查询推流黑名单列表 
  69.      * @param $domainName       域名 
  70.      * @return bool|int|mixed 
  71.      */  
  72.     public function describeLiveStreamsBlockList($domainName){  
  73.         $apiParams = array(  
  74.             'Action'=>'DescribeLiveStreamsBlockList',  
  75.             'DomainName'=>$domainName,  
  76.         );  
  77.         return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com");  
  78.     }  
  79.   
  80.     /** 
  81.      * 生成推流地址 
  82.      * @param $streamName 用户专有名 
  83.      * @param $vhost 加速域名 
  84.      * @param $time 有效时间单位秒 
  85.      */  
  86.     public function getPushSteam($streamName,$vhost,$time=3600){  
  87.         $time = time()+$time;  
  88.         $videohost = $this->aliLive->video_host;  
  89.         $appName=$this->aliLive->appName;  
  90.         $privateKey=$this->aliLive->privateKey;  
  91.         if($privateKey){  
  92.             $auth_key =md5('/'.$appName.'/'.$streamName.'-'.$time.'-0-0-'.$privateKey);  
  93.             $url =$videohost.'/'.$appName.'/'.$streamName.'?vhost='.$vhost.'&auth_key='.$time.'-0-0-'.$auth_key;  
  94.         }else{  
  95.             $url = $videohost.'/'.$appName.'/'.$streamName.'?vhost='.$vhost;  
  96.         }  
  97.         return $url;  
  98.     }  
  99.   
  100.     /** 
  101.      * 生成拉流地址 
  102.      * @param $streamName 用户专有名 
  103.      * @param $vhost 加速域名 
  104.      * @param $type 视频格式 支持rtmp、flv、m3u8三种格式 
  105.      */  
  106.     public function getPullSteam($streamName,$vhost,$time=3600,$type='rtmp'){  
  107.         $time = time()+$time;  
  108.         $appName=$this->aliLive->appName;  
  109.         $privateKey=$this->aliLive->privateKey;  
  110.         $url='';  
  111.         switch ($type){  
  112.             case 'rtmp':  
  113.                 $host = 'rtmp://'.$vhost;  
  114.                 $url = '/'.$appName.'/'.$streamName;  
  115.                 break;  
  116.             case 'flv':  
  117.                 $host = 'http://'.$vhost;  
  118.                 $url = '/'.$appName.'/'.$streamName.'.flv';  
  119.                 break;  
  120.             case 'm3u8':  
  121.                 $host = 'http://'.$vhost;  
  122.                 $url = '/'.$appName.'/'.$streamName.'.m3u8';  
  123.                 break;  
  124.         }  
  125.         if($privateKey){  
  126.             $auth_key =md5($url.'-'.$time.'-0-0-'.$privateKey);  
  127.             $url = $host.$url.'?auth_key='.$time.'-0-0-'.$auth_key;  
  128.         }else{  
  129.             $url = $host.$url;  
  130.         }  
  131.         return $url;  
  132.     }  
  133.   
  134.     /** 
  135.      * 禁止推流接口 
  136.      * @param $domainName        您的加速域名 
  137.      * @param $appName          应用名称 
  138.      * @param $streamName       流名称 
  139.      * @param $liveStareamName  用于指定主播推流还是客户端拉流, 目前支持”publisher” (主播推送) 
  140.      * @param $resumeTime       恢复流的时间 UTC时间 格式:2015-12-01T17:37:00Z 
  141.      * @return bool|int|mixed 
  142.      */  
  143.     public function forbid($streamName,$resumeTime,$domainName='www.test.com',$appName='xnl',$liveStreamType='publisher'){  
  144.         $apiParams = array(  
  145.             'Action'=>'ForbidLiveStream',  
  146.             'DomainName'=>$domainName,  
  147.             'AppName'=>$appName,  
  148.             'StreamName'=>$streamName,  
  149.             'LiveStreamType'=>$liveStreamType,  
  150.             'ResumeTime'=>$resumeTime  
  151.         );  
  152.         return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com");  
  153.     }  
  154.   
  155.     /** 
  156.      * 恢复直播流推送 
  157.      * @param $streamName              流名称 
  158.      * @param string $appName          应用名称 
  159.      * @param string $liveStreamType   用于指定主播推流还是客户端拉流, 目前支持”publisher” (主播推送) 
  160.      * @param string $domainName       您的加速域名 
  161.      */  
  162.     public function resumeLive($streamName,$domainName='www.test.top',$appName='xnl',$liveStreamType='publisher'){  
  163.         $apiParams = array(  
  164.             'Action'=>'ResumeLiveStream',  
  165.             'DomainName'=>$domainName,  
  166.             'AppName'=>$appName,  
  167.             'StreamName'=>$streamName,  
  168.             'LiveStreamType'=>$liveStreamType,  
  169.         );  
  170.         return $this -> aliLive -> aliApi($apiParams,$credential="GET", $domain="cdn.aliyuncs.com");  
  171.     }  
  172.   
  173. }  
posted on 2018-03-29 10:04  caigan  阅读(4893)  评论(0编辑  收藏  举报