SMS.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // 导入对应产品模块的client
  3. use TencentCloud\Sms\V20210111\SmsClient;
  4. // 导入要请求接口对应的Request类
  5. use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
  6. use TencentCloud\Common\Exception\TencentCloudSDKException;
  7. use TencentCloud\Common\Credential;
  8. // 导入可选配置类
  9. use TencentCloud\Common\Profile\ClientProfile;
  10. use TencentCloud\Common\Profile\HttpProfile;
  11. use Logger;
  12. /**
  13. * 腾讯短信
  14. */
  15. class SMS
  16. {
  17. private $_SecretId;
  18. private $_SecretKey;
  19. private $_appId;
  20. private $_appKey;
  21. private static ?SMS $_instance = null;
  22. private function __construct()
  23. {
  24. $tx_params = \Yii::app()->params['tencent'];
  25. $this->_SecretId = $tx_params['SecretId'];
  26. $this->_SecretKey = $tx_params['SecretKey'];
  27. $this->_appId = $tx_params['sms']['appId'];
  28. $this->_appKey = $tx_params['sms']['appKey'];
  29. }
  30. public static function getInstance(): SMS
  31. {
  32. if (self::$_instance === null) {
  33. self::$_instance = new self();
  34. }
  35. return self::$_instance;
  36. }
  37. /**
  38. * 发送短信
  39. *
  40. * @param string $phone 手机号
  41. * @param string $templateId 模板id
  42. * @param array $params 模板id的参数
  43. * @param string $sign 签名
  44. * @return array
  45. */
  46. public function send($phone, $templateId, $params = [], $sign = '乐外卖')
  47. {
  48. try {
  49. /* 必要步骤:
  50. * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
  51. * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
  52. * 您也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
  53. * 以免泄露密钥对危及您的财产安全。
  54. * SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi */
  55. $cred = new Credential($this->_SecretId, $this->_SecretKey);
  56. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  57. $httpProfile = new HttpProfile();
  58. // 配置代理(无需要直接忽略)
  59. // $httpProfile->setProxy("https://ip:port");
  60. $httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
  61. $httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
  62. $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
  63. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  64. $clientProfile = new ClientProfile();
  65. $clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
  66. $clientProfile->setHttpProfile($httpProfile);
  67. // 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的
  68. // 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
  69. $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
  70. // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。
  71. $req = new SendSmsRequest();
  72. /* 填充请求参数,这里request对象的成员变量即对应接口的入参
  73. * 您可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
  74. * 基本类型的设置:
  75. * 帮助链接:
  76. * 短信控制台: https://console.cloud.tencent.com/smsv2
  77. * 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */
  78. /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
  79. // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
  80. $req->SmsSdkAppId = $this->_appId;
  81. /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
  82. // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
  83. $req->SignName = $sign;
  84. /* 模板 ID: 必须填写已审核通过的模板 ID */
  85. // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
  86. $req->TemplateId = $templateId;
  87. /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空*/
  88. $req->TemplateParamSet = $params;
  89. /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
  90. * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
  91. $req->PhoneNumberSet = array("+86".$phone);
  92. /* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
  93. $req->SessionContext = "";
  94. /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
  95. $req->ExtendCode = "";
  96. /* 国内短信无需填写该项;国际/港澳台短信已申请独立 SenderId 需要填写该字段,默认使用公共 SenderId,无需填写该字段。注:月度使用量达到指定量级可申请独立 SenderId 使用,详情请联系 [腾讯云短信小助手](https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81)。*/
  97. $req->SenderId = "";
  98. // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
  99. // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
  100. $resp = $client->SendSms($req);
  101. // 输出json格式的字符串回包
  102. $resp_msg = $resp->toJsonString();
  103. Logger::errorMult('发送腾讯云短信结果', $phone, $resp_msg);
  104. $resp_msg = json_decode($resp_msg, true);
  105. $ret_data = $resp_msg['SendStatusSet'] ?? [];
  106. if (!empty($ret_data) && !empty($ret_data[0])) {
  107. $ret_data1 = $ret_data[0];
  108. if ($ret_data1['Code'] != 'Ok') {
  109. throw new \Exception($ret_data1['Message'] ?? '发送腾讯云短信失败2');
  110. }
  111. } else {
  112. throw new \Exception('发送腾讯云短信失败1');
  113. }
  114. // 也可以取出单个值。
  115. // 您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
  116. // print_r($resp->TotalCount);
  117. /* 当出现以下错误码时,快速解决方案参考
  118. * [FailedOperation.SignatureIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
  119. * [FailedOperation.TemplateIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
  120. * [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunauthorizedoperation.smssdkappidverifyfail-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
  121. * [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
  122. * 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
  123. */
  124. }
  125. catch(TencentCloudSDKException $e) {
  126. return Helper::commonError('发送短信异常: ' . $e->getErrorCode());
  127. }
  128. catch (\Throwable $e) {
  129. return Helper::commonError('发送短信异常: ' . $e->getErrorCode());
  130. }
  131. return Helper::commonReturn();
  132. }
  133. }