阿里大於驗證碼發送 (ThinkPhp架構)

來源:互聯網
上載者:User

標籤:send   logger   tsm   and   result   代碼   success   top   開發   

1.登入平台 阿里大於



2.登陸之後我們可以看到資費,使用情境等,在進入正題之前我們需要一些準備工作,首先我們先瞭解下簡訊的請求參數,在這裡我們需要注意的是sms_param這個參數,在接下來我們申請簡訊模板的時候會用到




3.相應參數,對待傳回值我們記住最基本的true or false 即可




4.瞭解完公用參數,接下來我們進入正題,首先我們得配置簡訊簽名和配置簡訊模板,簡訊簽名出現在簡訊開頭,注意簡訊模板,這裡將用到上文提示的sms_param參數




5.同時,在其網站上下載好SDK,展示的是我的存放位置,使用的是thinkphp架構,其他的開發環境檔案的存放位置在官網上有詳細說明




6.接下來是項目背景配置,其中appkey,secret在你完成上述申請之後可以在個人管理中心查看



class DuanXinController extends Controller {
    public function index()
    {
$appkey = "...";//你的App key
$secret = "...";//你的App Secret:
import(‘Org.taobao.top.TopClient‘);
import(‘Org.taobao.top.ResultSet‘);
import(‘Org.taobao.top.RequestCheckUtil‘);
import(‘Org.taobao.top.TopLogger‘);
import(‘Org.taobao.top.request.AlibabaAliqinFcSmsNumSendRequest‘);
//將需要的類引入,並且將檔案名稱改為原檔案名稱.class.php的形式
$c = new \TopClient;
$c->appkey = $appkey;
$c->secretKey = $secret;
$req = new \AlibabaAliqinFcSmsNumSendRequest;
$req->setExtend("123456");//確定發給的是哪個使用者,參數為使用者id
$req->setSmsType("normal");
session_start();
$verifycode = strval(rand(1000,9999));
$_SESSION[‘verifycode‘] = $verifycode;
$userStatus=0;
/*
進入阿里大魚的管理中心找到簡訊簽名管理,輸入已存在簽名的名稱,這裡是身分識別驗證。
*/
$req->setSmsFreeSignName("山水優品");
$smsParams = [
    ‘code‘ => $verifycode,
    ‘product‘ => ‘...‘
];
$req->setSmsParam(json_encode($smsParams));
//$req->setSmsParam("{‘code‘:numfour() ‘product‘:‘山水優品‘}");
//這裡設定的是發送的簡訊內容:驗證碼${code},您進行中${product}身分識別驗證,打死不要告訴別人哦!”
$req->setRecNum(...);//參數為使用者的手機號碼
$req->setSmsTemplateCode("SMS_16670740");
$resp = $c->execute($req);
//var_dump($resp);這裡是用來列印返回結果
if($resp->result->success)
    {
        $userStatus=1;
    }
    else
    {
        $userStatus=0;
    }
    echo $userStatus;
}
// 檢查驗證碼是否正確
function checkcode(){
    session_start();
    $verifycode = $_SESSION[‘verifycode‘];
    $inputcode = I(‘post.code‘);
    $checkstatus = 0;
    if ($inputcode == $verifycode) {
        $checkstatus = 1;
    }else{
        $checkstatus = 0;
    }
    echo $checkstatus;
}




7.下面的代碼是項目前台的Ajax請求部分:



    send.onclick = function() {
                var oldTel = document.getElementById(‘oldTel‘).value;
                var that = this;
                var times = 60;
                this.disabled = true;
                timer1 = setInterval(function() {
                    times--;
                    that.value = times + "秒後重試";
                    if (times <= 0) {
                        that.disabled = false;
                        that.value = "發送驗證碼";
                        clearInterval(timer1);
                        times = 60;
                    }
                }, 1000);
                $.ajax({
                url: "{:U(‘DuanXin/index‘)}",
                type: "post",
                data: {
                    ‘name‘:oldTel,
                },
                    success: function(responseText, status, xhr) {
                        if (status == ‘success‘) {
                            if (responseText == 1) {
                                return true;
                            } else {
                                return false;
                            }
                        } else {
                            return false;
                        }
    
                    },
    
                    error: function() {
                        return false;
                    },
    
                    timeout: 1000 * 60,
            });
}

這裡的按鈕加入了一個簡單的倒計時功能,即點擊發送後60s內無法點擊按鈕;

阿里大於驗證碼發送 (ThinkPhp架構)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.