引入腾讯云短信
参数获取
签名
https://console.cloud.tencent.com/smsv2/csms-sign
获取签名ID 和 签名内容创建应用
https://console.cloud.tencent.com/smsv2/app-manage
并且拿到 SDK AppID 和 App Key消息模板
https://console.cloud.tencent.com/smsv2/csms-template
创建消息模板,并拿到模板ID安装包
composer require qcloudsms/qcloudsms_php
引入包
use Qcloud\Sms\SmsSingleSender;
.env配置参数
#腾讯云短信 QCLOUD_SMS_APP_ID=应用的APPID QCLOUD_SMS_APP_KEY=应用的key QCLOUD_SMS_TEMPLATE_ID=模板ID QCLOUD_SMS_SIGN_NAME=签名
源码
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Qcloud\Sms\SmsSingleSender; use App\Models\UserAuth; use Cache; class EmailController extends Controller { function tencentMsg(Request $request){ $messages = [ 'phone.regex' => '手机号不合法', 'required'=>'必要参数不能为空' ]; $validator = Validator::make($request->all(), [ 'phone' => 'required', 'phone' => 'regex:/^1[345789][0-9]{9}$/', 'token' => 'required', ], $messages); if ($validator->fails()) { $error = $validator->errors(); return hiSetCode(ERR, $error->first()); } $phone = Request()->input('phone', ''); // 收件邮箱 $token = Request()->input('token', ''); // 收件邮箱 $auth = UserAuth::where('token', $token)->first(); if (!$auth) return hiSetCode(ERR, '无效的token!', $auth); $appId = env("QCLOUD_SMS_APP_ID"); $appKey = env("QCLOUD_SMS_APP_KEY"); $templateId = env("QCLOUD_SMS_TEMPLATE_ID"); $sign = env("QCLOUD_SMS_SIGN_NAME"); $message = rand(1000,9999); $sender = new SmsSingleSender($appId, $appKey); // 构建短信参数 $params = [$message]; // 这里假设模板中只有一个参数,根据实际情况进行修改 // 发送短信 $result = $sender->sendWithParam("86", $phone, $templateId, $params, $sign); $result = json_decode($result, true); if(isset($result['errmsg']) && $result['errmsg']){ // return hiSetCode(ERR, $result['errmsg']); } $auth->code = $message; $auth->save(); return hiSetCode(SUCC, '发送成功!'); } }
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: