本文简要介绍了PHP SDK的安装方法,并提供了示例代码。

背景信息

  • OpenAPI开发者门户提供在线调试API和动态生成SDK示例代码的功能,能显著降低API的使用难度,推荐您使用。
  • 关于云监控API详情,请参见API概览

PHP SDK安装

关于PHP SDK的安装方法。具体操作,请参见快速开始

PHP SDK安装包下载地址如下:

PHP SDK示例

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md

AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
                        ->regionId('cn-beijing')
                        ->asDefaultClient();

try {
    $result = AlibabaCloud::rpc()
                          ->product('Cms')
                          // ->scheme('https') // https | http
                          ->version('2019-01-01')
                          ->action('DescribeMetricList')
                          ->method('POST')
                          ->options([
                                        'query' => [
                                          'RegionId' => 'cn-beijing',
                                          'MetricName' => 'cpu_idle',
                                          'Namespace' => 'acs_ecs_dashboard',
                                          'Period' => '60',
                                          'StartTime' => '2019-05-13 10:20:27',
                                          'EndTime' => '2019-05-13 11:06:27',
                                          'Dimensions' => '{\"instanceId\":<instanceId_as_example>}',
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}