腾讯云短信发送php
申请完腾讯云的签名和模板之后
composer安装依赖包
composer require qcloudsms/qcloudsms_php安装成功后开始封装
在config中创建sms.php
return[
    // SDK AppID
    'app_id'=>'******',
    // App Key
    'app_key'=>'*****',
    // 签名管理内容
    'sms_sign'=>'*****',
    // 正文模板id
    'template_id'=>'*****'
];app_id,app_key在 应用管理->应用列表中查看

发送短信代码
class SendSms
{
    public static function sendInfo($code,$phone)
    {
        $app_id = config('sms.app_id');
        $app_key = config('sms.app_key');
        $template_id = config('sms.template_id');
        $sms_sign = config('sms.sms_sign');
        try {
            $sender = new \Qcloud\Sms\SmsSingleSender($app_id, $app_key);
            $params = [$code];
            $result = $sender->sendWithParam("86", $phone, $template_id,
                $params, $sms_sign, "", "");  // 签名参数未提供或者为空时,会使用默认签名发送短信
            $rsp = json_decode($result);
            if ($rsp->result == 0) {//0代表成功
                return '发送成功';
            }
            \think\facade\Log::error('发送短信验证码失败:' . $result);
        } catch (\Exception $e) {
        }
        return '发送失败';
    }
}在控制器中调用
public function sendSms(Request $request){
        $phone=$request->post('phone');
        // 生成随机验证码
        $code=rand(00000,9999);
        if (Cache::get("$phone"."_time")){
            return fail('发送次数频繁');
        }
        $res=SendSms::sendInfo($code,$phone);
        // 将验证码放入缓存
        Cache::set("$phone"."_code","$code",300);
        // 缓存验证码发送时间
        Cache::set("$phone"."_time",time(),60);
        return success($code);
    }发送成功

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号