SDK integration
Alibaba Cloud SDK simplifies the development process, enables quick feature integration, and reduces O&M costs. Integrate Alibaba Cloud SDK V1.0 for PHP in your project to start calling API operations.
Environment requirements
PHP >= 5.5
Install SDK
When you install Alibaba Cloud SDK V1.0 for PHP, the core library is automatically installed. You only need to install the cloud product SDK.
Cloud product SDK
The V1.0 SDK of an Alibaba Cloud service provides the request and response objects of API operations and the Unmarshaller object for serializing return values. The following example adds the ECS V1.0 SDK as a dependency:
# ECS V1.0 SDK
composer require alibabacloud/ecs
The V1.0 SDKs use the naming format alibabacloud/${product_name}. You can also find information about the V1.0 SDK for a specific product in the SDK Center.
Core SDK
The core library includes the client object, signature logic, and error handling logic required for calling API operations. To make generic calls or install a specific version of the core library, run the following command:
composer require alibabacloud/client
For more information about the latest version, see alibabacloud/client - Packagist.
Use the SDK
The following example uses Alibaba Cloud SDK V1.0 to call the DescribeInstances operation of ECS.
1. Initialize a request client
All API operations are called by using the Client object from the core library. Before you call an API operation, initialize a request client. In this example, the client is initialized with an AccessKey pair. For more information, see Manage access credentials.
This example reads credentials from environment variables. Before you run the code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. For more information, see Configure environment variables on 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
Use the request object provided by the SDK to encapsulate 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 send the 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 occurs when you call an API operation, capture ServerException and ClientException to obtain the error message.
References
-
For advanced SDK settings such as proxy and timeout configuration, see Advanced settings.
-
For information about how to create an AccessKey pair, see Create an AccessKey pair.