SDK integration

更新时间:
复制 MD 格式

Alibaba Cloud SDK simplifies the development process, enables quick feature integration, and reduces O&M costs. Integrate Alibaba Cloud SDK V1.0 for PHP in your project to start calling API operations.

Environment requirements

PHP >= 5.5

Install SDK

When you install Alibaba Cloud SDK V1.0 for PHP, the core library is automatically installed. You only need to install the cloud product SDK.

Cloud product SDK

The V1.0 SDK of an Alibaba Cloud service provides the request and response objects of API operations and the Unmarshaller object for serializing return values. The following example adds the ECS V1.0 SDK as a dependency:

# ECS V1.0 SDK
composer require alibabacloud/ecs

The V1.0 SDKs use the naming format alibabacloud/${product_name}. You can also find information about the V1.0 SDK for a specific product in the SDK Center.

Core SDK

The core library includes the client object, signature logic, and error handling logic required for calling API operations. To make generic calls or install a specific version of the core library, run the following command:

composer require alibabacloud/client

For more information about the latest version, see alibabacloud/client - Packagist.

Use the SDK

The following example uses Alibaba Cloud SDK V1.0 to call the DescribeInstances operation of ECS.

1. Initialize a request client

All API operations are called by using the Client object from the core library. Before you call an API operation, initialize a request client. In this example, the client is initialized with an AccessKey pair. For more information, see Manage access credentials.

Note

This example reads credentials from environment variables. Before you run the code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. For more information, see Configure environment variables on Linux, macOS, and Windows.

use AlibabaCloud\Client\AlibabaCloud;

// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'))
            ->regionId('<REGION_ID>')
            ->asDefaultClient();

2. Create a request object

Use the request object provided by the SDK to encapsulate request parameters.

use AlibabaCloud\Ecs\Ecs;

// Build the request parameters.    
$request = Ecs::v20140526()
    ->describeInstances() // Create a request object for the API operation.
    ->withInstanceIds(json_encode(["i-bp1dXXXXXXXXXXXX"])) // The InstanceIds request parameter. Specify the ID of your instance.
    ->withPageSize(10) // The PageSize request parameter. Specify the number of entries that you want to obtain.
    ->withPageNumber(1); // The PageNumber request parameter. Specify the number of the page that you want to obtain.

3. Initiate an API request

Call the request() function to send the request.

$result = $request
    ->debug(true) // Output details. If this field is set to false, details are not output.
    ->request(); // Execute the request.
print_r($result->toArray()); // Print the response to the API request.

4. Handle errors

If an error occurs when you call an API operation, capture ServerException and ClientException to obtain the error message.

Complete sample code

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

require_once 'vendor/autoload.php';

try {
    // Obtain the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET of the RAM user from environment variables.
    AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'))
        ->regionId('cn-hangzhou') // The region ID. This example uses cn-hangzhou.
        ->asDefaultClient();

    // Construct the request parameters.
    $request = Ecs::v20140526()
        ->describeInstances()
        ->withInstanceIds(json_encode(["i-bp1dXXXXXXXXXXXX"]))
        ->withPageSize(10)
        ->withPageNumber(1);

    $result = $request
        ->debug(true)
        ->request();
    print_r($result->toArray());
} catch (ClientException $exception) {
    # This is for demonstration only. Handle exceptions with caution and do not ignore them in your project.
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    # This is for demonstration only. Handle exceptions with caution and do not ignore them in your project.
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

References