1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| <?php
class QcloudSimple { private $SecretId; private $SecretKey; private $endpoint = "sms.tencentcloudapi.com"; private $service = "sms"; private $version = "2021-01-11"; private $region = "ap-guangzhou";
public function __construct($SecretId,$SecretKey) { $this->SecretId = $SecretId; $this->SecretKey = $SecretKey; }
public function SendSms($PhoneNumber, $AppId, $TemplateId, $TemplateParam = null, $SignName = null, $ExtendCode = null, $SessionContext = null, $SenderId = null) { $action = 'SendSms'; $param = ['PhoneNumberSet' => $PhoneNumber, 'SmsSdkAppId' => $AppId, 'TemplateId' => $TemplateId]; if(!is_array($PhoneNumber)) { $param['PhoneNumberSet'] = [$PhoneNumber]; } if(!empty($TemplateParam) || trim($TemplateParam) !== '') { $param['TemplateParamSet'] = $TemplateParam; } if(!empty($SignName) || trim($SignName) !== '') { $param['SignName'] = $SignName; } if(!empty($ExtendCode) || trim($ExtendCode) !== '') { $param['ExtendCode'] = $ExtendCode; } if(!empty($SessionContext) || trim($SessionContext) !== '') { $param['SessionContext'] = $SessionContext; } if(!empty($SenderId) || trim($SenderId) !== '') { $param['SenderId'] = $SenderId; } return $this->send_request($action, $param); } private function send_request($action, $param) { $payload = json_encode($param); $time = time(); $authorization = $this->generateSign($payload, $time); $header = ['Authorization: ' . $authorization, 'Content-Type: application/json; charset=utf-8', 'X-TC-Action: ' . $action, 'X-TC-Timestamp: ' . $time, 'X-TC-Version: ' . $this->version, 'X-TC-Region: ' . $this->region]; return $this->curl_post($payload, $header); } private function generateSign($payload, $time) { $algorithm = "TC3-HMAC-SHA256"; $httpRequestMethod = "POST"; $canonicalUri = "/"; $canonicalQueryString = ""; $canonicalHeaders = "content-type:application/json; charset=utf-8\n" . "host:" . $this->endpoint . "\n"; $signedHeaders = "content-type;host"; $hashedRequestPayload = hash("SHA256", $payload); $canonicalRequest = $httpRequestMethod . "\n" . $canonicalUri . "\n" . $canonicalQueryString . "\n" . $canonicalHeaders . "\n" . $signedHeaders . "\n" . $hashedRequestPayload; $date = gmdate("Y-m-d", $time); $credentialScope = $date . "/" . $this->service . "/tc3_request"; $hashedCanonicalRequest = hash("SHA256", $canonicalRequest); $stringToSign = $algorithm . "\n" . $time . "\n" . $credentialScope . "\n" . $hashedCanonicalRequest; $secretDate = hash_hmac("SHA256", $date, "TC3" . $this->SecretKey, true); $secretService = hash_hmac("SHA256", $this->service, $secretDate, true); $secretSigning = hash_hmac("SHA256", "tc3_request", $secretService, true); $signature = hash_hmac("SHA256", $stringToSign, $secretSigning); $authorization = $algorithm . " Credential=" . $this->SecretId . "/" . $credentialScope . ", SignedHeaders=content-type;host, Signature=" . $signature; return $authorization; } private function curl_post($payload, $header) { $url = 'https://' . $this->endpoint . '/'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); $json = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode == 200) { $arr = json_decode($json, true); return $arr['Response']; } else { return false; } } }
|