zoukankan      html  css  js  c++  java
  • 阿里云视频点播sdk封装 php

    阿里云的点播sdk封装到同一个类里面了,调用的时候分方便

    1.首先下载 aliyun-php-sdk(不知道的自行去阿里云文档上搜索下载)

    2.把零散的方法放到同一个类里面

    <?php
    require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // 假定您的源码文件和aliyun-php-sdk处于同一目录
    use vodRequestV20170321 as vod;
    use StsRequestV20150401 as Sts;
    class AliyunVideo {
        
        //private $accessKeyId = 'zsZ';
        //private $accessKeySecret = 'myQkfV';
        private $accessKeyId = 'uWLFMAO';
        private $accessKeySecret = '5FS1b39QnRdzRNVWp';
        private $regionId = 'cn-shanghai';    // 点播服务所在的Region,国内请填cn-shanghai,不要填写别的区域
        private $endpoint = 'sts.cn-shanghai.aliyuncs.com';
        private $client = null ;
        private $sts = null ;
    
        public function __construct(){
    
            try {
                $this -> init_vod_client($this -> accessKeyId,$this -> accessKeySecret);
            } catch (Exception $e) {
                print $e->getMessage();
            }
        }
    
    
    
    
        /**
         * @desc 初始化客户端
         * @param  [type] $accessKeyId     [description]
         * @param  [type] $accessKeySecret [description]
         * @return [type]                  [description]
         */
        public function init_vod_client($accessKeyId, $accessKeySecret) {
            //初始化视频客户端
            $profile = DefaultProfile::getProfile($this -> regionId, $accessKeyId, $accessKeySecret);
            $this -> client = new DefaultAcsClient($profile);
            //初始化sts授权策略客户端
            DefaultProfile::addEndpoint($this -> regionId, $this -> regionId, "Sts", $this -> endpoint);
            $iClientProfile = DefaultProfile::getProfile($this -> regionId, $accessKeyId,  $accessKeySecret);
            $this -> sts = new DefaultAcsClient($iClientProfile);
    
    
        }
    
    
        /**
         * @desc 刷新视频上传凭证
         * @param  [type] $client  [description]
         * @param  [type] $videoId [description]
         * @return [type]          [description]
         */
        function refresh_upload_video( $videoId) {
            $request = new vodRefreshUploadVideoRequest();
            $request->setVideoId($videoId);
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
    
    
    
        /**
         * @desc [获取上传地址和凭证]
         * @param  [type] $client [description]
         * @param array $fileInfo = array();
         * @return [type]         [description]
         */
        public function create_upload_video($fileInfo) {
            $request = new vodCreateUploadVideoRequest();
    
            $request->setTitle($fileInfo['title']);        // 视频标题(必填参数)
            $request->setFileName($fileInfo['filename']); // 视频源文件名称,必须包含扩展名(必填参数)
            $request->setDescription($fileInfo['desc']);  // 视频源文件描述(可选)
            //$request->setCoverURL("http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"); // 自定义视频封面(可选)
            if($fileInfo['thumb']){ $request -> setCoverURL($fileInfo['thumb']) ; }
            if($fileInfo['catid']){ $request -> setCateId($fileInfo['catid']) ; }
            if($fileInfo['tags']){
                $request -> setTags($fileInfo['tags']);
            }
            $request->setAcceptFormat('JSON');
    
            return $this -> client -> getAcsResponse($request);
        }    
    
    
        /**
         * @desc 获取播放地址接口
         * @param  [type] $client  [为上面init_vod_client接口返回的客户端对象]
         * @param  [type] $videoId [为视频ID]
         * @return [type]          [description]
         */
        public function get_play_info( $videoId) {
            $request = new vodGetPlayInfoRequest();
            $request->setVideoId($videoId);
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
    
        /**
         * @desc 获取sts授权策略信息
         * @author 646943067@qq.com
         * @version 1.0
         * @date 2018-11-27
         */
        public function getStsInfo(){
            $roleArn = "acs:ram::1357249633099392:role/aliyunvoduploadauth";  //角色资源描述符,在RAM的控制台的资源详情页上可以获取
            // 在扮演角色(AssumeRole)时,可以附加一个授权策略,进一步限制角色的权限;
            // 详情请参考《RAM使用指南》
            // 此授权策略表示读取所有OSS的只读权限
            $policy=<<<POLICY
    {
      "Statement": [
        {
          "Action": "sts:AssumeRole",
          "Effect": "Allow",
          "Principal": {
            "RAM": [
              "acs:ram::1357249633099392:root"
            ]
          }
        }
      ],
      "Version": "1"
    }
    POLICY;
            
            $request = new StsAssumeRoleRequest();
            // RoleSessionName即临时身份的会话名称,用于区分不同的临时身份
            // 您可以使用您的客户的ID作为会话名称
            $request->setRoleSessionName("client_name");
            $request->setRoleArn($roleArn);
            $request->setPolicy($policy);
            $request->setDurationSeconds(3600);
            
            try {
                $response = $this -> sts ->getAcsResponse($request);
                print_r($response);
            } catch(ServerException $e) {
                print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "
    ";
            } catch(ClientException $e) {
                print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "
    ";
            }
            
            
            
        }
    
    
    
        /**
         * @desc 删除媒体流
         * 可删除视频流或音频流信息及存储文件,并支持批量删除;删除后当CDN缓存过期,该路流会无法播放,请谨慎操作
         * @param  [type] $client  [description]
         * @param  [type] $videoId [description]
         * @param  [type] $jobIds  [description]
         * @return [type]          [description]
         */
        function delete_stream( $videoId, $jobIds) {
            $request = new vodDeleteStreamRequest();
            $request->setVideoId($videoId);
            $request->setJobIds($jobIds);   // 媒体流转码的作业ID列表,多个用逗号分隔;JobId可通过获取播放地址接口(GetPlayInfo)获取到。
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
    
    
        /**
         * @desc 获取视频列表
         * @param  [type] $client [description]
         * @return [type]         [description]
         */
        function get_video_list() {
            $request = new vodGetVideoListRequest();
            // 示例:分别取一个月前、当前时间的UTC时间作为筛选视频列表的起止时间
            $localTimeZone = date_default_timezone_get();
            date_default_timezone_set('UTC');
            $utcNow = gmdate('Y-m-dTH:i:s');
            $utcMonthAgo = gmdate('Y-m-dTH:i:s', time() - 30*86400);
            date_default_timezone_set($localTimeZone);
            $request->setStartTime($utcMonthAgo);   // 视频创建的起始时间,为UTC格式
            $request->setEndTime($utcNow);          // 视频创建的结束时间,为UTC格式
            #$request->setStatus('Uploading,Normal,Transcoding');  // 视频状态,默认获取所有状态的视频,多个用逗号分隔
            #$request->setCateId(0);               // 按分类进行筛选
            $request->setPageNo(1);
            $request->setPageSize(20);
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
    
    
        /**
         * @desc 获取源文件信息(含原片下载地址)
         * @param  [type] $client  [description]
         * @param  [type] $videoId [description]
         * @return [type]          [description]
         */
        function get_mezzanine_info( $videoId) {
            $request = new vodGetMezzanineInfoRequest();
            $request->setVideoId($videoId);
            $request->setAuthTimeout(3600*5);   // 原片下载地址过期时间,单位:秒,默认为3600秒
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
        /**
         * @desc 删除视频
         * @param  [type] $client   [description]
         * @param  [type] $videoIds [description]
         * @return [type]           [description]
         */
        function delete_videos( $videoIds) {
            $request = new vodDeleteVideoRequest();
            $request->setVideoIds($videoIds);   // 支持批量删除视频;videoIds为传入的视频ID列表,多个用逗号分隔
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
        /**
         * @desc 修改视频信息
         * @param  [type] $client  [description]
         * @param  [type] $videoId [description]
         * @return [type]          [description]
         */
        function update_video_info( $videoId) {
            $request = new vodUpdateVideoInfoRequest();
            $request->setVideoId($videoId);
            $request->setTitle('New Title');   // 更改视频标题
            $request->setDescription('New Description');    // 更改视频描述
            $request->setCoverURL('http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png');  // 更改视频封面
            $request->setTags('tag1,tag2');    // 更改视频标签,多个用逗号分隔
            $request->setCateId(0);       // 更改视频分类(可在点播控制台·全局设置·分类管理里查看分类ID:https://vod.console.aliyun.com/#/vod/settings/category)
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
        /**
         * @desc 获取视频信息
         * @param  [type] $client  [description]
         * @param  [type] $videoId [description]
         * @return [type]          [description]
         */
        function get_video_info( $videoId) {
            $request = new vodGetVideoInfoRequest();
            $request->setVideoId($videoId);
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
        /**
         * @desc 获取指定的分类信息,及其子分类(即下一级分类)的列表
         * @param  [type]  $client   [description]
         * @param  integer $cateId   [description]
         * @param  integer $pageNo   [description]
         * @param  integer $pageSize [description]
         * @return [type]            [description]
         */
        function get_categories( $cateId=-1, $pageNo=1, $pageSize=10) {
            $request = new vodGetCategoriesRequest();
            $request->setCateId($cateId);   // 分类ID,默认为根节点分类ID即-1
            $request->setPageNo($pageNo);
            $request->setPageSize($pageSize);
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
    
    
        /**
         * @desc 删除分类
         * 删除视频分类,同时会删除其下级分类(包括二级分类和三级分类),请慎重操作
         * @param  [type] $client [description]
         * @param  [type] $cateId [description]
         * @return [type]         [description]
         */
        function delete_category( $cateId) {
            $request = new vodDeleteCategoryRequest();
            $request->setCateId($cateId);
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
        /**
         * @desc 修改分类
         * @param  [type] $client   [description]
         * @param  [type] $cateId   [description]
         * @param  [type] $cateName [description]
         * @return [type]           [description]
         */
        function update_category( $cateId, $cateName) {
            $request = new vodUpdateCategoryRequest();
            $request->setCateId($cateId);
            $request->setCateName($cateName);   // 分类名称,不能超过64个字节,UTF8编码
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
        /**
         * @desc 创建分类 
         * 1.创建视频分类,最大支持三级分类,每个分类最多支持创建100个子分类
         * 2.一级分类最大也是支持100个,若有更大需求请提工单联系我们
         * @param [type]  $client   [description]
         * @param [type]  $cateName [description]
         * @param integer $parentId [description]
         */
        function add_category( $cateName, $parentId=-1) {
            $request = new vodAddCategoryRequest();
            $request->setCateName($cateName);   // 分类名称,不能超过64个字节,UTF8编码
            $request->setParentId($parentId);   // 父分类ID,若不填,则默认生成一级分类,根节点分类ID为-1
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
    
        /**
         * @desc 获取图片上传地址和凭证
         * @param  [type] $client    [description]
         * @param  [type] $imageType [description]
         * @param  [type] $imageExt  [description]
         * @return [type]            [description]
         */
        function create_upload_image( $imageType, $imageExt) {
            $request = new vodCreateUploadImageRequest();
            $request->setImageType($imageType);
            $request->setImageExt($imageExt);
            $request->setAcceptFormat('JSON');
            return $this -> client ->getAcsResponse($request);
        }
    
    
    
    
    
    }

    3.调用

    假如步骤二的类文件名为video.php

    那么代码如下:

    require_once './video.php';
    $videoObj = new AliyunVideo();  //实例化阿里云对象
    $videoObj -> create_upload_video();
  • 相关阅读:
    JavaScript DOM 选择器 querySelector
    JavaScript call()函数的应用
    flex布局中 align-items 和 align-content的区别
    移动端WEB
    触发器与存储过程
    游标
    触发器---存储过程---存储函数
    mysql中check无效
    mysql唯一性约束和索引
    分页查询
  • 原文地址:https://www.cnblogs.com/ailingfei/p/10032102.html
Copyright © 2011-2022 走看看