php调用云片网接口发送短信实例
2024-11-23 18:38:19
public function mobilecode(){ $randcode = rand('100000','999999'); $_SESSION['randcode'] = $randcode; $phone = $this->input->get('phone'); $this->yunpian_appkey = '41b1f8262ac7adfe74e5d59eb226864c';//云片账户appkey $tpl_content = '【MC魔力工匠】您的验证码是'.$randcode; //初始化 $ch = curl_init(); $data=array('text'=>$tpl_content,'apikey'=>$this->yunpian_appkey,'mobile'=>$phone); curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); $send_data = curl_exec($ch); curl_close($ch); exit(json_encode(array(code=>1,msg=>$backmassage))); } <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no"> <meta name="format-detection" content="telephone=no" /> <title>魔力工匠</title> <link rel="stylesheet" type="text/css" href="../addons/ewei_shopv2/static/js/dist/foxui/css/foxui.min.css?v=0.2"> <link rel="stylesheet" type="text/css" href="../addons/ewei_shopv2/template/mobile/default/static/css/style.css?v=2.0.9"> <link rel="stylesheet" type="text/css" href="../addons/ewei_shopv2/static/fonts/iconfont.css?v=2017070719"> <script src="./resource/js/lib/jquery-1.11.1.min.js"></script> <script src="../addons/ewei_shopv2/static/js/dist/foxui/js/foxui.min.js"></script> </head> <body ontouchstart> <div class='fui-page-group statusbar'> <link href="../addons/ewei_shopv2/plugin/mmanage/static/css/style.css?v=20170419" rel="stylesheet" type="text/css" /> <div class='fui-page fui-page-current'> <div class="fui-header fui-header-success" style="background: #2c3845;"> <div class="title">授权登录</div> <div class="fui-header-right"></div> </div> <div class='fui-content page-login'> <div class="panel-logo"> <img src="../addons/logo.jpg" /> </div> <div class="panel-login"> <div class="fui-cell-group fui-cell-group-o"> <div class="fui-cell"> <div class="fui-cell-info"> <input type="num" placeholder="请输入手机号码" id="dr_phone" class="fui-input" name="phone"/> </div> </div> <div class="fui-cell"> <div class="fui-cell-info"> <input type="text" class="fui-input" id="randcode" placeholder="请输入验证码" style="width: 100px;margin-right:0;"> <div class="yanzheng" onclick="dr_send_sms()" style="color:#FEB37B!important;float: right!important;">获取验证码</div> </div> </div> </div> <div class="btn btn-success block btn-submit" data-type="" id="btn-submit">登录</div> </div> </div> </div> </div> <script> $("#btn-submit").click(function(){ var phone=document.getElementById("dr_phone").value; var randcode=document.getElementById("randcode").value; var reg=/^[1][3,4,5,7,8][0-9]{9}$/; if(!reg.test(phone)){ FoxUI.alert('手机号格式不正确'); return false; } if(!randcode){ FoxUI.alert('请输入验证码'); return false; } $.get('/?c=app&m=login&phone='+$(".fui-input").val()+'&randcode='+randcode, {}, function(res){ if(res.code == 0){ setTimeout(function(){ location.href = '/?c=app&m=member'; }, 1000) } else if(res.code==1){ FoxUI.confirm('验证码不正确','提示', function(){ location.href = '/?c=app&m=login'; }); }else{ FoxUI.confirm('账号不存在','提示', function(){ location.href = '/?c=app&m=vip'; }); } }, 'json') }) </script> <script> function dr_send_sms() { var phone=document.getElementById("dr_phone").value; var reg=/^[1][3,4,5,7,8][0-9]{9}$/; if(!reg.test(phone)){ alert('手机号格式不正确'); return false; } $.post("/index.php?&c=app&m=mobilecode&phone=" + phone, function(data){ if (data.code) { countDown(60); } else { alert(data.msg); } }, 'json'); } function countDown(time){ $('.yanzheng').text('剩余 ' + time + 's').attr('onclick',"javascript:void(0)"); var time = time - 1; // console.log(time); if(time < 1){ $('.yanzheng').css('background-color','#fe7418').text('发送验证码').attr('onclick',"dr_send_sms()"); return false; } setTimeout("countDown(" + time + ")",1000); } function getCookie(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = [xss_clean].match(reg)) return unescape(arr[2]); else return null; } </script> </body> </html>