This topic describes how to install and use Alibaba Cloud SDK for PHP. Alibaba Cloud SDK for PHP is a development kit that enables PHP developers to quickly access Alibaba Cloud services. The Alibaba Cloud Client for PHP provides underlying support. Alibaba Cloud SDK for PHP takes the complexity out of coding and allows you to access Alibaba Cloud services such as Elastic Compute Service (ECS), ApsaraDB for RDS, and CloudMonitor.

Prerequisites

  • You must have a RAM user and an AccessKey pair (AccessKey ID and AccessKey Secret) to use Alibaba Cloud SDK for PHP. You can go to the Security Management page of the Alibaba Cloud Console to create or view the AccessKey pair. Alternatively, you can contact your system administrator to query the AccessKey pair.
  • Before you use Alibaba Cloud SDK for PHP to call the API operations of a service, make sure that you have activated this service in the Alibaba Cloud Console.
  • PHP is installed in your system. Alibaba Cloud SDK for PHP supports PHP 5.5.0 or later. You can run the php -v command to query the current PHP version number.
    Note If you use the RsaKeyPair client that is supported only on the Alibaba Cloud Japan site, you must also install the OpenSSL PHP extension. For more information, see OpenSSL.
  • Install cURL and use the TLS backend to compile cURL 7.16.2 or later.

Install Alibaba Cloud SDK for PHP

You can use one of the following methods to install Alibaba Cloud SDK for PHP:

  • (Recommended) Use Composer to install dependencies.

    Composer is a tool for dependency management in PHP.

  • Download and use a ZIP file that contains all classes and dependencies.

    You can click here to download the ZIP file, and extract it in the selected location in the project to obtain Alibaba Cloud SDK for PHP.

To install Alibaba Cloud SDK for PHP as dependencies by using Composer, follow these steps:
  1. Download and install Composer.
    • Linux or macOS:
      curl -sS https://getcomposer.org/installer | php

      Assume that the composer.phar binary file is downloaded to the $HOME directory.

    • Windows:

      Click here to download and install Composer.

  2. Set environment variables.
    • Linux or macOS:
      Note Before you perform this step, ensure that the PATH system variable has a value of /usr/local/bin. Otherwise, you must set available environment variables for Composer.

      Run the following command to move the composer.phar binary file to the /usr/local/bin/composer directory in the global environment.

      sudo mv composer.phar /usr/local/bin/composer

      After setting the environment variables, you can use the composer command on your terminal.

    • Windows

      You can skip this step because environment variables are automatically set when Composer is installed.

  3. Run the following command to install Alibaba Cloud SDK for PHP in the current directory:
    composer require alibabacloud/sdk
    Note If the installation fails due to network problems, you can use the full image of Alibaba Cloud Composer. For more information, see Alibaba Cloud Composer full image.
  4. Introduce the automatic loading tool of Composer in PHP scripts.
    <? php
    
    require __DIR__ . '/vendor/autoload.php'; 

Use Alibaba Cloud SDK for PHP

The following code provides an example on how to use Alibaba Cloud SDK for PHP:

  1. Initialize the client.
    <? php
    
    use AlibabaCloud\Client\AlibabaCloud;
    
    AlibabaCloud::accessKeyClient('accessKeyId', 'accessKeySecret')->asDefaultClient();
  2. Initiate the request.
    • Use RPC:
      <? php
      
      use AlibabaCloud\Client\AlibabaCloud;
      use AlibabaCloud\Client\Exception\ClientException;
      use AlibabaCloud\Client\Exception\ServerException;
      
      try {
          $result = AlibabaCloud::rpc()
                                ->product('Cdn')
                                ->version('2014-11-11')
                                ->action('DescribeCdnService')
                                ->method('POST')
                                ->request();
      
          print_r($result->toArray());
      
      } catch (ClientException $exception) {
          print_r($exception->getErrorMessage());
      } catch (ServerException $exception) {
          print_r($exception->getErrorMessage());
      }
    • Use ROA:
      <? php
      
      use AlibabaCloud\Client\AlibabaCloud;
      use AlibabaCloud\Client\Exception\ClientException;
      use AlibabaCloud\Client\Exception\ServerException;
      
      try {
          $result = AlibabaCloud::roa()
                                ->regionId('cn-hangzhou) // Specify the region of the request. If this parameter is not specified, the region of the client or the default region is used.
                                ->product('CS') // Specify the service.
                                ->version('2015-12-15 ') // Specify the service version.
                                ->action('DescribeClusterServices ') // Specify the API operation of the service.
                                ->serviceCode('cs') // Set the ServiceCode for addressing. This parameter is optional.
                                ->endpointType('openAPI') // Set the endpoint type. This parameter is optional.
                                ->method('GET') // Specify the request method.
                                ->host('cs.aliyun.com ') // Specify the domain name. If this parameter is specified, addressing is not required. If a service uses bearer tokens for authentication, you must specify the domain name.
                                ->pathPattern('/clusters/[ClusterId]/services') // Specify the path rule for ROA.
                                ->withClusterId('123456') // Supply values to the parameters in the path. You can specify a parameter by adding parameter name after the word with.
                                ->request(); // Initiate the request and obtain the result. The request must be at the end of the settings.
      
          print_r($result->toArray());
      
      } catch (ClientException $exception) {
          print_r($exception->getErrorMessage());
      } catch (ServerException $exception) {
          print_r($exception->getErrorMessage());
      }