写一个php调用腾讯云RESTful API的示例程序
时间: 2024-06-02 22:14:05 浏览: 92
<?php
// 配置参数
$secretId = 'your_secret_id';
$secretKey = 'your_secret_key';
$region = 'ap-guangzhou';
$action = 'DescribeInstances';
$params = array(
'Version' => '2017-03-12',
'Limit' => 20,
'Offset' => 0,
);
// 生成签名
$timestamp = time();
$paramsStr = http_build_query($params);
$signStr = 'GETcvm.tencentcloudapi.com/?' . $paramsStr;
$sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
$auth = 'TC3-HMAC-SHA256 Credential=' . $secretId . '/' . date('Y-m-d') . '/cvm/tc3_request, SignedHeaders=content-type;host, Signature=' . $sign;
// 发送请求
$url = 'https://cvm.tencentcloudapi.com/?' . $paramsStr;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ' . $auth,
'Content-Type: application/json',
'Host: cvm.tencentcloudapi.com',
'X-TC-Action: ' . $action,
'X-TC-Region: ' . $region,
'X-TC-Timestamp: ' . $timestamp,
));
$response = curl_exec($ch);
curl_close($ch);
// 输出响应
echo $response;
?>
阅读全文