阿里云直播服务 sdk demo php
[php]
- <?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]
- <?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");
- }
- }