Sms.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php namespace App\Servers\Tencent;
  2. // 导入 SMS 的 client
  3. use TencentCloud\Sms\V20190711\SmsClient;
  4. // 导入 SMS 的 client
  5. // 导入要请求接口对应的 Request 类
  6. use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
  7. use TencentCloud\Common\Exception\TencentCloudSDKException;
  8. use TencentCloud\Common\Credential;
  9. /**
  10. * 验证码模型
  11. *
  12. * @author 刘相欣
  13. */
  14. class Sms{
  15. /**
  16. * 生成邮件验证码
  17. *
  18. * @param [String] 验证码 验证码
  19. * @param [Int] $expire_time 过期时间,600秒
  20. * @return [Int] $client_id 结果ID
  21. *
  22. * */
  23. public function send($phone,$code=''){
  24. // 腾讯云账户密钥对
  25. $secretId = config('tencent.secretId','');
  26. $secretKey = config('tencent.secretKey','');
  27. // 调用地域
  28. $region = config('tencent.sms.region','');
  29. // SDKappid
  30. $sdkAppid = config('tencent.sms.sdkAppid','');
  31. // 模板ID
  32. $templateID = config('tencent.sms.templateID','');
  33. // 签名
  34. $sign = config('tencent.sms.sign','');
  35. // 尝试执行
  36. try {
  37. // 入参需要传入腾讯云账户密钥对 secretId 和 secretKey
  38. $cred = new Credential($secretId, $secretKey);
  39. // 实例化 SMS 的 client 对象,clientProfile 是可选的
  40. $client = new SmsClient($cred, $region);
  41. // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个 request 对象。
  42. $req = new SendSmsRequest();
  43. /* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666 */
  44. $req->SmsSdkAppid = $sdkAppid;
  45. /* 模板 ID: 必须填写已审核通过的模板 ID。可登录 [短信控制台] 查看模板 ID */
  46. $req->TemplateID = $templateID;
  47. /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 */
  48. $req->Sign = $sign;
  49. /* 模板参数: 若无模板参数,则设置为空*/
  50. $req->TemplateParamSet = [$code];
  51. /* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
  52. * 例如+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
  53. $req->PhoneNumberSet = ['+86'.$phone];
  54. // 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
  55. $resp = $client->SendSms($req);
  56. // 发送成功
  57. return ['success'=>$resp->SendStatusSet];
  58. }catch(TencentCloudSDKException $e) {
  59. return ['error'=>$e->getMessage()];
  60. }
  61. }
  62. }