You need to enable JavaScript to run this app.
导航
音视频播放
最近更新时间:2024.09.29 17:41:40首次发布时间:2021.02.23 10:42:40

本文为您提供了服务端 PHP SDK 的媒资播放模块的接口调用示例。

前提条件

调用接口前,请先完成 SDK 的安装初始化

调用示例

签发临时播放 Token

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个临时播放 Token,您可以循环调用生成方法。

说明

<?php
require('../vendor/autoload.php');

use Volc\Service\Vod\Models\Request\VodGetPlayInfoRequest;
use Volc\Service\Vod\Models\Response\VodGetPlayInfoResponse;
use Volc\Service\Vod\Vod;

// Create a VOD instance in the specified region.
// $client = Vod::getInstance('cn-north-1');
$client = Vod::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$vid = "your vid";
$expire = 600; // token有效期

$req = new VodGetPlayInfoRequest();
$req->setVid($vid);
$req->setSsl('1');

try {
    $response = $client->getPlayAuthToken($req,$expire);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}

echo $response;

获取播放地址

接口请求参数和返回参数详见OpenAPI:获取播放地址

<?php
require('../../vendor/autoload.php');

// Create a VOD instance in the specified region.
// $client = Volc\Service\Vod\Vod::getInstance('cn-north-1');
$client = Volc\Service\Vod\Vod::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$request = new Volc\Service\Vod\Models\Request\VodGetPlayInfoRequest();
$request->setVid("your Vid");
$request->setFormat("your Format");
$request->setCodec("your Codec");
$request->setDefinition("your Definition");
$request->setFileType("your FileType");
$request->setLogoType("your LogoType");
$request->setBase64("your Base64");
$request->setSsl("your Ssl");
$request->setNeedThumbs("your NeedThumbs");
$request->setNeedBarrageMask("your NeedBarrageMask");
$request->setCdnType("your CdnType");
$request->setUnionInfo("your UnionInfo");
$request->setHDRDefinition("your HDRDefinition");
$request->setPlayScene("your PlayScene");
$request->setDrmExpireTimestamp("your DrmExpireTimestamp");
$request->setQuality("your Quality");
$request->setPlayConfig("your PlayConfig");
$request->setNeedOriginal("your NeedOriginal");
$request->setForceExpire("your ForceExpire");


$response = new Volc\Service\Vod\Models\Response\VodGetPlayInfoResponse();
try {
    $response = $client->getPlayInfo($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response != null && $response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError()->serializeToJsonString(), "\n";
} else {
    echo $response->serializeToJsonString(), "\n";
}

签发私有加密 Token

私有加密 Token 用于 Web 点播 SDK 播放私有加密视频,详情请见以下文档:

私有加密 Token 可由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个私有加密 Token,您可以循环调用生成方法。

<?php
require('../../vendor/autoload.php');

// Create a VOD instance in the specified region.
// $client = Volc\Service\Vod\Vod::getInstance('cn-north-1');
$client = Volc\Service\Vod\Vod::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

// Web 点播 SDK 内部携带 playAuthId、vid、unionInfo 向应用服务端请求获取私有加密 Token,详细示例代码和参数说明请见 [Web 端播放私有加密视频实现方式部分](https://www.volcengine.com/docs/4/68698#%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F-2)
$request = new Volc\Service\Vod\Models\Request\VodGetPrivateDrmPlayAuthRequest();
// 需指定 DrmType 为 webdevice
$request->setDrmType("your DrmType");
$request->setVid("your Vid");
$request->setPlayAuthIds("your PlayAuthIds");
$request->setUnionInfo("your UnionInfo");


$response = new Volc\Service\Vod\Models\Response\VodGetPrivateDrmPlayAuthResponse();
try {
    $response = $client->getPrivateDrmPlayAuth($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response != null && $response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError()->serializeToJsonString(), "\n";
} else {
    echo $response->serializeToJsonString(), "\n";
}

签发 HLS 标准加密 Token

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个 HLS 标准加密 Token,您可以循环调用生成方法。HLS 标准加密 Token 用于 Web 客户端播放 HLS 加密音视频,详见 HLS 标准加密

<?php
require('../../vendor/autoload.php');

// Create a VOD instance in the specified region.
// $client = Volc\Service\Vod\Vod::getInstance('cn-north-1');
$client = Volc\Service\Vod\Vod::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$request = new Volc\Service\Vod\Models\Request\VodGetHlsDecryptionKeyRequest();
$request->setDrmAuthToken("your DrmAuthToken");
$request->setAk("your Ak");
$request->setSource("your Source");


$response = new Volc\Service\Vod\Models\Response\VodGetHlsDecryptionKeyResponse();
try {
    $response = $client->getHlsDecryptionKey($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response != null && $response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError()->serializeToJsonString(), "\n";
} else {
    echo $response->serializeToJsonString(), "\n";
}