This topic describes how to install Security Token Service (STS) SDK for PHP and provides an example on how to use STS SDK for PHP.

Background information

  • Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
  • For more information about STS API operations, see What is STS?.
  • For more information about STS endpoints, see Endpoints.

Install the SDK for PHP

You can download the installation packages of the SDKs for PHP from the following links:

Example

The following sample code provides an example on how to call the AssumeRole operation by using the SDK for PHP. For other operations, you can use OpenAPI Explorer to debug the operations and obtain sample code.

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Construct an Alibaba Cloud client to initiate requests. 
// Specify the AccessKey ID and AccessKey secret of the requester. The requester can be a Resource Access Management (RAM) user or a RAM role. 
AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();
// Specify request parameters and initiate a request. For more information about the parameters, see API Reference. 
try {
    $result = AlibabaCloud::rpc()
                          ->product('Sts')
                          // ->scheme('https') // https | http
                          ->version('2015-04-01')
                          ->action('AssumeRole')
                          ->method('POST')
                          ->host('sts.aliyuncs.com')
                          ->options([
                                        'query' => [
                                          'RegionId' => "cn-hangzhou",
                                          'RoleArn' => "<RoleArn>",
                                          'RoleSessionName' => "<RoleSessionName>",
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}