You can use Composer to install Alibaba Cloud SDK for PHP of the latest version. Alibaba Cloud SDK for PHP of the latest version is different from that of the earlier version in terms of installation, initialization, and usage. For more information about how to initialize and use Alibaba Cloud SDK for PHP of the latest version, see the installation instructions and the sections in this topic.

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.

Usage notes

  • For more information about the code of the server operation SDK for PHP of the latest version, see OpenAPI.
  • You must call Vod::v20170321()->${apiName} to create an API request. ${apiName} indicates the ApsaraVideo VOD operation that you want to call. The first letter of the operation name must be in lower case. For more information about the API operations that you can call, see List of operations by function.

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. In the following sample code, PHP 8.0.16 is used as an example.
<?php
require __DIR__ . '/vendor/autoload.php';

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

define("VOD_CLIENT_NAME", 'AliyunVodClientDemo');

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';
    // Specify the AccessKey pair.
    AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
        ->regionId($regionId)
        ->connectTimeout(1)
        ->timeout(3)
        ->name(VOD_CLIENT_NAME);
}

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. In the following sample code, PHP 8.0.16 is used as an example.
<?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';
    // Specify the STS token.
    AlibabaCloud::stsClient($accessKeyId, $accessKeySecret, $securityToken)
        ->regionId($regionId)
        ->connectTimeout(1)
        ->timeout(3)
        ->name(VOD_CLIENT_NAME);
}

Sample code

In this example, the GetPlayInfo operation is used to query a streaming URL. The PHP version in this example is 8.0.16. The following sample code provides an example:
<?php
<?php
require __DIR__ . '/vendor/autoload.php';

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

define("VOD_CLIENT_NAME", 'AliyunVodClientDemo');

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';
    // Specify the AccessKey pair.
    AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
        ->regionId($regionId)
        ->connectTimeout(1)
        ->timeout(3)
        ->name(VOD_CLIENT_NAME);
}

function getPlayInfo($videoId) {
    return Vod::v20170321()->getPlayInfo()->client(VOD_CLIENT_NAME)
        ->withVideoId($videoId)    // Specify the parameters for the API operation.
        ->withAuthTimeout(3600*24)
        ->format('JSON')  // Specify the response format.
        ->request();      // Execute the request.
}

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