cloudflare视频转码接口

2019-09-19 10:51:09 浏览数 (3)

代码语言:javascript复制
 /**
     * 视频上传转码
     * @param $video_url
     * @param $vide_name
     */
    public static function postVideo($video_url, $vide_name)
    {
        require_once(__DIR__ . '/../../common/libs/requests/library/Requests.php');
        Requests::register_autoloader();
        $headers = array(
            'X-Auth-Key' => Yii::$app->params['cf_key'],
            'X-Auth-Email' => Yii::$app->params['cf_email']
        );
        $url = "https://api.cloudflare.com/client/v4/accounts/".Yii::$app->params['cf_account_id']."/stream/copy";
        $data = '{"url":"' . $video_url . '","meta":{"name":"' . $vide_name . '"}}';
        $response = Requests::post($url, $headers, $data);
        $postData = json_decode($response->body);
        if ($postData->success) {
            return $postData;
        } else {
            return false;
        }
    }

    /**
     * 获取视频转码后的状态
     * @param string $videoCode
     * @return bool|mixed
     */
    public static function getVideoStatus($videoCode)
    {
        require_once(__DIR__ . '/../../common/libs/requests/library/Requests.php');
        Requests::register_autoloader();
        $headers = array(
            'X-Auth-Key' => Yii::$app->params['cf_key'],
            'X-Auth-Email' => Yii::$app->params['cf_email'],
            'Content-Type' => 'application/json'
        );
        $url = "https://api.cloudflare.com/client/v4/accounts/".Yii::$app->params['cf_account_id']."/media/{$videoCode}";
        $response = Requests::get($url, $headers);
        $postData = json_decode($response->body);
        if($postData->success){
            return $postData;
        }else{
            return false;
        }
    }

    /**
     * 删除转码后的视频
     * @param string $videoCode
     * @return bool
     */
    public static function delVideo($videoCode)
    {
        require_once(__DIR__ . '/../../common/libs/requests/library/Requests.php');
        Requests::register_autoloader();
        $headers = array(
            'X-Auth-Key' => Yii::$app->params['cf_key'],
            'X-Auth-Email' => Yii::$app->params['cf_email'],
            'Content-Type' => 'application/json'
        );
        $url = "https://api.cloudflare.com/client/v4/zones/".Yii::$app->params['cf_zone_id']."/media/{$videoCode}";
        $response = Requests::delete($url, $headers);
        if($response->status_code == 200){
            return true;
        }else{
            return false;
        }
    }

0 人点赞