php 調用短信驗證碼接口

這裏我只是大概記錄下基本的實現哈……           年後首更    ヾ(✿゚▽゚)ノ

這裏我使用的是騰訊雲的短信SDK。

執行下面代碼前首先要去下載個SDK包哦!!!

git地址: https://github.com/qcloudsms/qcloudsms_php     (解壓,取src下的php文件。

 

前端代碼 :

  <div>
      <input type="text" name="mobile" id="mobile" placeholder='手機號' />
        <input type="text" placeholder="短信驗證碼" name="msg_code" >
        <button id="btnSendCode">發送驗證碼</button>
 </div>


js:
 <script>
     $('#btnSendCode').click(function () {
        var countdown = 60;
        var t;
        function settime(obj) {
            if (countdown == 0) {
                obj.removeAttribute("disabled", false);
                obj.innerHTML = "獲取驗證碼";
                countdown = 60;
                return;
            } else {
                obj.setAttribute("disabled", true);
                obj.innerHTML = "重新發送(" + countdown + ")";
                countdown--;
            }
            t = setTimeout(function () {
                    settime(obj)
                }
                , 1000)
        }
        settime(this);
        $.post("調用發送短信的方法", {'mobile':$('#mobile').val()}, function (res) {
            if (res.status == 1) {
                $.toast(res.msg);
            } else {
                clearTimeout(t);
                $('#getMobileCode').attr("disabled", false);
                $('#btnSendCode').html('獲取驗證碼');
                $.alert(res.msg);
            }
        })
        return false;

    })

</script>

後端代碼:

appid、appkey、templid需要自己獲取哦……

 public function sendSms()
    {
        $data = input('post.');
        $sms_code = rand(1000, 9999);
        $appid = 1400000000;
        $appkey = '';
        $phoneNumber = $data['mobile'];
        $templId = 00000;
        try {
            $sender = new SmsSingleSender($appid, $appkey);
            $params = array($sms_code, 3);
            $result = $sender->sendWithParam("86", $phoneNumber, $templId,
                $params, "", "", "");
            $rsp = json_decode($result);
            if($rsp['errmsg'] == 'OK'){
                return ['code'=>200,'msg'=>'發送成功'];
            }else{
                return ['code'=>400,'msg'=>'發送失敗'];
            }
        } catch (\Exception $e) {
            echo var_dump($e);
        }


    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章