码迷,mamicode.com
首页 > 其他好文 > 详细

腾讯音视频 TRTC

时间:2020-04-21 00:02:15      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:http   return   ace   scribe   sha   php_eol   pre   limit   cal   

<?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;
}

}

腾讯音视频 TRTC

标签:http   return   ace   scribe   sha   php_eol   pre   limit   cal   

原文地址:https://blog.51cto.com/13346331/2488855

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!