Client initialization and request methods
Initialize a client and make OpenAPI requests by using the PHP SDK.
SDK client initialization
The V1.0 SDK uses a shared SDK Core for all products. You initialize a client object through the SDK Core to handle requests. The following example shows how to initialize a V1.0 SDK client:
<?php
use AlibabaCloud\Client\AlibabaCloud;
// Create a client and chain other options.
AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'))
->regionId('cn-hangzhou') // Set the client region. This region is used for all requests that use this client, unless a region is specified for a request.
->timeout(1) // Set the timeout to 1 second. This timeout is used for all requests that use this client, unless a timeout is specified for a request.
->connectTimeout(0.1) // Configure the connection timeout. Set to 10 milliseconds. If the value is less than 1, it is converted to milliseconds. This timeout is used for all requests that use this client, unless a connection timeout is specified for a request.
->debug(true) // Enable debugging. This prints detailed information in the command-line interface (CLI). This setting is used for all requests that use this client, unless debugging is configured for a request.
->name('client1');
// Set the default region. The default region is used if no region is set for a request or its client.
AlibabaCloud::setDefaultRegionId('cn-hangzhou');
// Get the default region.
AlibabaCloud::getDefaultRegionId();
// Get all clients.
AlibabaCloud::all();
// Get a specific client. An exception is thrown if the client does not exist.
AlibabaCloud::get('client1');
// Get the AccessKey of a specific client.
AlibabaCloud::get('client1')->getCredential()->getAccessKeyId();
// Give a new name to a specific client.
AlibabaCloud::get('client1')->name('otherName');
// Get the region of the default client, and so on.
AlibabaCloud::getDefaultClient()->regionId;
// Check if a client with the specified name exists.
AlibabaCloud::has('client1');
// Delete a client.
AlibabaCloud::del('client1');
// Clear all client configurations.
AlibabaCloud::flush();
// Create a client from the default configuration file. This step is skipped if the file does not exist. An exception is thrown if the file fails to parse.
AlibabaCloud::load();
// Create a client from a specified configuration file. An exception is thrown if the file does not exist or fails to parse.
AlibabaCloud::load('your/path/file', 'vfs://AlibabaCloud/credentials', '...');
// Get the AccessKey or STS access credential for a client. If the client already has the credential, the credential is returned.
AlibabaCloud::ecsRamRoleClient('role')->getSessionCredential();
// Get the AccessKey or STS access credential for a specific client. If the client already has the credential, the credential is returned.
AlibabaCloud::get('client1')->getSessionCredential();
The single-client pattern is not thread-safe. Using one profile for different products can also cause permission management issues. We recommend that you use the V2.0 SDK for development.
OpenAPI request methods
The PHP V1.0 SDK supports generalized calls (using CommonRequest) and specialized calls. For more information, see Generalized and specialized calls.
Make calls using CommonRequest
With the core package alone, you can make OpenAPI requests directly through the SDK Core without installing individual product SDKs. For more information, see Generalized calls.
Make specialized calls using a cloud product SDK
You must install the cloud product SDK. To obtain the SDK:
-
Go to SDK Center.
-
In the top menu bar, Select a Cloud Product, such as Elastic Compute Service.
-
Select an API Version. If the product has multiple versions, select the recommended or latest version.
-
Select an SDK Generation, and then select V1.0.
-
From All Languages, select PHP.
-
In the Installation Method section, copy the command and run it in your terminal.
# ECS V1.0 SDK
composer require alibabacloud/ecs 1.8.883
composer require alibabacloud/client
Request example
<?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 set.
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 regionId.
->host("ecs.cn-hangzhou.aliyuncs.com") // Specify the endpoint.
->request();
print_r($result->toArray());
} catch (ClientException $exception) {
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
// This is for demonstration only. Handle exceptions with care. Do not ignore exceptions in your project.
echo $exception->getMessage() . PHP_EOL;
echo $exception->getErrorCode() . PHP_EOL;
echo $exception->getRequestId() . PHP_EOL;
echo $exception->getErrorMessage() . PHP_EOL;
}