This topic describes how to initialize an SDK for PHP client and use the client to initiate API requests.
Initialize an SDK client
In SDK for PHP V1.0, SDKs of all Alibaba Cloud services share the same core library. You can use the method provided in the core library to initialize an SDK client to process API requests for accessing Alibaba Cloud services. The following sample code provides an example on how to initialize a client in SDK for PHP V1.0.
<?php
use AlibabaCloud\Client\AlibabaCloud;
// Create a client and configure parameters by using method chaining.
AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'))
->regionId('cn-hangzhou') // Specify the region in which the client resides. This setting takes effect on the requests that are sent by using the client and have no independent settings.
->timeout(1) // Set the timeout period to 1 second. This setting takes effect on the requests that are sent by using the client and have no independent settings.
->connectTimeout(0.1) // Set the timeout period to 10 milliseconds. If the value is smaller than 1, the value in seconds is automatically converted to a value in milliseconds. This setting takes effect on the requests that are sent by using the client and have no independent settings.
->debug(true) // Enable debugging. The detailed information is exported to a CLI. This setting takes effect on the requests that are sent by using the client and have no independent settings.
->name('client1');
// Specify the ID of the default region. If you do not specify a region ID for the client or for a request that is sent by using the client, the ID of the default region is used.
AlibabaCloud::setDefaultRegionId('cn-hangzhou');
// Query the ID of the default region.
AlibabaCloud::getDefaultRegionId();
// Query all clients.
AlibabaCloud::all();
// Query a specific client. If the client does not exist, an exception is reported.
AlibabaCloud::get('client1');
// Query the AccessKey pair of a specific client.
AlibabaCloud::get('client1')->getCredential()->getAccessKeyId();
// Rename a specific client.
AlibabaCloud::get('client1')->name('otherName');
// Query the ID of the region in which the default client resides.
AlibabaCloud::getDefaultClient()->regionId;
// Check whether a specific client exists.
AlibabaCloud::has('client1');
// Delete a client.
AlibabaCloud::del('client1');
// Clear all client settings.
AlibabaCloud::flush();
// Create a client based on the default configuration file. If the file does not exist, the client is created based on other configurations. If a parsing error occurs, an exception is reported.
AlibabaCloud::load();
// Specify a configuration file to create a client. If the file does not exist or a parsing error occurs, an exception is reported.
AlibabaCloud::load('your/path/file', 'vfs://AlibabaCloud/credentials', '...');
// Query the AccessKey pair or an STS token of a client. If the client uses the AccessKey pair or an STS token, the AccessKey pair or an STS token is returned.
AlibabaCloud::ecsRamRoleClient('role')->getSessionCredential();
// Query the AccessKey pair or an STS token of a specific client. If the client uses the AccessKey pair or an STS token, the AccessKey pair or an STS token is returned.
AlibabaCloud::get('client1')->getSessionCredential();If one SDK client processes multiple threads, security issues may occur. If multiple Alibaba Cloud services share the same profile information, unauthorized access may occur. We recommend that you use SDK for PHP V2.0 for development.
Initiate API requests by using an SDK client
SDK for PHP V1.0 supports generic and specialized calls. For more information, see Generic calls and specialized calls.
Make generic calls
To make generic calls, you can use the core library of SDK for PHP without the need to install SDKs of Alibaba Cloud services. For more information, see Use CommonRequest.
Make specialized calls
To make specialized calls, you must install SDKs of Alibaba Cloud services. To install SDKs of Alibaba Cloud services, perform the following steps:
Go to the SDK Center.
In the top navigation bar, move the pointer over Cloud Products and choose Computing > Elastic Compute Service.
On the page that appears, select an API version. If multiple versions are available, select the recommended or latest version.
Select V1.0 from the SDK Generation drop-down list.
Select PHP as the language.
Copy the command from the Installation Method section, paste the command to the terminal, and then run the command.
# ECS V1.0 SDK
composer require alibabacloud/ecs 1.8.883
composer require alibabacloud/client
Sample request
<?php
require_once 'vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Ecs\Ecs;
try {
// Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
$request = Ecs::v20140526()->describeRegions();
$result = $request
->regionId('cn-hangzhou') // Specify the region ID.
->host("ecs.cn-hangzhou.aliyuncs.com") // Specify the endpoint of Elastic Compute Service (ECS).
->request();
print_r($result->toArray());
} catch (ClientException $exception) {
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only.
echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only.
echo $exception->getMessage() . PHP_EOL;
echo $exception->getErrorCode() . PHP_EOL;
echo $exception->getRequestId() . PHP_EOL;
echo $exception->getErrorMessage() . PHP_EOL;
}