首页 > 开发 > PHP > 正文

Thinkphp实现短信验证注册功能

2024-05-04 22:51:46
字体:
来源:转载
供稿:网友

前言

注册时经常需要用到短信验证码,本文记录一下思路和具体实现。

短信验证平台使用云片,短信验证码的生成使用thinkphp。

思路

1、用户输入手机号,请求获取短信验证码。

2、thinkphp生成短信验证码,存储,同时和其他参数一起发送请求给云片。

3、云片发送短信验证码到指定手机号。

4、用户输入短信验证码。

5、thinkphp根据验证码是否正确、验证码是否过期两个条件判断是否验证通过。

代码实现

验证接口

接口地址:https://sms.yunpian.com/v1/sms/send.json。

使用postman,输入三个必须的参数apikey、mobile和text。

php发起http/https请求

使用php的curl函数发起https请求,带入参数apikey、mobile和text。

// 获取短信验证码public function getSMSCode(){// create curl resource $ch = curl_init(); // set url$url = 'https://sms.yunpian.com/v1/sms/send.json'; curl_setopt($ch, CURLOPT_URL, $url); // set param$paramArr = array('apikey' => '******','mobile' => '******','text' => '【小太阳】您的验证码是1234');$param = '';foreach ($paramArr as $key => $value) {$param .= urlencode($key).'='.urlencode($value).'&';}$param = substr($param, 0, strlen($param)-1);curl_setopt($ch, CURLOPT_POSTFIELDS, $param);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_POST, 1);//curl默认不支持https协议,设置不验证协议curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); echo $output;}

生成随机短信验证码

默认生成四位的随机短信验证码。

// 生成短信验证码public function createSMSCode($length = 4){$min = pow(10 , ($length - 1));$max = pow(10, $length) - 1;return rand($min, $max);}

整合

在数据库新建表sun_smscode:

DROP TABLE IF EXISTS `sun_smscode`;CREATE TABLE `sun_smscode` (`id` int(8) NOT NULL AUTO_INCREMENT,`mobile` varchar(11) NOT NULL,`code` int(4) NOT NULL,`create_at` datetime NOT NULL,`update_at` datetime NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;thinkphp代码:// 获取短信验证码public function getSMSCode(){// create curl resource $ch = curl_init(); // set url$url = 'https://sms.yunpian.com/v1/sms/send.json'; curl_setopt($ch, CURLOPT_URL, $url); // set param$mobile = $_POST['mobile'];$code = $this->createSMSCode();$paramArr = array('apikey' => '******','mobile' => $mobile,'text' => '【小太阳】您的验证码是'.$code);$param = '';foreach ($paramArr as $key => $value) {$param .= urlencode($key).'='.urlencode($value).'&';}$param = substr($param, 0, strlen($param)-1);curl_setopt($ch, CURLOPT_POSTFIELDS, $param);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); //$outputJson = json_decode($output);$outputArr = json_decode($output, true);//echo $outputJson->code;//echo $outputArr['code'];if($outputArr['code'] == '0'){$data['mobile'] = $mobile;$data['code'] = $code;$smscode = D('smscode');$smscodeObj = $smscode->where("mobile='$mobile'")->find();if($smscodeObj){$data['update_at'] = date('Y-m-d H:i:s');$success = $smscode->where("mobile='$mobile'")->save($data);if($success !== false){$result = array('code' => '0','ext' => '修改成功','obj' => $smscodeObj);}echo json_encode($result,JSON_UNESCAPED_UNICODE);}else{$data['create_at'] = date('Y-m-d H:i:s');$data['update_at'] = $data['create_at'];if($smscode->create($data)){$id = $smscode->add();if($id){$smscode_temp = $smscode->where("id='$id'")->find();$result = array('code'=> '0','ext'=> '创建成功','obj'=>$smscode_temp);echo json_encode($result,JSON_UNESCAPED_UNICODE);}}}}}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表