日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

手机 验证码 发送

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手机 验证码 发送 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

調用京東萬象手機驗證碼接口

/*** @param number $phone 手機號碼* @param string $content 信息模板* @return bool|string*/public function sendmsg($phone, $content){//配置中調取$gateway = config('phone.gateway');$appkey = config('phone.appkey');$url = $gateway . '?appkey=' . $appkey . "&content=" . $content . "&mobile=" . $phone;$url .= '&mobile=' . $phone . '&content=' . $content;$res = curl_request($url, false, [], true);//處理結果if (!$res) {return '請求發送失敗';}//解析結果$arr = json_decode($res, true);if (isset($arr['code']) && $arr['code'] == 10000) {//短信接口調用成功return true;} else {return '短信發送失敗';}}

接受手機號碼并進行驗證

/*** @param number $phone 手機號碼* @return \think\response\Json*/public function phone_code($phone){$phone = input('phone') ?? '';if (!preg_match('/^1[3-9]\d{9}$/', $phone)) return json(['code' => 1001, 'msg' => '手機號不符合規則', 'data' => []]);//判斷 是否重復發送$last_time = \cache('week_time_' . $phone);if ((time() - $last_time) < 60) {return json(['code' => 1001, 'msg' => '驗證碼獲取太過頻繁,請一分鐘后再試', 'data' => []]);}//調用 函數 發送 隨機驗證碼$number = rand(1111, 9999);$content = "【品優購】你的驗證碼是:{$number},3分鐘內有效!";//調用 公共方法$result = $this->sendmsg($phone, $content);if ($result) {//記錄住驗證碼內容cache("week_" . $phone, $number, 60);//記錄發送驗證碼的時間cache("week_time_" . $phone, time());return json(['code' => 200, 'msg' => '驗證碼發送成功', 'data' => ['phone' => $phone]]);} else {return json(['code' => 1001, 'msg' => '服務器繁忙,請稍候再試', 'data' => ['phone' => $phone]]);}}

封裝公共函數

if (!function_exists('phone_code')) {function phone_code($phone){$phone = input('phone') ?? '';if (!preg_match('/^1[3-9]\d{9}$/', $phone)) return ['code'=>1001,'msg'=>'手機號不符合規則','data'=>''] ;//判斷 是否重復發送$last_time = \cache('week_time_' . $phone);if ((time() - $last_time) < 60) return ['code'=>1001,'msg'=>'驗證碼獲取太過頻繁,請一分鐘后再試','data'=>''];//調用 函數 發送 隨機驗證碼$number = rand(1111, 9999);$content = "【品優購】你的驗證碼是:{$number},3分鐘內有效!";//調用 公共方法$result = sendmsg($phone, $content);if ( $result && $result['code'] == 200 ) {//記錄住驗證碼內容cache("week_" . $phone, $number, 60);//記錄發送驗證碼的時間cache("week_time_" . $phone, time());return ['code'=>200,'msg'=>$result['msg'],'data'=>''];} else {return ['code'=>1001,'msg'=>$result['msg'],'data'=>''];}} }if (!function_exists('sendmsg')) {//使用curl_request函數調用短信接口發送短信function sendmsg($phone, $number = '',$content = ''){//從配置中取出請求地址、appkey$gateway = config('phone.gateway');$appkey = config('phone.appkey');//https://way.jd.com/chuangxin/dxjk?mobile=13568813957&content=【創信】你的驗證碼是:5873,3分鐘內有效!&appkey=您申請的APPKEY$url = $gateway . '?appkey=' . $appkey;if ($content && $number = ''){$url .= '&mobile=' . $phone . '&content=' . $content;//get//$res = curl_request($url, false, [], true);//post請求$params = ['mobile' => $phone,'content' => $content];}else{$content = $content = "【品優購】你的驗證碼是:{$number},3分鐘內有效!";$url .= '&mobile=' . $phone . '&content=' . $content;//get//$res = curl_request($url, false, [], true);//post請求$params = ['mobile' => $phone,'content' => $content];}$res = curl_request($url, true, $params, true);//處理結果if (!$res) {return ['code'=>1001,'msg'=>'請求發送失敗','data'=>''];}//解析結果$arr = json_decode($res, true);if (isset($arr['code']) && $arr['code'] == 10000) {//短信接口調用成功return ['code'=>200,'msg'=>'驗證碼發送成功','data'=>''];} else {return ['code'=>1001,'msg'=>'驗證碼發送失敗','data'=>''];}}}

發送 和登錄

public function test(){$phone = input('phone') ?? '';//$res = phone_code($phone);$number = rand(000,9999);$res = sendmsg($phone,$number ,'');if ( $res['code'] == 200) return json(['code'=>200,'msg'=>$res['msg'],'data'=>$res]);return json(['code'=>1001,'msg'=>$res['msg'],'data'=>'']);}public function plogin(Request $request){$params = input();$validate = Validate::rule(['phone'=> 'require|mobile','code' => 'require|length:4']);cache('week_'.$params['phone'],'1234',300);if (!$validate->check($params)) return json(['code'=>1001,'msg'=>$validate->getError(),'data'=>'']);$old_code = cache('week_'.$params['phone']) ?? '';cache('week_'.$params['phone'],'');halt($old_code);if ($old_code != $params['code']) return json(['code'=>1001,'msg'=>'驗證碼錯誤','data'=>'']);$model = \app\model\User::where('phone',$params['phone'])->find();if (!$model) return json(['code'=>1001,'msg'=>'用戶不存在 請先注冊','data'=>'']);return json(['code'=>200,'msg'=>'登錄成功','data'=>'']);}

調用方法 進行發送

$params = input();$validte = Validate::rule(['captcha' => 'require|length:4|captcha','phone' => 'require|mobile']);if (! $validte->check($params) ) return json(['code'=>1001,'msg'=>$validte->getError(),'data'=>'']);$res = phone_code($params['phone']);if ($res && $res['code'] == 200){return json(['code'=>200,'msg'=>'驗證碼發送成功','data'=>$res['data']]);}return json(['code'=>1001,'msg'=>$res['msg'],'data'=>'']);

一個手機號每天限制發送次數

public function index(){//$phone = input('phone');$key = date('Ymd',time()).$phone ;$click = cache($key) ?? 0;if ($click >= 5){return json(['code'=>1001,'msg'=>'驗證碼發送次數上限','data'=>'已發送'.$click.'次']);}else{//這里調用公共函數 發送 驗證碼$res = phone_code($phone);if ($res && $res['code'] == 200){$click ++;cache($key,$click,3600*24);return json(['code'=>200,'msg'=>'驗證碼發送成功','data'=>$res['data']]);}else{return json(['code'=>1001,'msg'=>'驗證碼發送失敗','data'=>'']);}}}

總結

以上是生活随笔為你收集整理的手机 验证码 发送的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。