12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?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] 验证码 验证码
- * @param [Int] $expire_time 过期时间,600秒
- * @return [Int] $client_id 结果ID
- *
- * */
- public function send($phone,$code=''){
- // 腾讯云账户密钥对
- $secretId = config('tencent.secretId','');
- $secretKey = config('tencent.secretKey','');
- // 调用地域
- $region = config('tencent.sms.region','');
- // SDKappid
- $sdkAppid = config('tencent.sms.sdkAppid','');
- // 模板ID
- $templateID = config('tencent.sms.templateID','');
- // 签名
- $sign = config('tencent.sms.sign','');
- // 尝试执行
- 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 = $templateID;
- /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 */
- $req->Sign = $sign;
- /* 模板参数: 若无模板参数,则设置为空*/
- $req->TemplateParamSet = [$code];
- /* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
- * 例如+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
- $req->PhoneNumberSet = ['+86'.$phone];
- // 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
- $resp = $client->SendSms($req);
- // 发送成功
- return ['success'=>$resp->SendStatusSet];
- }catch(TencentCloudSDKException $e) {
- return ['error'=>$e->getMessage()];
- }
- }
- }
|