如何傳送簡訊驗證碼之laravel框架
阿新 • • 發佈:2018-11-26
簡訊驗證碼的應用場景: 應用場景:簡訊註冊 密碼找回 簡訊通知 場景一:註冊 場景二:賬號繫結手機號 場景三:登陸 簡訊實現原理: 發起請求 =》 簡訊API介面流程處理 =》接收結果 簡訊平臺網址:http://www.sms.cn/zhuce.html?u=34952 需要注意事項: 明確介面【PHP】 簡訊模板設定 第一步:找到對應的模板 第二步:學會看介面,明確需要的引數有哪些及如何獲取到具體相關引數 第三步:如何讀介面【file_get_contents()】 實現簡訊註冊: 發起請求:編寫簡訊傳送核心功能 【重點:跨域訪問】 前端對接:前臺使用者點擊發送 【Ajax無重新整理】 專案融合對接:後臺接收驗證結果 【流程及邏輯】 以下以Laravel程式碼為例: public function userA(Request $request){ $result=$request->all();//only $phone=$result['telphone'];//取手機號 $code=rand(111111,666666);//隨機數 Session::put('jiushen',$code);//設定了一個jiushen對應的值 Session::get('jiushen'); $url='http://api.sms.cn/sms/?ac=send&uid=賬號&pwd=加密密碼&template=模板id&mobile='.$phone.'&content={"code":"'.$code.'"}'; //file_get_contents 讀取檔案資訊 //file_put_contents 寫入檔案 // file_put_contents('demo.txt', $url); $info=file_get_contents($url);//json //如何將json資料轉換成陣列 json_decode($info) 物件 json_decode($info,true) 陣列 $data=json_decode($info,true); if($data['stat']==100){ $array['status']=100; $array['info']="簡訊傳送成功"; }else{ file_put_contents('demo.txt', $url); $array['status']=101; $array['info']="簡訊傳送失敗"; } return $status; }