This topic describes how to initialize the server operation SDK for PHP by using an AccessKey pair or a Security Token Service (STS) token. You can use one of the methods as needed.
Background information
The server SDK of ApsaraVideo VOD can be initialized by using two different methods. You can initialize the SDK by using the AccessKey pair of an Alibaba Cloud account or a RAM user that is granted required permissions based on authorization policies. The AccessKey pair remains valid after the Alibaba Cloud account or RAM user is created. We recommend that you use this method on the server. Alternatively, you can initialize the SDK by using an STS token that is granted required permissions based on authorization policies. You can specify the validity period of the STS token.
Prerequisites
The server operation SDK for PHP is installed. For more information, see Installation.
A region is specified for using ApsaraVideo VOD. For example, if you use ApsaraVideo VOD in the China (Shanghai) region, the region ID is cn-shanghai. For more information, see VOD centers and endpoints.
Initialize the SDK by using an AccessKey pair
Obtain an AccessKey pair to complete identity verification so that you can call server API operations. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.
Use the AccessKey pair to initialize the SDK. The following sample code provides an example:
If you fail to call ApsaraVideo VOD operations in other regions, update aliyun-php-sdk-core to V1.3.8 or later, and aliyun-php-sdk-vod to V2.15.1 or later.
<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php'; // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;
function initVodClient($accessKeyId, $accessKeySecret) {
$regionId = 'cn-shanghai'; // Specify the region from which you want to access ApsaraVideo VOD.
$profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
return new DefaultAcsClient($profile);
}
try {
$client = initVodClient('<Your AccessKeyId>', '<Your AccessKeySecret>');
var_dump($client);
} catch (Exception $e) {
print $e->getMessage()."\n";
}
Initialize the SDK by using an STS token
An STS token is obtained for initializing the SDK. For more information about how to obtain an STS token, see the "Use STS to authorize access" section of the Create a role and grant temporary access permissions to the role by using STS topic.
Use an STS token to initialize the SDK. The following sample code provides an example:
<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php'; // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;
function initVodClient($accessKeyId, $accessKeySecret, $securityToken) {
$regionId = 'cn-shanghai'; // Specify the region from which you want to access ApsaraVideo VOD.
$profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret, $securityToken);
return new DefaultAcsClient($profile);
}
try {
$client = initVodClient('<AccessKeyId>', '<AccessKeySecret>', '<SecurityToken>');
var_dump($client);
} catch (Exception $e) {
print $e->getMessage()."\n";
}