thinkphp网易云信短信接口核心代码

来源:互联网 发布:最全在线网络理财投资 编辑:程序博客网 时间:2024/05/16 15:54
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>网易云信短信接口使用详解</title></head><body><script type="text/javascript">$(function(){ $('.sms').click(function(){   $.ajax({type:'POST',url:"__URL__/sendSms",data:{'mobile':$('#username').val()},success:function(date){if(date=='200'){  alert('验证码发送成功,请注意查收,有效时间10分钟,如未收到2分钟后可重新获取')  var tj = setInterval('daoji()',1000)  }else{    alert('验证码发送失败,请检查手机号码格式是否正确')    } }})       })})//$() endvar flag = false;//验证输入的短信验证码是否正确function CheckSmsYzm(){$.ajax({type:'POST',async:false, ////要设置为同步的,要不CheckUserName的返回值永远为falseurl:"__URL__/CheckSmsYzm",data:{'mobile':$('#username').val(),'code':$('#code').val()},success:function(date){if(date=='200'){ }else{    alert('验证码输入错误')  flag = true;  } }})}//表单提交时,用于判断验证码是否正确CheckSmsYzm()   if(flag){           return false;}//倒计时函数,即获取验证码后120秒内无法单机再次获取$time = '120';function daoji(){if($time == 0){$('.time').text('')$('.sms').removeAttr('disabled')//clearInterval(tj);}else{$time = $time - 1;$('.time').text($time)$('.sms').attr({'disabled':'disabled'})}} </script><button class="sms submit" onclick="return false"  >发送验证码</button><span class="red time"></span><?php//发送短信验证码 public function sendSms(){    $sms =  new \Org\Util\Sms;;$mobile = $_POST['mobile'];$date = $sms->SendSmsCode($mobile); //返回数据示例'{"code": 200,"msg": "88","obj": "1908"}' 分别代表状态吗,短信id,本次发送的验证码; $date = json_decode($date, true); //将json格式数据转换为数组$da =$date['code'];//$da ='200';$this->ajaxReturn($da);    } //效验短信验证码public function CheckSmsYzm(){    $sms =  new \Org\Util\Sms;;$mobile = $_POST['mobile'];$code =   $_POST['code'];$date = $sms->CheckSmsYzm($mobile,$code); //返回数据示例'{"code": 200}' 代表状态码; $date = json_decode($date, true); //将json格式数据转换为数组$da =$date['code'];//$da ='200';$this->ajaxReturn($da);    }//以下为使用的短信验证码类// 自己建立短信验证库 namespace Org\Util;class Sms{    const APP_KEY = '8e8888faa93043d29bca6dfcdbdba99e';        const APP_SECRET = '775f6ec3d5d1';//发送验证码 public function SendSmsCode($mobile = ""){        $appKey = self::APP_KEY;        $appSecret = self::APP_SECRET;        $nonce = '100';        $curTime = time();        $checkSum = sha1($appSecret . $nonce . $curTime);        $data  = array(            'mobile'=> $mobile,        );        $data = http_build_query($data);        $opts = array (            'http' => array(                'method' => 'POST',                'header' => array(                    'Content-Type:application/x-www-form-urlencoded;charset=utf-8',                    "AppKey:$appKey",                    "Nonce:$nonce",                    "CurTime:$curTime",                    "CheckSum:$checkSum"                ),                'content' =>  $data            ),        );        $context = stream_context_create($opts);        $html = file_get_contents("https://api.netease.im/sms/sendcode.action", false, $context);//echo $html;       return $html;} /* //校验验证码 //mobile 手机号 //code 验证码  */ public function CheckSmsYzm($mobile = "",$Code=""){        $appKey = self::APP_KEY;        $appSecret = self::APP_SECRET;        $nonce = '100';        $curTime = time();        $checkSum = sha1($appSecret . $nonce . $curTime);        $data  = array(            'mobile'=> $mobile,            'code' => $Code,        );        $data = http_build_query($data);        $opts = array (            'http' => array(                'method' => 'POST',                'header' => array(                    'Content-Type:application/x-www-form-urlencoded;charset=utf-8',                    "AppKey:$appKey",                    "Nonce:$nonce",                    "CurTime:$curTime",                    "CheckSum:$checkSum"                ),                'content' =>  $data            ),        );        $context = stream_context_create($opts);        $html = file_get_contents("https://api.netease.im/sms/verifycode.action", false, $context);return $html;    } }//class end  ?> </body></html>

0 0
原创粉丝点击