<?php
namespace trc;
// 腾讯音视频
class trc{
private $secretId = 'AKIDyDGxubFJtGRN6EKk8rPjmYswmlsiLXXS 111';
private $secretKey = 'qQozwhFSM0NUnriKdgXCnFUTBdpoW07y';
public function __construct($secretId=null, $secretKey=null) {
if ($secretId) $this->secretId = $secretId;
if ($secretKey) $this->secretKey = $secretKey;
}
// 生成授权链接
public function get_sha(){
$host = "cvm.tencentcloudapi.com";
$service = "cvm";
$version = "2017-03-12";
$action = "DescribeInstances";
$region = "ap-guangzhou";
$timestamp = time();
$algorithm = "TC3-HMAC-SHA256";

    // step 1: 生成规范请求字符串
    $httpRequestMethod    = "POST";
    $canonicalUri         = "/";
    $canonicalQueryString = "";
    $canonicalHeaders     = "content-type:application/json; charset=utf-8\n"."host:".$host."\n";
    $signedHeaders        = "content-type;host";
    $payload              = '{"Limit": 1, "Filters": [{"Values": ["\u672a\u547d\u540d"], "Name": "instance-name"}]}';
    $hashedRequestPayload = hash("SHA256", $payload);
    $canonicalRequest     = $httpRequestMethod."\n"
        .$canonicalUri."\n"
        .$canonicalQueryString."\n"
        .$canonicalHeaders."\n"
        .$signedHeaders."\n"
        .$hashedRequestPayload;
    // echo $canonicalRequest.PHP_EOL;

    // step 2: 生成要签名的字符串
    $date                   = gmdate("Y-m-d", $timestamp);
    $credentialScope        = $date."/".$service."/tc3_request";
    $hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
    $stringToSign           = $algorithm."\n"
        .$timestamp."\n"
        .$credentialScope."\n"
        .$hashedCanonicalRequest;
    // echo $stringToSign.PHP_EOL;

    // step 3: 签名字符串
    $secretDate    = hash_hmac("SHA256", $date, "TC3".$this->secretKey, true);
    $secretService = hash_hmac("SHA256", $service, $secretDate, true);
    $secretSigning = hash_hmac("SHA256", "tc3_request", $secretService, true);
    $signature     = hash_hmac("SHA256", $stringToSign, $secretSigning);
    // echo $signature.PHP_EOL;

    // step 4: 生成授权
    $authorization = $algorithm
        ." Credential=".$this->secretId."/".$credentialScope
        .", SignedHeaders=content-type;host, Signature=".$signature;
    // echo $authorization.PHP_EOL;

    $curl = "curl -X POST https://".$host
        .' -H "Authorization: '.$authorization.'"'
        .' -H "Content-Type: application/json; charset=utf-8"'
        .' -H "Host: '.$host.'"'
        .' -H "X-TC-Action: '.$action.'"'
        .' -H "X-TC-Timestamp: '.$timestamp.'"'
        .' -H "X-TC-Version: '.$version.'"'
        .' -H "X-TC-Region: '.$region.'"'
        ." -d '".$payload."'";
    return $curl.PHP_EOL;
}

}