阿里大魚短信接口整合Tp3.2.3開發整理


阿里大魚 http://www.alidayu.com/ 的短信接口總體是不錯的,別安駒個人認為不管是從性價比還是穩定性上都是跟同類的短信接口好些,畢竟是大公司的東西不會差到哪去。下面把之前開發的短信接口做個整理。

1,登陸阿里大魚的管理中心新增自己的應用,然后使用什么模板請提交審核,此處不做多說。

2,核心步驟,整理阿里大魚給出的php示例核心包,不過別安駒已經為你整理好了只需要下載即可  核心包傳送門 (ps:解壓密碼:http://www.bieanju.com/)。

3,前台調用發送短信利用ajax異步請求處理發送,

  3.1、當然此處會先用一些公共配置所以先來看看配置:

/* 阿里大魚短信接口 */
    'Alidayu' => array(
        'Appkey' => 'xxxx',
        'SecretKey'=>'xxxx',
        'SmsTemplate' => 'SMS_xxx',// 注冊提示模板號
        'SignName'    => '測試短信',
        'Product'    => '別安駒測試短信內容',
        'Expire'    => '300'
    ),

 需要注意的地方是SignName,短信簽名,位置在管理中心> 配置管理 > 短信通知 如圖:

  3.2、后台發送短信方法(ps:可以寫成公共函數,畢竟發短信的地方不止注冊,可能需要修改密碼或者找回密碼都會用到)此處傳參要求請參照阿里大魚官方要求 傳送門 (PS:請登錄后查看):

//調用的發送方法
public
function mobilevalid() { $mobile = I('mobile'); echo sendMobileCode($mobile); }
/* 
     * 阿里大魚手機驗證碼發送函數
     *  
     *  */
    function sendMobileCode($mobile){
        Vendor('Alidayu.Client');
        Vendor('Alidayu.SendSms');
        Vendor('Alidayu.ResultSet');
        Vendor('Alidayu.RequestCheckUtil');
        Vendor('Alidayu.TopLogger');
        $client = new Client();
        $client->appkey = C('Alidayu.Appkey');
        $client->secretKey = C('Alidayu.SecretKey');
        /* 組裝發送前置參數 */
        $code = randCode(4);
        $send_msg = array('code'=>$code,'product'=>C('Alidayu.Product'),'item'=>C('Alidayu.Product'));
        $request = new SendSms();
        $request->setExtend($mobile);
        $request->setSmsType("normal");
        $request->setSmsFreeSignName(C('Alidayu.SignName'));
        $request->setSmsParam(json_encode($send_msg));
        $request->setRecNum($mobile);
        $request->setSmsTemplateCode(C('Alidayu.SmsTemplate'));
        $result = json2array(json_encode($client->execute($request)));
        if($result['result']['success']){
            cookie('mobile_validated',$code,C('Alidayu.Expire'));
            $msg = array('status'=> 1,'info'=>"驗證碼已發送至您的手機!");
        }else{
            $msg = array('status'=> 0,'info'=>"驗證碼發送失敗,".$result['sub_msg']."請稍后重試!");    
        }
        return json_encode($msg);
        
    }

發送短信的公共函數中使用的函數:

  1:json串轉array處理 json2array:

 /**
     * json串轉array處理
     * @param  json json字符串
     * @author bieanju <bieanju@163.com>
     * @return array
     */
    function json2array($json){
        
        $json = str_replace("\r\n", '\n',trim($json,chr(239).chr(187).chr(191)));//剔除bom以及去除\r
    
        return json_decode($json,true);
    
    }

  2:隨機生成驗證碼數字函數:

function randCode($length){
        return substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, $length);
    }

  3.3、前台調用主要是ajax寫的函數處理發送短信和倒計時:

var mobileUrl = "處理發送短信的方法地址";
$(function(){   $('#sendmsg').click(function(){ var mobile = $("input[name='mobile']").val(); var url = mobileUrl; curCount = count; if(!mobile){ layer.msg("對不起請先填寫手機號"); return false; } $.get(mobileUrl,{mobile:mobile},function(json){ json = eval("("+json+")"); if(json.status){ InterValObj = window.setInterval(SetRemainTime, 1000); layer.msg(json.info); }else{ layer.msg(json.info); } }) }) }) function SetRemainTime() { if (curCount == 0) { window.clearInterval(InterValObj);//停止計時器 $("#sendmsg").removeAttr("disabled");//啟用按鈕 $("#sendmsg").html("重新發送驗證碼"); $("#sendmsg").css("background","#FAEBC4"); }else{ curCount--; $("#sendmsg").html(curCount+"秒后重發"); $("#sendmsg").attr("disabled","disabled"); $("#sendmsg").css("background","#E8E6E0"); } }

Ok大功告成,短信模板就此已經發送成功了!是不是很簡單呢?哈哈……


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM