新版PHP SDK支持Composer方式,与旧版在安装、初始化和使用上完全不同,请提前阅读安装说明,并参考以下文档进行初始化和使用。

初始化

点播服务的接入区域请参见接入区域和标识,例如:接入区域在上海,请使用cn-shanghai

  • 填入账号AccessKey信息,进行初始化。示例如下:
    <?php
    require __DIR__ . '/vendor/autoload.php';
    
    <?php
    require __DIR__ . '/vendor/autoload.php';
    use AlibabaCloud\Client\AlibabaCloud;
    use AlibabaCloud\Client\Exception\ClientException;
    use AlibabaCloud\Client\Exception\ServerException;
    use AlibabaCloud\Vod\Vod;
    
    define(VOD_CLIENT_NAME, 'AliyunVodClientDemo');
    
    function initVodClient($accessKeyId, $accessKeySecret) {
        $regionId = 'cn-shanghai';
        AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
                    ->regionId($regionId)
                    ->connectTimeout(1)
                    ->timeout(3)
                    ->name(VOD_CLIENT_NAME);
    }
  • 填入STS信息,进行初始化。示例如下:
    <?php
    require __DIR__ . '/vendor/autoload.php';
    use AlibabaCloud\Client\AlibabaCloud;
    use AlibabaCloud\Client\Exception\ClientException;
    use AlibabaCloud\Client\Exception\ServerException;
    use AlibabaCloud\Vod\Vod;
    
    define(VOD_CLIENT_NAME, 'AliyunVodClientDemo');
    
    function initVodClient($accessKeyId, $accessKeySecret, $securityToken) {
        $regionId = 'cn-shanghai';
        AlibabaCloud::stsClient($accessKeyId, $accessKeySecret, $securityToken)
            ->regionId($regionId)
            ->connectTimeout(1)
            ->timeout(3)
            ->name(VOD_CLIENT_NAME);
    }

使用说明

创建API请求时,调用的方法名为Vod::v20170321()->${apiName},其中${apiName}需要替换成要使用的点播API,首字母需要调整为小写。可用的API名称请参见API概览

调用示例

以调用获取播放地址接口GetPlayInfo为例。示例如下:
function getPlayInfo($videoId) {
    return Vod::v20170321()->getPlayInfo()->client(VOD_CLIENT_NAME)
              ->withVideoId($videoId)    // 指定接口参数
              ->withAuthTimeout(3600*24)
              ->format('JSON')  // 指定返回格式
              ->request();      // 执行请求
}

try {
    initVodClient('<AccessKeyId>', '<AccessKeySecret>');
    $playInfo = getPlayInfo('<videoId>');
    print_r($playInfo->PlayInfoList->PlayInfo);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}