All Products
Search
Document Center

Alibaba Cloud SDK:Configure Alibaba Cloud SDK V1.0 for PHP

Last Updated:May 16, 2025

To facilitate API calls, we recommend that you integrate with Alibaba Cloud SDK in your project. SDKs simplify the development process, quickly integrate features, and greatly reduce the O&M cost. This topic describes how to integrate Alibaba Cloud SDK V1.0 for PHP in your project and use the SDK for development.

Prerequisites

PHP 5.5 or later is installed.

Install Alibaba Cloud SDK V1.0 for PHP

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

Service SDK

The SDK V1.0 of an Alibaba Cloud service provides the request and response objects of API operations and the Unmarshaller object that is used to serialize the values to be returned. The following sample code shows how to configure the SDK V1.0 of Elastic Compute Service (ECS) as a dependency:

# ECS V1.0 SDK
composer require alibabacloud/ecs

The SDK V1.0 of each Alibaba Cloud service is named in the alibabacloud/${Service name} format. For more information about the SDK V1.0 of each Alibaba Cloud service, visit SDK Center.

Core library

The core library of an SDK includes the client object, the signature logic, and the error handling logic, which are required information for calling API operations. To make generic calls or install the core library of a specific version, 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 shows how to use Alibaba Cloud SDK V1.0 to call the DescribeInstances API operation of ECS.

1. Initialize a request client

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

Note

In this example, the AccessKey pair is obtained from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, which must be configured before you run the code. For more information, see Configure environment variables in 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

You can use the request object provided by the SDK to encapsulate the 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 initiate an API 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 is triggered when you can an API operation, you can 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 AccessKey pair of the Resource Access Management (RAM) user from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
    AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'))
        ->regionId('cn-hangzhou') // The region ID. In this example, cn-hangzhou is used.
        ->asDefaultClient();

    // Build 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) {
    # Handle errors with caution based on your actual business scenario and do not ignore exceptions in your project. The error message displayed in this example is for reference only. 
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    # Handle errors with caution based on your actual business scenario and do not ignore exceptions in your project. The error message displayed in this example is for reference only. 
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

References

  • For more information about advanced SDK settings, such as proxy settings and timeout settings, see Advanced settings.

  • For more information about how to create an AccessKey pair, see Create an AccessKey pair.