Alibaba Cloud SDK V1.0 for PHP supports generic API calls. This topic describes how to make generic calls by using Alibaba Cloud SDK V1.0 for PHP.
Characteristics
Lightweight: You can use Alibaba Cloud SDK V1.0 for PHP to call all API operations by installing only the core library of Alibaba Cloud SDK, without the need to install the SDK of each service.
Fast iteration and compatibility: If a cloud service does not provide an SDK, or the SDK is not updated for the latest API operations, you can make generic calls. This way, you can call the latest API operations without the need to wait for SDK updates.
For more information, see Generic calls and specialized calls.
Usage notes
Before you make a generic call, manually obtain and specify the required metadata, including the API version, request URL, and parameter type. For more information, see API metadata.
Install the core library of Alibaba Cloud SDK V1.0 for PHP
Run the following command on your terminal to install the core library of Alibaba Cloud SDK V1.0 for PHP:
composer require alibabacloud/client
Call an API operation
Initialize a request client
In the AlibabaCloud
package, create a client
module to initialize the request client, and use the client
to call API operations
. In this example, an AccessKey pair
is used to initialize the request client. For more information, see Manage access credentials.
To prevent AccessKey leaks, you can record the AccessKey pair in environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.
use AlibabaCloud\Client\AlibabaCloud;
// getenv indicates that the AccessKey pair obtained from environment variables is used to initialize the client.
AlibabaCloud::accessKeyClient(
getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),
getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET')
)
->regionId('cn-hangzhou') // Specify the region ID.
->asDefaultClient(); // Specify the client as the default client.
Configure the API operation information and request parameters
Use the client
module in the AlibabaCloud
package to configure the basic information about and request parameters of the API operation
.
The request
module is used to convert the API metadata, such as the version number, URL, and parameter type, to a valid HTTP request through a standard request configuration process, and then return the original response data. How parameters are passed are determined by the API style and design.
// Configure the basic information about and request parameters of the API operation.
$result = AlibabaCloud::rpc() // The API operation style, such as remote procedure call (RPC) and resource-oriented architecture (ROA).
// 1.Configure the basic information about the API operation.
->regionId('cn-hangzhou') // The region ID.
->product('Ecs') // The service name.
->version('2014-05-26') // Make sure that the version number must be the same as that in the API reference.
->action('DescribeRegions') // The name of the API operation. When you call an RPC-style API operation, you must configure action() to specify the name of the API operation.
// ->pathPattern() // The URL of the API operation. When you call an ROA-style API operation, you must configure pathPattern() to specify the complete URL of the API operation.
->method('GET') // The request method, such as GET and POST.
->setProtocolType('https') // The request protocol. Valid values: HTTP and HTTPS. We recommend that you use HTTPS.
// 2.Configure the request parameters.
->options([
// Method 1: Configure a query string, and set the request method to GET or POST.
'query' => [
'InstanceChargeType' => 'PostPaid',
],
// Method 2: Configure the request body, set reqBodyType to formData, and set the request method to POST.
// 'form_params' => [ // The form parameters.
// 'InstanceChargeType' => 'PostPaid'
// ],
// 'headers' => [
// 'content-type' => 'application/x-www-form-urlencoded'
// ]
])
// Method 3: Configure the body, set reqBodyType to byte, and set the request method to GET.
// ->contentType('application/octet-stream')
// ->body(json_encode(['InstanceChargeType' => 'PostPaid']))
// Method 4: Configure the body, set reqBodyType to json, and set the request method to GET.
// ->contentType('application/json') // The parameter type.
// ->jsonBody(['InstanceChargeType' => 'PostPaid']) // Set the parameter type to json.
Initiate a request
Use the client
to initiate a request by calling the request()
function.
// Call an RPC-style API operation.
$result = AlibabaCloud::rpc()
->request()
// Call an ROA-style API operation.
// $result = AlibabaCloud::roa()
// ->request()
// If the response is of the byte type, the request ID and response parameters are returned.
print_r($result->toArray());
Sample code
Example: Call an RPC-style API operation
In this example, the DescribeRegions
operation of ECS is called to show how to make a generic call of an operation.
<?php
namespace AlibabaCloud\SDK\Sample;
require_once 'vendor/autoload.php';
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Sample
{
public static function main()
{
try {
$result = AlibabaCloud::rpc()
->regionId('cn-hangzhou')
->product('Ecs') // The service name.
->version('2014-05-26') // The API version number. Make sure that the version number must be the same as the one in the API reference.
->action('DescribeRegions') // The name of the API operation. When you call an RPC-style API operation, you must configure action() to specify the name of the API operation.
->method('GET') // The request method.
// The request parameters.
->options([
'query' => [
'InstanceChargeType' => 'PostPaid',
],
])
->request();
print_r($result->toArray());
} catch (ClientException | ServerException $e) {
echo $e->getErrorMessage();
} catch (ServerException $exception) {
print_r($exception->getErrorMessage());
}
}
}
Sample::main();
Example: Call a RESTful-style (ROA-style) API operation
The following code shows how to use CommonRequest to call the DescribeClustersV1 operation of Container Service for Kubernetes (ACK):
<?php
namespace AlibabaCloud\SDK\Sample;
require_once 'vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Sample
{
public static function main()
{
try {
$result = AlibabaCloud::roa()
->regionId('cn-hangzhou) // The region ID. If this parameter is not specified, the region of the client or the default region is used.
->product('CS') // The service name.
->version('2015-12-15 ') // The service version.
->serviceCode('cs') // The service code for addressing. This parameter is optional.
->endpointType('openAPI') // The endpoint type. This parameter is optional.
->method('GET') // The request method.
->host('cs.aliyun.com ') // The domain name. If this parameter is specified, addressing is not performed. If a service uses bearer tokens for authentication, you must specify the domain name.
->pathPattern('/api/v1/clusters') // The URL of the API operation. When you call an ROA-style API operation, you must configure pathPattern() to specify the complete URL of the API operation. You can obtain the URL of an API operation from the API metadata.
->request(); // Initiate the request and obtain the result. This parameter must be at the end of the settings.
print_r($result->toArray());
} catch (ClientException $exception) {
print_r($exception->getErrorMessage());
} catch (ServerException $exception) {
print_r($exception->getErrorMessage());
}
}
}
Sample::main();