laravel 腾讯云短信发送

public function tecentSms(Request $request)
    {
        $phone = $request->input("hiphone");
        $appid = "1400617682";//SDK appid
        $appkey = "75c4537ce96a78e33ab1410a287084de";//SDK appkey
        $rand = rand(100000, 999999);//code
        $minute = 1000;//有效时间
        $tempid = 1264814;//模板id
        $params = ["$rand"];
        $sign = "郑自强学习和测试代码";//签名内容
        try {
            $sender = new SmsSingleSender($appid, $appkey);
            $result = $sender->sendWithParam("86", $phone, $tempid, $params, $sign);//86为地区数字编号
            $rsp = json_decode($result, true);
            if ($rsp['result'] == 0) {
                $rsp['code'] = '1';
                $rsp['msg'] = $phone;
                Cache::put($phone, $rand, $minute);//放入缓存
                return $rsp;
            } else {
                $rsp['code'] = '0';
                return $rsp;
            }
        } catch (\Exception $e) {
            $rsp['code'] = '0';
            return $rsp;
        }
    }

//判断验证码是否正确

 if(!Cache::has("$iphone"))//判断是否还存在
        {
            $data['code'] = '6';
            $data['msg'] = '请重新获取验证码';
            return response()->json($data);
        }
        $cache_code = Cache::get("$iphone");//获取

        if($yzm != $cache_code)//比对
        {
            $data['code'] = '7';
            $data['msg'] = '请输入正确的短信验证码';
            return response()->json($data);
        }

        Cache::forget("$iphone");//释放

 

posted @ 2022-03-22 10:40  是梦别喊疼想赢别喊停  阅读(147)  评论(0)    收藏  举报