| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- // 导入对应产品模块的client
- use TencentCloud\Sms\V20210111\SmsClient;
- // 导入要请求接口对应的Request类
- use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
- use TencentCloud\Common\Exception\TencentCloudSDKException;
- use TencentCloud\Common\Credential;
- // 导入可选配置类
- use TencentCloud\Common\Profile\ClientProfile;
- use TencentCloud\Common\Profile\HttpProfile;
- use Logger;
- /**
- * 腾讯短信
- */
- class SMS
- {
- private $_SecretId;
- private $_SecretKey;
- private $_appId;
- private $_appKey;
- private static ?SMS $_instance = null;
- private function __construct()
- {
- $tx_params = \Yii::app()->params['tencent'];
- $this->_SecretId = $tx_params['SecretId'];
- $this->_SecretKey = $tx_params['SecretKey'];
- $this->_appId = $tx_params['sms']['appId'];
- $this->_appKey = $tx_params['sms']['appKey'];
- }
- public static function getInstance(): SMS
- {
- if (self::$_instance === null) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- /**
- * 发送短信
- *
- * @param string $phone 手机号
- * @param string $templateId 模板id
- * @param array $params 模板id的参数
- * @param string $sign 签名
- * @return array
- */
- public function send($phone, $templateId, $params = [], $sign = '乐外卖')
- {
- try {
- /* 必要步骤:
- * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
- * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
- * 您也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
- * 以免泄露密钥对危及您的财产安全。
- * SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi */
- $cred = new Credential($this->_SecretId, $this->_SecretKey);
- // 实例化一个http选项,可选的,没有特殊需求可以跳过
- $httpProfile = new HttpProfile();
- // 配置代理(无需要直接忽略)
- // $httpProfile->setProxy("https://ip:port");
- $httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
- $httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
- $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
- // 实例化一个client选项,可选的,没有特殊需求可以跳过
- $clientProfile = new ClientProfile();
- $clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
- $clientProfile->setHttpProfile($httpProfile);
- // 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的
- // 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
- $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
- // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。
- $req = new SendSmsRequest();
- /* 填充请求参数,这里request对象的成员变量即对应接口的入参
- * 您可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
- * 基本类型的设置:
- * 帮助链接:
- * 短信控制台: https://console.cloud.tencent.com/smsv2
- * 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */
- /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
- // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
- $req->SmsSdkAppId = $this->_appId;
- /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
- // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
- $req->SignName = $sign;
- /* 模板 ID: 必须填写已审核通过的模板 ID */
- // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
- $req->TemplateId = $templateId;
- /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空*/
- $req->TemplateParamSet = $params;
- /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
- * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
- $req->PhoneNumberSet = array("+86".$phone);
- /* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
- $req->SessionContext = "";
- /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
- $req->ExtendCode = "";
- /* 国内短信无需填写该项;国际/港澳台短信已申请独立 SenderId 需要填写该字段,默认使用公共 SenderId,无需填写该字段。注:月度使用量达到指定量级可申请独立 SenderId 使用,详情请联系 [腾讯云短信小助手](https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81)。*/
- $req->SenderId = "";
- // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
- // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
- $resp = $client->SendSms($req);
- // 输出json格式的字符串回包
- $resp_msg = $resp->toJsonString();
- Logger::errorMult('发送腾讯云短信结果', $phone, $resp_msg);
- $resp_msg = json_decode($resp_msg, true);
- $ret_data = $resp_msg['SendStatusSet'] ?? [];
- if (!empty($ret_data) && !empty($ret_data[0])) {
- $ret_data1 = $ret_data[0];
- if ($ret_data1['Code'] != 'Ok') {
- throw new \Exception($ret_data1['Message'] ?? '发送腾讯云短信失败2');
- }
- } else {
- throw new \Exception('发送腾讯云短信失败1');
- }
- // 也可以取出单个值。
- // 您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
- // print_r($resp->TotalCount);
- /* 当出现以下错误码时,快速解决方案参考
- * [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)
- * [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)
- * [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)
- * [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)
- * 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
- */
- }
- catch(TencentCloudSDKException $e) {
- return Helper::commonError('发送短信异常: ' . $e->getErrorCode());
- }
- catch (\Throwable $e) {
- return Helper::commonError('发送短信异常: ' . $e->getErrorCode());
- }
- return Helper::commonReturn();
- }
- }
|