Use PHP SDK

Last Updated: Mar 22, 2018

The Alibaba Cloud PHP Software Development Kit (SDK) allows you to access Alibaba Cloud services such as Elastic Compute Service (ECS), Server Load Balancer (SLB), and CloudMonitor. You can access Alibaba Cloud services without the need to handle API related tasks, such as signing and constructing your requests.

This document introduces how to obtain and call Alibaba Cloud PHP SDK.

If you have any problem while using PHP SDK, please join the DingTalk group: 11771185 (the official SDK customer service group of Alibaba Cloud) for consultation.

Prerequisites

  • To use Alibaba Cloud PHP SDK, you must have an Alibaba Cloud account as well as an AccessKey.

    The AccessKey is required when initializing AcsClient. You can create an AccessKey in the Alibaba Cloud console. For more information, see Create an AccessKey.

    Note: To increase the security of your account, we recommend that you use the AccessKey of the RAM user to access Alibaba Cloud services.

  • To use Alibaba Cloud PHP SDK to access the APIs of a product, you must first activate the product on the Alibaba Cloud console if required.

  • Alibaba Cloud PHP SDK requires PHP version 5.3, 5.4 5.5, 5.6, 7.0 or 7.1. (You can use the php -v command to obtain the current PHP version.)

Install PHP SDK

  1. Run the following command to clone the source code of the PHP SDK:

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

  2. Add the PHP SDK reference to your code.

    For example, the cloned directory of the PHP SDK is /path/to/aliyun-openapi-php-sdk, add the following code to add the reference:

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

Use the SDK

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

  1. Create a DefaultAcsClient client.
  2. Create a request.
  3. Send the request and print 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. ?>
Thank you! We've received your feedback.