To facilitate API calls, we recommend that you integrate with Alibaba Cloud SDK in your project. SDKs simplify the development process, quickly integrate features, and greatly reduce the O&M cost. This topic describes how to integrate Alibaba Cloud SDK V1.0 for PHP in your project and use the SDK for development.
Prerequisites
PHP 5.5 or later is installed.
Install Alibaba Cloud SDK V1.0 for PHP
When you install Alibaba Cloud SDK V1.0 for PHP, the core library is automatically installed. Therefore, you only need to install the SDK.
Service SDK
The SDK V1.0 of an Alibaba Cloud service provides the request and response objects of API operations and the Unmarshaller object that is used to serialize the values to be returned. The following sample code shows how to configure the SDK V1.0 of Elastic Compute Service (ECS) as a dependency:
# ECS V1.0 SDK
composer require alibabacloud/ecsThe SDK V1.0 of each Alibaba Cloud service is named in the alibabacloud/${Service name} format. For more information about the SDK V1.0 of each Alibaba Cloud service, visit SDK Center.
Core library
The core library of an SDK includes the client object, the signature logic, and the error handling logic, which are required information for calling API operations. To make generic calls or install the core library of a specific version, run the following command:
composer require alibabacloud/clientFor more information about the latest version, see alibabacloud/client - Packagist.
Use the SDK
The following example shows how to use Alibaba Cloud SDK V1.0 to call the DescribeInstances API operation of ECS.
1. Initialize a request client
All API operations are called by using the Client object provided by the core library. Before you call an API operation, you must initialize a request client. In this example, the request client is initialized by using an AccessKey pair. For more information, see Manage access credentials.
In this example, the AccessKey pair is obtained from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, which must be configured before you run the code. For more information, see Configure environment variables in Linux, macOS, and Windows.
use AlibabaCloud\Client\AlibabaCloud;
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'))
->regionId('<REGION_ID>')
->asDefaultClient();
2. Create a request object
You can use the request object provided by the SDK to encapsulate the request parameters.
use AlibabaCloud\Ecs\Ecs;
// Build the request parameters.
$request = Ecs::v20140526()
->describeInstances() // Create a request object for the API operation.
->withInstanceIds(json_encode(["i-bp1dXXXXXXXXXXXX"])) // The InstanceIds request parameter. Specify the ID of your instance.
->withPageSize(10) // The PageSize request parameter. Specify the number of entries that you want to obtain.
->withPageNumber(1); // The PageNumber request parameter. Specify the number of the page that you want to obtain.
3. Initiate an API request
Call the request() function to initiate an API request.
$result = $request
->debug(true) // Output details. If this field is set to false, details are not output.
->request(); // Execute the request.
print_r($result->toArray()); // Print the response to the API request.
4. Handle errors
If an error is triggered when you can an API operation, you can capture ServerException and ClientException to obtain the error message.
References
For more information about advanced SDK settings, such as proxy settings and timeout settings, see Advanced settings.
For more information about how to create an AccessKey pair, see Create an AccessKey pair.