Install and call PHP SDK-Alibaba Cloud Developer Community

For more information about image download, domain name resolution, and time synchronization, click Alibaba open-source image site.

1. Environment preparation

Description if you use RsaKeyPairyou need to install the OpenSSL PHP extension on the client (only supported on the Japanese site).

  • Install cURL and use TLS backend to compile cURL 7.16.2 +.

2. Install Alibaba Cloud SDK for PHP

you can install Alibaba Cloud SDK for PHP in either of the following ways:

  • (Recommended) install dependencies through Composer. Composer is a PHP tool used to manage and install project dependencies.
  • Download and use a ZIP file that contains all classes and dependencies. Click sdk .zip to download the compressed package and decompress it at the selected location in the project to obtain the Alibaba Cloud SDK for PHP.

To install Composer as a dependency, follow these steps: Alibaba Cloud SDK for PHP. 1. Download and install the Composer.

  • Linux/MacOS:
curl -sS | php
  • download the composer.phar binary file to the current directory $HOME.
  • Windows: Click here to download and install the Composer.

2. Set environment variables.

  • Linux/MacOS:

description before completing this step, make sure that PATH the/usr/local/bin path exists in the system variable value. Otherwise, you can set the available environment variables for the Composer as needed.

Run the following command composer.pharmove the binary file to the/usr/local/bin/composer Directory of the global environment. Click trial to enter the call mode.

sudo mv composer.phar /usr/local/bin/composer
  • After setting environment variables, you can use them directly on your terminal. Composer command.
  • Windows: environment variables are automatically configured during Composer installation. Skip this step.

3. Run the following command to install the Alibaba Cloud SDK for PHP to the current directory.

composer require alibabacloud/sdk

Description if the installation fails due to network problems, you can use Alibaba Cloud Composer full images.

4. Introduce the Composer automatic loading tool into PHP scripts.

require __DIR__ . '/vendor/autoload.php';

3. Call Alibaba Cloud SDK for PHP

the following code example shows how to call Alibaba Cloud ECS. For more information, see instructions.

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Ecs\Ecs;
// 使用您获取的凭证设置全局客户端
// AlibabaCloud::accessKeyClient('accessKeyId', 'accessKeySecret')->asDefaultClient();
AlibabaCloud::accessKeyClient('foo', 'bar')
try {
    // 访问产品 APIs
    $request = Ecs::v20140526()->describeRegions();
    // 设置选项/参数并执行请求
    $result = $request->withResourceType('type') // API 的参数
                      ->withInstanceChargeType('type') // API 的参数
                      ->client('client1') // 指定发送客户端,否则使用全局客户端
                      ->debug(true) // 开启调试会输出详细信息
                      ->connectTimeout(0.01) // 连接超时会抛出异常
                      ->timeout(0.01) // 超时会抛出异常
                      ->request(); // 执行请求
    // 也可以传入数组设置
    $options = [
                   'debug'           => true,
                   'connect_timeout' => 0.01,
                   'timeout'         => 0.01,
                   'query'           => [
                       'ResourceType' => 'type',
                       'InstanceChargeType' => 'type',
    // 设置的优先级
    $result2 = Ecs::v20140526()
                                'query' => [
                                    'Key'      => '我会覆盖构造函数的这个值',
                                    'new'      => '我是新增的值',
                                'query' => [
                                    'Key' => '我会覆盖以前的值',
                                    'bar' => '我是新增的值',
                  ->debug(false) // 最后调用的会覆盖前者
} catch (ClientException $exception) {
    echo $exception->getMessage(). PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode(). PHP_EOL;
    echo $exception->getRequestId(). PHP_EOL;
    echo $exception->getErrorMessage(). PHP_EOL;

Alibaba's open-source image site provides comprehensive, efficient, and stable system images, application software downloads, domain name resolution, and time synchronization services."

Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now