PHP SDK examples
This topic uses the PHP SDK V2.0 as an example to demonstrate how to call Cloud Monitor 2.0 (cms20240330) OpenAPI operations. The calling method is similar for other operations. You only need to replace the request and response types and fields.
Prerequisite
PHP 5.6 or later.
Installation
Install via Composer:
composer require alibabacloud/cms-20240330 10.0.0Source code repository: alibabacloud-sdk-php/cms-20240330.
Configure access credentials
We recommend that you use a more secure method that does not require an AccessKey pair for your project code. For more information about credential configuration, see Manage access credentials. The simplest approach is to configure environment variables:
export ALIBABA_CLOUD_ACCESS_KEY_ID="<your-access-key-id>"
export ALIBABA_CLOUD_ACCESS_KEY_SECRET="<your-access-key-secret>"AlibabaCloud\Credentials\Credential searches for credentials in the order defined by the default credential chain.
Complete code example
The following example calls the GetCmsService operation to query the activation status of a Cloud Monitor 2.0 service for a specific product:
<?php
namespace AlibabaCloud\SDK\Sample;
use AlibabaCloud\SDK\Cms\V20240330\Cms;
use AlibabaCloud\Credentials\Credential;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Cms\V20240330\Models\GetCmsServiceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class Sample {
/**
* Initialize the client using credentials.
* @return Cms Client
*/
public static function createClient(){
// For production code, we recommend using a more secure method that does not involve an AccessKey pair.
$credential = new Credential();
$config = new Config([
"credential" => $credential
]);
// For the endpoint, see https://api.aliyun.com/product/Cms.
$config->endpoint = "metrics.cn-hangzhou.aliyuncs.com";
return new Cms($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
$client = self::createClient();
$getCmsServiceRequest = new GetCmsServiceRequest([
"product" => "your_value",
"service" => "your_value"
]);
$headers = [];
try {
$resp = $client->getCmsServiceWithOptions($getCmsServiceRequest, $headers, new RuntimeOptions([]));
var_dump($resp->toMap());
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// Error message
var_dump($error->message);
// Diagnostic address
var_dump($error->data["Recommend"]);
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main(array_slice($argv, 1));Procedure
The code works as follows:
Initialize the Config object
Darabonba\OpenApi\Models\Config. The Config object stores the credential, endpoint, and other configurations. The Endpoint ismetrics.cn-hangzhou.aliyuncs.comas shown in the example.use Darabonba\OpenApi\Models\Config; use AlibabaCloud\Credentials\Credential; $credential = new Credential(); $config = new Config([ "credential" => $credential ]); $config->endpoint = "metrics.cn-hangzhou.aliyuncs.com";Instantiate the client:
use AlibabaCloud\SDK\Cms\V20240330\Cms; $client = new Cms($config);(Optional) Initialize RuntimeOptions:
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; $runtime = new RuntimeOptions(); $runtime->maxIdleConns = 3; $runtime->connectTimeout = 10000; $runtime->readTimeout = 10000;Create the Request object for the API operation:
$request = new GetCmsServiceRequest();Set request parameters:
$request->product = "your_value"; $request->service = "your_value";Call the API operation through the client:
$response = $client->getCmsService($request, $runtime); var_dump($response->toMap());Read return values from the response, for example:
var_dump($response->body->requestId);Handle errors:
catch (Exception $error) { if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } var_dump($error->message); var_dump($error->data["Recommend"]); }
For more API operations and sample code, see SDK examples on the OpenAPI portal.