CommonRequest.Class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * 腾讯云公共请求参数类
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2019/11/11
  7. * Time: 10:54 AM
  8. */
  9. namespace Util\TencentCloud;
  10. class CommonRequest
  11. {
  12. /**
  13. * 生成公共请求参数header头
  14. * 官方文档地址: https://cloud.tencent.com/document/api/382/38767
  15. * @param string $region 区域名称
  16. * @param string $secretId 密钥
  17. * @param string $secretKey 密钥
  18. * @param string $service 服务名称
  19. * @param array $allParams 请求接口的所有参数
  20. */
  21. static function createCommonRequestHeader($region, $secretId, $secretKey, $service, $allParams, $host)
  22. {
  23. $time = time();
  24. $signature = self::createSignature($secretId, $secretKey, $allParams, $host, $service, $time);
  25. $returnData = [];
  26. $returnData['X-TC-Region'] = $region; // 地域参数
  27. $returnData['X-TC-Timestamp'] = $time; // 当前unix时间戳
  28. $returnData['Authorization'] = $signature;
  29. return $returnData;
  30. }
  31. /**
  32. * 生成签名
  33. * 官网文档地址: https://cloud.tencent.com/document/api/382/38768
  34. */
  35. static function createSignature($secretId, $secretKey, $allParams, $host, $service, $time)
  36. {
  37. // 拼接规范请求串
  38. $canonicalRequest = [
  39. 'HTTPRequestMethod' => 'POST', // HTTP请求方法
  40. 'CanonicalURI' => '/', // URL参数
  41. 'CanonicalQueryString' => '', //HTTP 查询字符串
  42. 'CanonicalHeaders' => 'content-type:application/json'."\n".'host:'.$host."\n", // 参与签名头部信息
  43. 'SignedHeaders' => 'content-type;host', // 参与签名的头部信息
  44. 'HashedRequestPayload' => strtolower(bin2hex(hash("SHA256", json_encode($allParams, JSON_UNESCAPED_UNICODE), true))),
  45. ];
  46. $canonicalRequestStr = '';
  47. $mark = "\n";
  48. $i = 1;
  49. foreach ($canonicalRequest as $key => $value){
  50. if($i == (count($canonicalRequest)) ){
  51. $mark = '';
  52. }
  53. $canonicalRequestStr .= $value . $mark ;
  54. $i++;
  55. }
  56. // 拼接待签名字符串
  57. $credentialScope = gmdate('Y-m-d', $time).'/'.$service.'/'.'tc3_request';
  58. $hashedCanonicalRequest = strtolower(bin2hex(hash("SHA256", $canonicalRequestStr, true)));
  59. $stringToSign = [
  60. 'algorithm' => 'TC3-HMAC-SHA256',
  61. 'timestamp' => $time,
  62. 'credentialScope' => $credentialScope,
  63. 'hashedCanonicalRequest' => $hashedCanonicalRequest,
  64. ];
  65. $stringToSignStr = '';
  66. $mark = "\n";
  67. $i = 1;
  68. foreach ($stringToSign as $key => $value){
  69. if($i == (count($stringToSign)) ){
  70. $mark = '';
  71. }
  72. $stringToSignStr .= $value . $mark ;
  73. $i++;
  74. }
  75. // 计算签名
  76. $secretDate = hash_hmac('SHA256', gmdate('Y-m-d', $time), 'TC3'.$secretKey, true);
  77. $secretService = hash_hmac('SHA256', $service, $secretDate, true);
  78. $secretSigning = hash_hmac('SHA256', 'tc3_request', $secretService, true);
  79. $signture = strtolower(hash_hmac('SHA256', $stringToSignStr, $secretSigning));
  80. // authorization
  81. $authorization = 'TC3-HMAC-SHA256 Credential='.$secretId.'/'.$credentialScope.', SignedHeaders=content-type;host, Signature='.$signture;
  82. return $authorization;
  83. }
  84. }