php乐视云直播接口类
<?php // +---------------------------------------------------------------------- // | Wemy Server - 博域科技威迈系统乐视云模块 // +---------------------------------------------------------------------- // | Copyright (c) 2016 http://www.wemy.com.cn, All rights reserved. // +---------------------------------------------------------------------- // | Author: Andy <www.wangyang.me> // +---------------------------------------------------------------------- class Lecloud { const ON_DEMAND_APIURL = 'http://api.letvcloud.com/open.php'; //点播接口 const ON_DEMAND_VER = '2.0'; //点播协议版本 const LIVE_APIURL = 'http://api.open.letvcloud.com/live/execute'; //直播接口 const LIVE_VER = '3.1'; //直播协议版本 public function __construct($options) { $this->userid = isset($options['userid'])?$options['userid']:''; //用户ID $this->uuid = isset($options['uuid'])?$options['uuid']:''; //UUID $this->pkey = isset($options['pkey'])?$options['pkey']:''; //私钥 } /** * 拼接数组成为字符串 * @param array $param 参数数组 * @param boolean $urlencode URL处理 * @return string 参数字符串 */ protected function _formatBizQueryParaMap($param, $urlencode = false){ $buff = ""; ksort($param); foreach ($param as $k => $v){ if($urlencode){ $v = urlencode($v); } $buff .= $k . $v; } return $buff; } /** * 整理GET方式URL参数 * @param array $param 参数数组 * @param boolean $urlencode URL处理 * @return string 参数字符串 */ protected function _formatGetPara($param){ $buff = ""; ksort($param); foreach ($param as $k => $v){ $buff .= $k . "=" . $v . "&"; } //去掉末位& $buff = "?" . substr($buff,0,strlen($buff)-1); return $buff; } /** * 获取签名 * @param array $param 参数 * @return string 签名 */ protected function _getSign($param){ //一、把除sign外的所有参数(包括另外的公共参数)按key升序排序 ksort($param); //二、把key和它对应的value拼接成一个字符串。按步骤1中顺序,把所有键值对字符串拼接成一个字符串。对于uri类的参数,请使用url编码前的字符串。 $String = $this->_formatBizQueryParaMap($param); //三、把分配给的pkey拼接在第2步骤得到的字符串后面 $String = $String . $this->pkey; //四、计算第3步骤字符串的md5值,使用md5值的16进制字符串作为sign的值。 $String = md5($String); return $String; } /** * 发送POST请求 * @param string $url 请求地址 * @param array $data 请求参数 * @return 请求结果 */ private function _curlPost($url,$data){ $ch = curl_init(); $header = "Accept-Charset: utf-8"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); curl_close($ch); return $tmpInfo; } /** * 发送GET请求 * @param string $url 请求地址 * @return 请求结果 */ private function _curlGet($url){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ return false; } } /** * 发送点播API请求 * @param array $param 参数 * @param string $method 请求方法 * @return 返回结果 */ protected function _sendOnDemandApi($param, $method = "get"){ $param['ver'] = self::ON_DEMAND_VER; $param['user_unique'] = $this->uuid; $param['timestamp'] = time() * 1000; //当前Unix时间戳 $param['format'] = 'json'; //返回参数格式:支持json和xml两种方式 $param['sign'] = $this->_getSign($param); if($method === 'get'){ $url = $this->_formatGetPara($param); $json = $this->_curlGet(self::ON_DEMAND_APIURL . $url); } else { $json = $this->_curlPost(self::ON_DEMAND_APIURL, $param); } $res = json_decode($json,true); if($res['code'] == 0){ return isset($res['data']) ? $res['data'] : true; } return false; } /** * 发送直播API请求 * @param array $param 参数 * @param string $method 请求方法 * @return 返回结果 */ protected function _sendLiveApi($param, $method = "get"){ $param['ver'] = self::LIVE_VER; $param['userid'] = $this->userid; $param['timestamp'] = time()*1000; //当前Unix时间戳 $param['sign'] = $this->_getSign($param); if($method === 'get'){ $url = $this->_formatGetPara($param); $json = $this->_curlGet(self::LIVE_APIURL . $url); } else { $json = $this->_curlPost(self::LIVE_APIURL, $param); } return json_decode($json,true); } /* * 点播api * $api_name string 点播接口名 * $param array 点播参数 */ public function dianbo($api_name,$param=''){ $param['api'] = $api_name; return $this->_sendOnDemandApi($param);//请求接口 } /** * 直播活动信息查询 * @param string $activityId 直播活动ID * @param string $activityName 直播活动名称 * @param string $activityStatus 直播活动状态。0:未开始 1:已开始 3:已结束 * @param integer $offSet 从第几条数据开始查询,默认0 * @param integer $fetchSize 一次返回多少条数据,默认为10,最多不能超过100条 * @return 直播活动信息 */ public function getLiveInfo($activityId = "", $activityName = "", $activityStatus = "", $offSet = 0, $fetchSize = 10){ $param['method'] = "lecloud.cloudlive.vrs.activity.vrsinfo.search"; $param['activityId'] = $activityId; $param['activityName'] = $activityName; $param['activityStatus'] = $activityStatus; $param['offSet'] = $offSet; $param['fetchSize'] = $fetchSize; return $this->_sendLiveApi($param); } /** * 获取直播活动推流Token * @param string $activityId 直播活动ID * @return string token */ public function getPushToken($activityId){ if(empty($activityId)){ return false; } $param['method']= "lecloud.cloudlive.activity.getPushToken"; $param['activityId'] = $activityId; return $this->_sendLiveApi($param); } /** * 获取直播活动推流地址 * @param string $activityId 直播活动ID * @return array */ public function getPushUrl($activityId){ if(empty($activityId)){ return false; } $param['method']= "lecloud.cloudlive.activity.getPushUrl"; $param['activityId'] = $activityId; return $this->_sendLiveApi($param); } /** * 修改直播活动媒资信息 * @param string $activityId 直播活动ID * @param array $param 活动信息 * @return NULL * * * 例: * ------------------------------------------------------- * $data['activityName'] = "直播活动名称(200个字符以内)"; * $data['startTime'] = "开始时间 格式yyyyMMddHHmmss"; * $data['endTime'] = "结束时间 格式yyyyMMddHHmmss"; * $data['coverImgUrl'] = "直播中-活动封面地址"; * $data['description'] = "直播中-活动描述(255个字符以内)"; * $data['activityCategory'] = "活动分类参见《活动编码》,无二级编码时直接填写一级编码"; * $data['extensions'] = "扩展字段,活动分类修改时,需要修改分类的扩展字段,也可在活动分类不变的情况下单独修改扩展字段,按如下方式传参数"; * $lecloud->modifyLive($activityId, $data); * * */ public function modifyLive($activityId, $param = array()){ if(empty($activityId)){ return false; } $param['method']= "lecloud.cloudlive.vrs.activity.vrsinfo.modify"; $param['activityId'] = $activityId; return $this->_sendLiveApi($param, "post"); } /** * 用于第三方播放器获取直播活动的流信息 * @param string $activityId 直播活动ID * @return array */ public function getStreamForThird($activityId){ if(empty($activityId)){ return false; } $param['method']= "lecloud.cloudlive.vrs.activity.streaminfo.search"; $param['activityId'] = $activityId; return $this->_sendLiveApi($param); } /** * 直播活动结束 * @param string $activityId 直播活动ID * @return NULL */ public function stopLive($activityId){ if(empty($activityId)){ return false; } $param['method']= "lecloud.cloudlive.activity.stop"; $param['activityId'] = $activityId; return $this->_sendLiveApi($param, "post"); } /** * 直播活动机位状态查询 * @param string $activityId 直播活动ID * @return array */ public function getActivityMachineState($activityId){ if(empty($activityId)){ return false; } $param['method']= "letv.cloudlive.activity.getActivityMachineState"; $param['activityId'] = $activityId; return $this->_sendLiveApi($param); } /** * 直播活动封面上传 * @param string $activityId 直播活动ID * @param File $file 图片文件,支持格式jpg、png、gif * @return string 图片地址 */ public function modifyCoverImg($activityId, $file){ if(empty($activityId) OR empty($file)){ return false; } $param['method']= "lecloud.cloudlive.activity.modifyCoverImg"; $param['activityId'] = $activityId; $param['file'] = $file; return $this->_sendLiveApi($param, "post"); } /** * 直播活动创建 * @param string $activityName 直播活动名称(200个字符以内) * @param string $startTime 开始时间 格式yyyyMMddHHmmss * @param string $endTime 结束时间 格式yyyyMMddHHmmss * @param string $coverImgUrl 活动封面地址,如果为空,则系统会默认一张图片 * @param string $description 活动描述(1024个字符以内) * @param integer $liveNum 机位数量,范围为:1,2,3,4. 默认为1 * @param string $codeRateTypes 流的码率类型,逗号分隔。由大到小排列。取值范围:10 流畅;13 标清;16 高清;19 超清;22 720P;25 1080P;99 原画 * @param integer $needRecord 是否支持全程录制 0:否 1:是。默认为0 * @param integer $needTimeShift 是否支持时移 0:否 1:是。默认为0 * @param integer $needFullView 是否全景观看 0:否 1:是。默认为0 * @param string $activityCategory 活动分类,参见《活动编码》。默认为999-其他 * @param integer $playMode 播放模式,0:实时直播。1:流畅直播 * @return string activityId */ public function createLive($activityName, $startTime, $endTime, $coverImgUrl = '', $description = '', $liveNum = 1, $codeRateTypes = '99', $needRecord = 0, $needTimeShift = 0, $needFullView = 0, $activityCategory = '999', $playMode = 0){ if(empty($activityName) OR empty($startTime) OR empty($endTime)){ return false; } $param['method']= "lecloud.cloudlive.activity.create"; $param['activityName'] = $activityName; $param['startTime'] = $startTime; $param['endTime'] = $endTime; $param['coverImgUrl'] = $coverImgUrl; $param['description'] = $description; $param['liveNum'] = $liveNum; $param['codeRateTypes'] = $codeRateTypes; $param['needRecord'] = $needRecord; $param['needTimeShift'] = $needTimeShift; $param['needFullView'] = $needFullView; $param['activityCategory'] = $activityCategory; $param['playMode'] = $playMode; return $this->_sendLiveApi($param, "post"); } /** * 直播活动播放页地址获取 * @param string $activityId 直播活动ID * @return string 直播播放页地址 */ public function getLiveUrl($activityId){ if(empty($activityId)){ return false; } $param['method']= "lecloud.cloudlive.activity.playerpage.getUrl"; $param['activityId'] = $activityId; $playPageUrl = $this->_sendLiveApi($param); return $playPageUrl; } /** * 直播活动安全信息设置 * @param string $activityId 直播活动ID * @param integer $neededPushAuth 是否启用推流鉴权 0:否 1:是 * @param integer $pushUrlValidTime 推流地址有效时长,单位s,启用推流鉴权时有效 * @param string $liveKey 直播安全码,计算推流地址时用到的安全码,如果为空的话,则使用客户的安全码 * @param integer $needIpWhiteList 是否启用IP推流白名单 0:否 1:是。 * @param string $pushIpWhiteList 推流IP白名单,启用IP推流白名单时有效。多个IP时,用英文半角逗号分隔,IP最多可以配置10个,值总长度最大为512 * @param integer $needPlayerDomainWhiteList 是否启用域名白名单,默认为0,否 * @param string $playerDomainWhiteList 域名白名单,多个时逗号分隔。最大长度为512,最多为10个 * @return NULL */ public function setLiveSafe($activityId, $neededPushAuth = 1, $pushUrlValidTime = 60, $liveKey = '', $needIpWhiteList = 0, $pushIpWhiteList = '', $needPlayerDomainWhiteList = 0, $playerDomainWhiteList = ''){ if(empty($activityId) OR empty($file)){ return false; } $param['method']= "lecloud.cloudlive.activity.sercurity.config"; $param['activityId'] = $activityId; $param['neededPushAuth'] = $neededPushAuth; $param['pushUrlValidTime'] = $pushUrlValidTime; $param['liveKey'] = $liveKey; $param['needIpWhiteList'] = $needIpWhiteList; $param['pushIpWhiteList'] = $pushIpWhiteList; $param['needPlayerDomainWhiteList'] = $needPlayerDomainWhiteList; $param['playerDomainWhiteList'] = $playerDomainWhiteList; return $this->_sendLiveApi($param, "post"); } /** * 直播创建打点录制任务 - 原画码率作为输入码率,直播上的码率作为输出码率,执行转码获取打点录制任务 * @param string $liveId 直播ID,直播id查询参考getStreamForThird() * @param string $startTime 开始时间 格式yyyyMMddHHmmss * @param string $endTime 结束时间 格式yyyyMMddHHmmss * @return string taskId -任务ID */ public function createLiveRecTask($liveId, $startTime, $endTime){ if(empty($activityId) OR empty($file)){ return false; } $param['method']= "lecloud.cloudlive.rec.createRecTask"; $param['liveId'] = $liveId; $param['startTime'] = $startTime; $param['endTime'] = $endTime; return $this->_sendLiveApi($param, "post"); } /** * 直播打点录制结果查询 * @param array $param 直播id等信息 * @return array * * * 例: * ------------------------------------------------------- * $data['liveId'] = "直播ID"; * $data['taskId'] = "任务ID"; * $data['offset'] = 开始行数; * $data['size'] = 每页记录数; * $data['startTime'] = "开始时间 格式yyyyMMdd"; * $data['endTime'] = "结束时间 格式yyyyMMdd"; * $lecloud->getLiveRecTask($data); * * */ public function getLiveRecTask($param){ $param['method']= "lecloud.cloudlive.rec.searchResult"; return $this->_sendLiveApi($param); } }
下载地址:附件:Lecloud.class.php
下一篇:浅谈兔子是怎样吃掉狼的