Alibaba Cloud SDK for Java V2.0 supports generic API calls. This topic describes how to make generic calls by using Alibaba Cloud SDK for Java V2.0.
Characteristic
Lightweight: You can use Alibaba Cloud SDK V2.0 for PHP to call API operations by installing only the core library of Alibaba Cloud SDK, without the need to install the SDK of each service.
Easy to use: You only need to create common request parameters and use a common client to initiate requests. The responses are returned in common formats.
For more information, see Generic calls and specialized calls.
Usage notes
Before you make a generic call, we recommend that you view the metadata of the API operation to obtain the API style, request parameters, and URL.
Install the core library of Alibaba Cloud SDK
Run the following command on your terminal to install the core library of Alibaba Cloud SDK V2.0 for PHP:
composer require alibabacloud/darabonba-openapiCall an API operation
Initialize a request client
Create the OpenApiClient object to initialize a request client, and use the client to call the API operation. When you initialize a client, you can also use the Credentials tool. For more information about the Credentials tool, see Manage access credentials.
// getenv indicates that the AccessKey pair is obtained from environment variables. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
$config = new Config([
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
// Specify the endpoint of the service. In this example, the endpoint of Elastic Compute Service (ECS) in the China (Hangzhou) region is used.
$config->endpoint = "ecs-cn-hangzhou.aliyuncs.com";
// $config->protocol = "HTTPS"; // Specify HTTPS as the protocol.
$client = new OpenApiClient($config);
// Use the default credential to initialize the credential client.
// $credentialClient = new Credential();
// $Config = new Config([
// Configure the credential.
// 'credential' => $credentialClient,
// // The domain name of the service.
// 'endpoint' => 'ecs-cn-hangzhou.aliyuncs.com'
// ]);
// $client = new OpenApiClient($Config);Configure the information about the API operation
Use Params to configure the basic information about the API operation, such as the API style, API version, and request method.
$params = new Params([
"action" => "DescribeInstanceTypeFamilies", // The API operation.
"version" => "2014-05-26", // The version number of the operation.
"protocol" => "HTTPS", // The protocol of the operation.
"method" => "POST", // The HTTP method of the operation.
"authType" => "AK", // The authentication method.
"pathname" => "/", // The URL of the operation.
"reqBodyType" => "json", // The format of the request body.
"bodyType" => "json",// The request body of the JSON type.
"style" => "RPC" // The API style, such as remote procedure call (RPC) and resource-oriented architecture (ROA).
]);Configure request parameters
Use OpenApiRequest to configure the request parameters. You can pass the request parameters in a query string, a body, or a stream. Select a method to pass request parameters based on the metadata of the API operation. For example, the RegionId request parameter of the DescribeInstanceTypeFamilies API operation is defined as {"name":"RegionId","in":"query",...}} in the metadata. "in":"query" indicates that the RegionId parameter is passed in a query string.
How the parameter is passed | Description |
query | If the metadata defines |
body | If the metadata defines |
stream | If you need to upload files, you can pass file streams by configuring the Stream parameter. |
// Scenario 1: Configure a query string.
$query = ["RegionId" => "cn-hangzhou"];
$request = new OpenApiRequest([
"query" => OpenApiUtilClient::query($query),
]);
// Scenario 2: Configure the body and set reqBodyType to json.
// $body = [
// "param1" => "value1"
// ];
// $request = new OpenApiRequest([
// "body" => OpenApiUtilClient::query($body)
// ]);
// Scenario 3: Configure the Stream parameter to pass file streams
// $request = new OpenApiRequest([
// "stream" => "<FILE_STREAM>", // The file stream that you want to pass.
// ]);
// Scenario 4: Configure a request body and set reqBodyType to formData.
// $formData = [
// "param1" => "value1",
// ];
// $request = new OpenApiRequest([
// "body" => $formData,
// ]);
Initiate a request
Use OpenApiClient to call the callApi method to initiate a request. When you call an API operation, you can specify runtime parameters, such as timeout parameters and proxy parameters. For more information, see Advanced settings.
// Create a RuntimeOptions instance. You can configure runtime parameters in the RuntimeOptions instance, such as the timeout period.
$runtime = new RuntimeOptions([]);
// $runtime -> ignoreSSL = true; // A value of true specifies to disable certificate verification. A value of false specifies to enable certificate verification.
// $runtime -> httpProxy = "http://127.0.0.1:8080"; // The proxy settings.
// $runtime -> httpsProxy = "https://username:password@proxyServer:port";
// $runtime -> noProxy = "127.0.0.1,localhost";
// The response is of the MAP type, which contains the response body, response headers, and HTTP status code.
$response = $client->callApi($params, $request, $runtime);
var_dump($response);Sample code
Call an RPC 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\Credentials\Credential;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\OpenApiRequest;
use Darabonba\OpenApi\Models\Params;
use Darabonba\OpenApi\OpenApiClient;
class Sample
{
public static function main()
{
// Obtain the AccessKey pair from environment variables. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
$config = new Config([
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
// Specify the endpoint of the service. In this example, the endpoint of Elastic Compute Service (ECS) in the China (Hangzhou) region is used.
$config->endpoint = "ecs-cn-hangzhou.aliyuncs.com";
$client = new OpenApiClient($config);
$params = new Params([
"action" => "DescribeInstanceTypeFamilies", // The API operation.
"version" => "2014-05-26", // The version number of the operation.
"protocol" => "HTTPS", // The protocol of the operation.
"method" => "POST", // The HTTP method of the operation.
"authType" => "AK", // The authentication method.
"style" => "RPC", // The API style.
"pathname" => "/", // The URL of the operation.
"reqBodyType" => "json", // The format of the request body.
"bodyType" => "json",// The request body of the JSON type.
]);
// Configure the query parameters.
$query = ["RegionId" => "cn-hangzhou"];
$request = new OpenApiRequest([
"query" => OpenApiUtilClient::query($query),
]);
// Create a RuntimeOptions instance. You can configure runtime parameters in the RuntimeOptions instance, such as the timeout period.
$runtime = new RuntimeOptions([]);
// The response is of the MAP type, which contains the response body, response headers, and HTTP status code.
$response = $client->callApi($params, $request, $runtime);
var_dump($response);
}
}
Sample::main();Call a RESTful API operation
In this example, the DescribeClustersV1 operation of Container Service for Kubernetes (ACK) is called to show how to make a generic call of an operation.
<?php
namespace AlibabaCloud\SDK\Sample;
require_once 'vendor/autoload.php';
use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;
class Sample
{
public static function main()
{
$config = new Config([
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
$config->endpoint = "cs.cn-qingdao.aliyuncs.com";
$client = new OpenApiClient($config);
$params = new Params([
// The name of the API operation.
"action" => "DescribeClustersV1",
// The version number of the API operation.
"version" => "2015-12-15",
// The protocol of the API operation.
"protocol" => "HTTPS",
// The HTTP method of the API operation.
"method" => "GET",
"authType" => "AK",
"style" => "ROA",
// The path of the API operation.
"pathname" => "/api/v1/clusters",
// The format of the request body.
"reqBodyType" => "json",
// The format of the response body.
"bodyType" => "json"
]);
// query params
$queries = [
"name" => "cluster-demo"
];
$request = new OpenApiRequest([
"query" => OpenApiUtilClient::query($queries)
]);
// runtime options
$runtime = new RuntimeOptions([]);
// The response is of the MAP type, which contains the response body, response headers, and HTTP status code.
$response = $client->callApi($params, $request, $runtime);
var_dump($response);
}
}
Sample::main();