本文简要介绍了PHP SDK的安装方法,并提供了示例代码。
背景信息
- OpenAPI开发者门户提供在线调试API和动态生成SDK示例代码的功能,能显著降低API的使用难度,推荐您使用。
- 关于云监控API详情,请参见API概览。
PHP SDK安装
PHP SDK安装包下载地址如下:
PHP SDK示例
下面为您提供云监控API DescribeMetricList的PHP SDK示例代码。关于其他API,请访问OpenAPI开发者门户调试并获取示例代码。
关于如何设置DescribeMetricList的请求参数,请参见DescribeMetricList。
<?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
//构建一个阿里云客户端,用于发起请求。
//构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
->regionId('cn-hangzhou')
->asDefaultClient();
#设置请求参数。
try {
$result = AlibabaCloud::rpc()
->product('Cms')
// ->scheme('https') // https | http
->version('2019-01-01')
->action('DescribeMetricList')
->method('POST')
->host('metrics.aliyuncs.com')
->options([
'query' => [
'RegionId' => "cn-hangzhou",
'MetricName' => "cpu_total",
'Namespace' => "acs_ecs_dashboard",
'StartTime' => "1628055731050",
'EndTime' => "1628062931050",
'Dimensions' => "{\"instanceId\":\"i-0xii2bvf42iqvxbp****\"}",
'Length' => "10",
],
])
->request();
print_r($result->toArray());
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}