PHP SDK user guide

Last Updated: Nov 16, 2017

Introduction

Welcome to the Alibaba Cloud Software Development Kit (SDK). Alibaba Cloud PHP SDK allows you to access multiple Alibaba Cloud services such as ECS and CloudMonitor without complex coding. We will introduce how to get Alibaba Cloud PHP SDK and use it.

Prepare the environment

  1. Alibaba Cloud PHP SDK is available for PHP versions 5.3, 5.4, 5.5, 5.6, 7.0 and 7.1. (You can use the php -v command to obtain the current PHP version number.)

  2. To use Alibaba Cloud PHP SDK, you need an Alibaba Cloud account and an Access Key ID and Access Key Secret. Create and view your AccessKey in the Alibaba Cloud console, or contact the system administrator.

  3. To access a product API with Alibaba Cloud SDK, you need to activate the product in Alibaba Cloud console.

How to get and install SDK

Download the SDK from PHP SDK resource list.

Note: SDKs of some Alibaba Cloud products, for example Table Store, are not in this list, go to the details page of these products for corresponding SDKs.

Currently, only one method is supported for obtaining and using the PHP SDK.

Method: Source code reference

First download PHP SDK source code from GitHub:

  1. git clone https://github.com/aliyun/aliyun-openapi-php-sdk.git

Assuming the download path of PHP SDK is /path/to/aliyun-openapi-php-sdk. Add a reference to PHP SDK in your code files.

  1. require_once '/path/to/aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php';

After that you can use Alibaba Cloud PHP SDK in your code.

Start to use

The following code shows the three main steps to use Alibaba Cloud SDK:

  1. Create a DefaultAcsClient instance and initialize it;
  2. Create an API request and set parameters;
  3. Initiate the request and process the response.
  1. <?php
  2. use Ecs\Request\V20140526\DescribeInstancesRequest;
  3. # Create a DefaultAcsClient instance and initialize it;
  4. $clientProfile = DefaultProfile::getProfile(
  5. "<your-region-id>", # Your Region ID
  6. "<your-access-key-id>", # Your Access Key ID
  7. "<your-access-key-secret>" # Your Access Key Secret
  8. );
  9. $client = new DefaultAcsClient($clientProfile);
  10. # Create an API request and set parameters
  11. $request = new DescribeInstancesRequest();
  12. $request->setPageSize(10);
  13. # Initiate a request and process the response
  14. try {
  15. $response = $client->getAcsResponse($request);
  16. print_r($response);
  17. } catch(ServerException $e) {
  18. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  19. } catch(ClientException $e) {
  20. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  21. }
  22. ?>

When you create a DefaultAcsClient instance and initialize it, three parameters are required: Region ID, Access Key ID and Access Key Secret. Access Key ID and Access Key Secret are available from the console; while Region ID is available in Region List.

Thank you! We've received your feedback.