https://blog.csdn.net/sunny_lg/article/details/52471225
现在登录注册时 我们的验证方法 不在单一化 手机发送验证码 已经成为常态 让我们 一起来实现以下吧 !
首先我们要先找短信的接口 我用的是 云信使 这里是他的地址http://sms.sms.cn/login.php?cgh 大家可以去注册一下
1.进入视图
<ul class="register"> <li> <label>手机号:</label> <input class="ipt-box tel-bg" value="" id="regi_mobile" type="text"> </li> <li> <label>验证码:</label> <input class="code" value="六位数字验证码" id="validatecode" type="text"> <input class="code" onclick=‘duanxin()‘ value="获取验证码" type="button"> </li> </ul>
<script src="js/jquery.js"></script> <script> function duanxin() { //获取手机ID var iphone = $("#regi_mobile").val(); $.ajax({ url: ‘registers‘, data: {‘iphone‘: iphone}, type: "GET", dataType: "Json", success: function (msg) { if (msg[‘stat‘] == ‘100‘) { alert(‘短信发送成功了‘); } else { alert(‘短信发送失败了‘); } } }); } </script>
2. 路由
Route::any(‘registers‘,‘LoginController@loginDo‘);
3.进入控制器
public function loginDo() { $iphone = $_GET[‘iphone‘]; $code = rand(1000, 9999); setcookie(‘code‘, $code, time() + 600); //把URL地址改成你自己就好了,把手机号码和信息模板套进去就行 $url = ‘http://api.sms.cn/sms/?=send&uid=XXX&pwd=61dfa5a45c06bf691767d35bcb197595&template=384859&mobile=‘ . $iphone . ‘&content={"code":"‘ . $code . ‘"}‘ $data = array(); $method = ‘GET‘; $res = $this->curlPost($url, $data, $method); echo $res; } /*curlpost传值*/ public function curlPost($url, $data, $method) { $ch = curl_init(); //1.初始化 curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式 //4.参数如下 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)‘);//模拟浏览器 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Accept-Encoding: gzip, deflate‘));//gzip解压内容 curl_setopt($ch, CURLOPT_ENCODING, ‘gzip,deflate‘); if ($method == "POST") {//5.post方式的时候添加数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch);//6.执行 if (curl_errno($ch)) {//7.如果出错 return curl_error($ch); } curl_close($ch);//8.关闭 return $tmpInfo; }
4. 这样就好了 大家快测试一下吧!