/openapi-sdk-php

The OpenAPI SDK for PHP with Composer support

Primary LanguagePHPOtherNOASSERTION

English | 简体中文

Latest Stable Version Total Downloads Latest Unstable Version License
Scrutinizer Code Quality Travis Build Status Appveyor Build Status codecov Code Intelligence Status

About

Alibaba Cloud SDK for PHP is a development kit that supports quick access to products, dependency on Alibaba Cloud Client for PHP.

Release Notes

We developed a new kernel on the principle of eliminating known issues and compatible with old grammar, adding the following features:

Requirements

  • You must use PHP 5.5.0 or later.
  • if you use the RsaKeyPair (Only Japan station is supported) client, you will also need OpenSSL PHP extension.

Recommendations

  • Use Composer and optimize automatic loading composer dump-autoload --optimize
  • Install cURL 7.16.2 or later version
  • Use OPCache
  • In a production environment, do not use Xdebug

Installation

  1. Download and install Composer(Windows user please download and run Composer-Setup.exe)
curl -sS https://getcomposer.org/installer | php
  1. Execute the Composer command, install the newest and stable version of Alibaba Cloud SDK for PHP
php -d memory_limit=-1 composer.phar require alibabacloud/sdk
  1. Require the Composer auto-loading tool
<?php

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

Compatible with old grammar

The new kernel still supports the old syntax, but the @deprecated has been flagged to highlight the IDE. Developers are strongly encouraged to use the new syntax, and we will completely remove the old syntax in a future release.

<?php

use AlibabaCloud\Client\DefaultAcsClient;
use AlibabaCloud\Client\Profile\DefaultProfile;
use AlibabaCloud\Ecs\V20140526\DescribeRegions;
use AlibabaCloud\Client\Exception\ClientException;

$profile = DefaultProfile::getProfile('<region>', '<accessKeyId>', '<accessKeySecret>');
$client  = new DefaultAcsClient($profile);
$request = new DescribeRegions();

try {
    $result = $client->getAcsResponse($request);
    print_r($result->Regions);
    print_r($result['Regions']);
    print_r($result->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage(). PHP_EOL;
}

Recommend new grammar

Before request, please Understanding the usage of the client, after request, please Understanding the result object.

Currently only some Alibaba Cloud products are supported. For products that are not supported, you can use Alibaba Cloud Client for PHP to initiate custom requests, and you can use API Explorer to generate Alibaba Cloud Client for PHP code online.

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Set up a global client
AlibabaCloud::accessKeyClient('foo', 'bar')
            ->regionId('cn-hangzhou')
            ->asGlobalClient();

try {
    // Access product APIs
    $request = AlibabaCloud::ecs()->v20140526()->describeRegions();
    
    // Set options/parameters and execute request
    $result = $request->withResourceType('type') // API parameter
                      ->withInstanceChargeType('type') // API parameter
                      ->client('client1') // Specify the client for send
                      ->debug(true) // Enable the debug will output detailed information
                      ->connectTimeout(0.01) // Throw an exception when Connection timeout 
                      ->timeout(0.01) // Throw an exception when timeout 
                      ->request(); // Execution request

    // Can also Set by passing in an array
    $options = [
                   'debug'           => true,
                   'connect_timeout' => 0.01,
                   'timeout'         => 0.01,
                   'query'           => [
                       'ResourceType' => 'type',
                       'InstanceChargeType' => 'type',
                   ],
               ];
    
    // Settings priority
    $result2 = AlibabaCloud::ecs()
                           ->v20140526()
                           ->describeRegions($options)
                           ->options([
                                         'query' => [
                                             'Key'      => 'I will overwrite this value in constructor',
                                             'new'      => 'I am new value',
                                         ],
                                     ])
                           ->options([
                                         'query' => [
                                             'Key' => 'I will overwrite the previous value',
                                             'bar' => 'I am new value',
                                         ],
                                     ])
                           ->debug(false) // Overwrite the true of the former
                           ->request();
    
} 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;
}

References