| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php namespace App\Servers\Tencent;
- // 导入 SMS 的 client
- use TencentCloud\Sms\V20190711\SmsClient;
- // 导入 SMS 的 client
- // 导入要请求接口对应的 Request 类
- use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
- use TencentCloud\Common\Exception\TencentCloudSDKException;
- use TencentCloud\Common\Credential;
- /**
- * 验证码模型
- *
- * @author 刘相欣
- */
- class Sms{
-
- /**
- * 发送短信
- * @param string $phone 手机号。国际/港澳台消息需加国际区号。支持向不同的手机号码发送短信,手机号码之间以半角逗号(,)分隔。上限为 200 个手机号码。批量发送相对于单条发送,及时性稍有延迟。验证码类型的短信,建议单条发送。
- * @param string $signName 签名名称。
- * @param string $templateCode 模板ID。
- * @param array $param 模板参数。短信模板变量对应的实际值。支持传入多个参数, json格式,如 {"name":"张三","number":"1390000****"}
- *
- */
- public function sendSms(string $phone,string $signName,string $templateCode,array $param=[]){
- // 切割数据
- $phone = explode(',',$phone);
- // 循环手机号
- foreach ($phone as $key => $value) {
- // 组合手机号
- $phone[$key] = '+86'.trim($value);
- }
- // 参数必须是字符串
- foreach ($param as $key => $value) {
- // 组合手机号
- $param[$key] = (string) $value;
- }
- // 腾讯云账户密钥对
- $secretId = config('tencent.secretId','');
- $secretKey = config('tencent.secretKey','');
- // 调用地域
- $region = config('tencent.sms.region','');
- // SDKappid
- $sdkAppid = config('tencent.sms.sdkAppid','');
- // 尝试执行
- try {
- // 入参需要传入腾讯云账户密钥对 secretId 和 secretKey
- $cred = new Credential($secretId, $secretKey);
- // 实例化 SMS 的 client 对象,clientProfile 是可选的
- $client = new SmsClient($cred, $region);
- // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个 request 对象。
- $req = new SendSmsRequest();
- /* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666 */
- $req->SmsSdkAppid = $sdkAppid;
- /* 模板 ID: 必须填写已审核通过的模板 ID。可登录 [短信控制台] 查看模板 ID */
- $req->TemplateID = $templateCode;
- /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 */
- $req->Sign = $signName;
- /* 模板参数: 若无模板参数,则设置为空*/
- $req->TemplateParamSet = $param;
- /* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
- * 例如+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
- $req->PhoneNumberSet = $phone;//['+86'.$phone];
- // 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
- $resp = $client->SendSms($req);
- // 发送成功
- return ['success'=>$resp->SendStatusSet];
- }catch(TencentCloudSDKException $e) {
- return ['error'=>$e->getMessage()];
- }
- }
- }
|