先下载类文件包,解压到lib文件下,到入口文件中定义一个新的路径,引入类文件。
<?php namespace app\index\controller; use think\Controller; use lib\REST; use think\Db; use think\Request; use think\Session; class Phone extends Controller { public function index() { return view("index"); } //入库 public function register(){ $data['name']=Request::instance()->param("name"); $data['password']=Request::instance()->param("password"); $data['phone']=Request::instance()->param("phone"); $data['regtime']=date('Y-m-d H:i:s'); $code1=Request::instance()->param("code"); $code=Session::get('code'); if($code1!=$code){ return $this->error("验证码错误"); }else{ $res=Db::table('phones')->insert($data); if($res){ return $this->success("注册成功"); }else{ return $this->success("注册失败"); } } } //发送验证码 public function sendMoblieCode(){ //接收手机号 $phone=Request::instance()->param("phone"); //生成验证码 $code=rand(100000,999999); //引入类文件 require_once EXTEND_PATH."Phone.php"; //主账号 $accountSid= '8aaf070866235bc501667f9e877c3421'; //主账号Token $accountToken= 'aef9c6aa26f04085bda5594c9b25397f'; //应用Id $appId='8aaf070866235bc501667f9e87d53427'; //请求地址 $serverIP='app.cloopen.com'; //端口号 $serverPort='8883'; //REST版本号 $softVersion='2013-12-26'; //设置接口参数 $rest = new REST($serverIP,$serverPort,$softVersion); $rest->setAccount($accountSid,$accountToken); $rest->setAppId($appId); //发送验证码 $result=$rest->sendTemplateSMS($phone,array($code,60),'1'); if($result){ Session::set('code',$code); return 1; }else{ return 0; } } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户注册页面</title> </head> <body> <center> <form action="{:url('phone/register')}" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="name" id="name"/></td> </tr> <tr> <th>密码:</th><td> <input type="password" name="password" id="password"/></td> </tr> <tr> <th>手机号:</th> <td> <input type="text" name="phone" id="phone"/> <button onclick="sendMobile();return false;">发送手机验证码</button> </td> </tr> <tr> <td>验证码:</td> <td><input type="text" name="code" id="code"/></td> </tr> <tr> <td><input type="submit" value="注册用户"></td> </tr> </table> </form> </center> </body> <script type="text/javascript" src="/static/js/jquery.js"></script> <script type="text/javascript"> function sendMobile(){ var phone=$("#phone").val(); $.ajax({ url:"{:url('phone/sendMoblieCode')}", type:"POST", data:{phone:phone} }).done(function(d){ if(d==1){ alert('发送成功'); }else{ alert('发送失败'); } }) } </script> </html>