<?php
namespace app\common\library;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Vod\Vod;

use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class Video
{
    protected $accessKeyId = '';
    protected $accessKeySecret = '';
    public function __construct(){
        define("VOD_CLIENT_NAME", 'AliyunVodClientDemo');

        $config = config('vod');
        $this->accessKeyId = $config['accessKeyId'];
        $this->accessKeySecret = $config['accessKeySecret'];

        $this->initVodClient();
    }

    //初始化
    public function initVodClient() {
        $regionId = 'cn-beijing';
        //填入AccessKey信息
        AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)
            ->regionId($regionId)
            ->connectTimeout(1)
            ->timeout(3)
            ->name(VOD_CLIENT_NAME);
    }

    //GetPlayInfo
    public function getPlayInfo($videoId) {
        return Vod::v20170321()->getPlayInfo()->client(VOD_CLIENT_NAME)
            ->withVideoId($videoId)    // 指定接口参数
            ->withAuthTimeout(3600*24)
            ->format('JSON')  // 指定返回格式
            ->request();      // 执行请求
    }

    /**
     * @throws ClientException
     * @throws ServerException
     */
    public function getVideoPlayAuth($videoId) {
        return Vod::v20170321()->getVideoPlayAuth()->client(VOD_CLIENT_NAME)
            ->withVideoId($videoId)    // 指定音/视频ID
            ->withAuthInfoTimeout(3600)
            ->format('JSON')  // 指定返回格式
            ->request();      // 执行请求
    }

}
