使用容联云通讯开发获取短信验证码功能

首先,注册一个容联云通讯的账号,然后创建一个应用。

 

 

然后在号码管理那里绑定测试号码。可以查看官方文档:https://doc.yuntongxun.com/p/5a531a353b8496dd00dcdfe2

 

再然后,开干,话不多说,直接上代码:

前端(忒简陋哈,只是测试能否获取):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <button id="dd">点击调用</button>
    </body>
    <script type="text/javascript">
        $("#dd").click(function(){
            $.ajax({
                url: "http://127.0.0.1/sms/Demo/SendTemplateSMS.php", 
                data:{
                    phone:1331144797
                },
                success: function(res){
                    console.log(res)
                  }});
        })
    </script>
</html>

php代码(SendTemplateSMS.php):看不懂参数的看官方文档:https://doc.yuntongxun.com/p/5a533e0c3b8496dd00dce08c

<?php
/*
 *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

include_once("../SDK/CCPRestSDK.php");

//主帐号
$accountSid= '8aaf070863b5263bf1a011806ee';

//主帐号Token
$accountToken= 'c30beebd39db4835d1d68d36db6';

//应用Id
$appId='8aaf0708780055cd01ec83fa5';

//请求地址,格式如下,不需要写https://
$serverIP='app.cloopen.com';

//请求端口 
$serverPort='8883';

//REST版本号
$softVersion='2013-12-26';

/**
  * 发送模板短信
  * @param to 手机号码集合,用英文逗号分开
  * @param datas 内容数据 格式为数组 例如:array('Marry','Alon'),如不需替换请填 null
  * @param $tempId 模板Id
  */       
function sendTemplateSMS($to,$datas,$tempId)
{
    
         // 初始化REST SDK
         global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion; 
         $rest = new REST($serverIP,$serverPort,$softVersion); 
         $rest->setAccount($accountSid,$accountToken); 
         $rest->setAppId($appId); 
        
     
         // 发送模板短信
         echo "Sending TemplateSMS to $to 
    ";
         $result = $rest->sendTemplateSMS($to,$datas,$tempId); 
         if($result == NULL ) {
             echo "result error!"; 
         }
         if($result->statusCode!=0) {
             echo "模板短信发送失败!
    ";
             echo "error code :" . $result->statusCode . "
    ";
             echo "error msg :" . $result->statusMsg . "
    ";
             //下面可以自己添加错误处理逻辑
         }else{
             echo "模板短信发送成功!
    ";
             // 获取返回信息
             $smsmessage = $result->TemplateSMS; 
             echo "dateCreated:".$smsmessage->dateCreated."
    ";
             echo "smsMessageSid:".$smsmessage->smsMessageSid."
    ";
             //下面可以自己添加成功处理逻辑
         }
}

//Demo调用,参数填入正确后,放开注释可以调用 
sendTemplateSMS("13897,16601161",array(str_pad(mt_rand(10, 999999), 6, "0", STR_PAD_BOTH),2),1);
//sendTemplateSMS("手机号码集合",array(生成的随机验证码,2),1);


?>

 

目录结构:

 

 

实测真实可用,上面的电话号码以及Appid之类的要用的朋友需要改哈

 

posted @ 2021-04-13 10:31  健伟博客  阅读(520)  评论(0编辑  收藏  举报