待发短信

在线客服
产品支持 短信宝客服
合作渠道 渠道合作
服务咨询

4001-021-502

工作时间

9:00-21:00

极致CMS系统新增短信宝短信接口

极致CMS是开源免费的PHPCMS网站内容管理系统,无商业授权,简单易用,提供丰富的插件,实现零基础搭建不同类型网站(企业站,门户站,个人博客站等),是个建站的好帮手。小编带着大家一起开发极致CMS的短信宝插件接口。我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
1.首先打开项目app\admin\exts,新增smsbao 文件并创建config.php

1
2
3
4
5
6
7
8
9
10
<?php
 
return [
    'name'=>'短信宝短信注册',//插件名
    'desc'=>'使用短信宝短信注册或者登录系统',//插件介绍
    'author'=>'短信宝小编',//作者介绍,这里可以把自己的联系方式带上去,方便用户沟通
    'version'=>'1.0',//插件版本,默认1.0为最低版本
    'update_time'=>'2025-06-12',//插件更新时间,格式:Y-m-d
    'module'=>'Home',//插件应用的模块,Home表示前台模块,Admin表示后台模块.插件安装的时候会据此加载控制器到对应的目录中
];

接着创建PluginsController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace app\admin\exts;
 
use frphp\lib\Controller;
use frphp\extend\Page;
class PluginsController extends Controller {
 
    //自动执行
    public function _init(){
    /**
            继承系统默认配置
 
        **/
 
        //检查当前账户是否合乎操作
        if(!isset($_SESSION['admin']) || $_SESSION['admin']['id']==0){
            Redirect(U('Login/index'));
 
        }
 
        if($_SESSION['admin']['isadmin']!=1){
            if(strpos($_SESSION['admin']['paction'],','.APP_CONTROLLER.',')!==false){
 
            }else{
                $action = APP_CONTROLLER.'/'.APP_ACTION;
                if(strpos($_SESSION['admin']['paction'],','.$action.',')===false){
                   $ac = M('Ruler')->find(array('fc'=>$action));
                   if($this->frparam('ajax')){
 
                       JsonReturn(['code'=>1,'msg'=>'您没有【'.$ac['name'].'】的权限!','url'=>U('Index/index')]);
                   }
                   Error('您没有【'.$ac['name'].'】的权限!',U('Index/index'));
                }
            }
        }
 
        $webconf = webConf();
        $this->webconf = $webconf;
        $customconf = get_custom();
        $this->customconf = $customconf;
 
        //插件模板页目录
 
        $this->tpl = '@'.dirname(__FILE__).'/tpl/';
 
        /**
            在下面添加自定义操作
        **/
 
    }
 
    //执行SQL语句在此处处理,或者移动文件也可以在此处理
    public  function install(){
        //下面是新增test表的SQL操作        
        $this->recurse_copy(APP_PATH.'app/admin/exts/smsbao/file/',APP_PATH);
        return true;
 
    }
 
    //卸载程序,对新增字段、表等进行删除SQL操作,或者其他操作
    public function uninstall(){
        if(is_dir(APP_PATH.'smsbao')){
            deldir(APP_PATH.'smsbao');
        }
        return true;
    }
 
    // 原目录,复制到的目录
    function recurse_copy($src,$dst) {  
 
        $dir = opendir($src);
        @mkdir($dst);
        while(false !== ( $file = readdir($dir)) ) {
            if (( $file != '.' ) && ( $file != '..' )) {
                if is_dir($src '/' $file) ) {
                    $this->recurse_copy($src '/' $file,$dst '/' $file);
                }
                else {
                    copy($src '/' $file,$dst '/' $file);
                }
            }
        }
        closedir($dir);
    }
 
    //安装页面介绍,操作说明
    public function desc(){
 
        $this->display($this->tpl.'plugins-description.html');
    }
 
    //配置文件,插件相关账号密码等操作
    public  function setconf($plugins){
        //将插件赋值到模板中
        $this->plugins = $plugins;
        $this->config = json_decode($plugins['config'],1);
 
        $this->display($this->tpl.'plugins-body.html');
    }
 
    //获取插件内提交的数据处理
    public function setconfigdata($data){
 
        M('plugins')->update(['id'=>$data['id']],['config'=>json_encode($data,JSON_UNESCAPED_UNICODE)]);
        setCache('hook',null);//清空hook缓存
 
        JsonReturn(['code'=>0,'msg'=>'设置成功!']);
    }
 
}

2.接着在app\admin\exts\smsbao目录下创建tpl文件夹并创建plugins-body.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {include="style"}
<style>
.layui-form-label {
    width: 230px;
    }
</style>
</head>
<body >
  <div class="layui-rows" style="    margin: 84px;">
    <form class="layui-form" action="">
    <input name="id" value="{$plugins['id']}" type="hidden">
  <div class="layui-form-item">
    <label class="layui-form-label">短信宝账号:</label>
    <div class="layui-input-inline">
      <input type="text" name="username" required  value="{$config['username']}" lay-verify="required" placeholder="请输入短信宝账号" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">还没注册短信宝账号? <a style="color: blue;" target="_blank" href="https://console.smsbao.com/#/register">去注册</a></div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">短信宝APIKEY:</label>
    <div class="layui-input-inline">
      <input type="password" name="apiKey"  value="{$config['apiKey']}"  required lay-verify="required" placeholder="请输入APIKEY" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux"></div>
  </div>
 <div class="layui-form-item">
    <label class="layui-form-label">短信签名:</label>
    <div class="layui-input-inline">
      <input type="text" name="SignName" required  value="{$config['SignName']}" lay-verify="required" placeholder="短信签名" autocomplete="off" class="layui-input">
    </div>
  </div>
   <div class="layui-form-item">
    <label class="layui-form-label">模板内容:</label>
    <div class="layui-input-inline">
      <input type="text" name="templateContent" required  value="{$config['templateContent']}" lay-verify="required" placeholder="模板内容" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">模板示例:您的验证码为: {code},如非本人操作,请忽略本短信</div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
</div>
    <script>
        $(function  () {
            layui.use('form'function(){
              var form = layui.form;
              //监听提交
              form.on('submit(formDemo)'function(data){
                $.post("{fun U('setconf')}",data.field,function(res){
                    //console.log(res);return false;
                     var res = JSON.parse(res);
                     if(res.code==1){
                        layer.msg(res.msg);
                     }else{
                        layer.msg(res.msg, {icon: 6,time: 2000},function(){
                        window.location.reload();
                        });
                     }
                })
                return false;
              });
            });
        })
    </script>
</body>
</html>

3.接着在app\admin\exts\smsbao目录下创建file\smsbao文件夹并创建Sms.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
 
ini_set("display_errors""on");
 
class Sms
{
 
    private static $sendUrl 'http://api.smsbao.com/sms?';
    private static $statusStr array(
                "0" => "短信发送成功",
                "-1" => "参数不全",
                "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
                "30" => "密码错误",
                "40" => "账号不存在",
                "41" => "余额不足",
                "42" => "帐户已过期",
                "43" => "IP地址限制",
                "50" => "内容含有敏感词",
                "51" => "手机号码不正确"
            );
 
    public static $username '';
    public static $apiKey '';
    public static $SignName '';
    public static $templateContent '';
 
    public static function setdata($username,$apiKey,$SignName,$templateContent){
        static::$username $username;
        static::$apiKey $apiKey;
        static::$SignName $SignName;
        static::$templateContent $templateContent;
 
    }
 
    private static function curl_get($sendurl){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $sendurl);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    /**
     * 发送短信
     * @return stdClass
     */
    public static function sendSms($tel,$code) {
         try {
            $statusStr static::$statusStr;
            $user static::$username;
            $apikey static::$apiKey;
            $templateContent str_replace('{code}'$codestatic::$templateContent);
            $content '【'static::$SignName.'】'.$templateContent;
            $sendurl static::$sendUrl."u=".$user."&p=".$apikey."&m=".$tel."&c=".urlencode($content);
            $result static::curl_get($sendurl);
 
            if ($result == 0) {
                return $res['code'] = $result;
            }else{
                $res['code'] = $result;
                $res['msg'] = $statusStr[$result];
                return $res;
            }
        catch (\Exception $error) {
            $res['code'] = '-1';
            $res['msg'] = "发送失败";
            return $res;
        }
 
        return $res['code'] = '0';
 
    }
 
}

4.接着在app\admin\exts\smsbao目录下创建controller\home文件夹并创建MsgController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace app\home\plugins;
 
use app\home\c\CommonController;
 
 
class MsgController extends CommonController
{
 
    function _init(){
        parent::_init();
        $w['filepath'] = 'smsbao';
        $w['isopen'] = 1;
        $res = M('plugins')->find($w);
        if(!$res){
            JsonReturn(['code'=>1,'msg'=>'短信插件未开启!']);
        }
        $res['config'] = json_decode($res['config'],1);
        $this->plugin = $res;
        require_once  APP_PATH.'smsbao/Sms.php';
        \Sms::setdata($res['config']['username'],$res['config']['apiKey'],$res['config']['SignName'],$res['config']['templateContent']);
    }
 
    function index(){
 
        $yzmname $this->frparam('codename',1);
        $yzm $this->frparam('yzm',1);
        if(isset($GLOBALS['Redis'])){
            $code $GLOBALS['Redis']->get($yzmname);
            if(!$yzm || md5(md5($yzm))!=$code){
 
                JsonReturn(['code'=>1,'msg'=>'验证码错误!','data'=>[]]);
            }
 
        }else{
            if(!$yzm || md5(md5($yzm))!=$_SESSION[$yzmname]){
 
                JsonReturn(['code'=>1,'msg'=>'验证码错误!','data'=>[]]);
            }
        }
        $tel $this->frparam('tel',1);
        if(strlen($tel)==11){
 
        }else{
            JsonReturn(['code'=>1,'msg'=>'手机号格式错误!','data'=>[]]);
        }
 
        $code = rand(100000,999999);
        $_SESSION['tel_code'] = $code;
        if(isset($GLOBALS['Redis'])){
            $GLOBALS['Redis']->setex('tel_code',5 * 60,$code);
        }
        $response = \Sms::sendSms($tel,$code);
        //echo "发送短信(sendSms)接口返回的结果:\n";
        //print_r($response);
        if($response['code']=='0'){
            JsonReturn(['code'=>0,'msg'=>'发送成功!','data'=>[]]);
 
        }else{
            JsonReturn(['code'=>1,'msg'=>$response['msg'],'data'=>[]]);
        }
 
    }
 
    function sendmsg($code,$tel){
        if(strlen($tel)==11){
 
        }else{
            JsonReturn(['code'=>1,'msg'=>'手机号格式错误!','data'=>[]]);
        }
 
        $_SESSION['tel_code'] = $code;
        if(isset($GLOBALS['Redis'])){
            $GLOBALS['Redis']->setex('tel_code',5 * 60,$code);
        }
        $response = \Sms::sendSms($tel,$code);
        //echo "发送短信(sendSms)接口返回的结果:\n";
        //print_r($response);
        if($response['code']=='0'){
            JsonReturn(['code'=>0,'msg'=>'发送成功!','data'=>[]]);
 
        }else{
            JsonReturn(['code'=>1,'msg'=>$response['msg'],'data'=>[]]);
        }
    }
}

5.接着在注册、登录页面增加以下前端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#################login.html页面####################
<div class="form-control">
<label for="">手机验证码:</label>
<input type="text" value="" name="telyzm" id="telyzm" placeholder="请输入手机验证码">
<button type="button" onclick="sendmsg()">发送验证码</button>
  </div>
function sendmsg(){
var tel = $("#tel").val();
if(tel==''){
alert('手机号不能为空!');
return false;
}
var vercode = $("#yzm").val();
if(vercode==''){
alert('图形验证码不能为空!');
return false;
}
$.post('/msg/index',{tel:tel,yzm:vercode,codename:'login_vercode'},function(r){
alert(r.msg);
},'json');
}
 
#################register.html页面####################
 
<div class="form-control">
<label for="">手机验证码:</label>
<input type="text" value="" name="telyzm" id="telyzm" placeholder="请输入手机验证码">
<button type="button" onclick="sendmsg()">发送验证码</button>
  </div>
function sendmsg(){
var tel = $("#tel").val();
if(tel==''){
alert('手机号不能为空!');
return false;
}
var vercode = $("#yzm").val();
if(vercode==''){
alert('图形验证码不能为空!');
return false;
}
$.post('/msg/index',{tel:tel,yzm:vercode,codename:'reg_vercode'},function(r){
alert(r.msg);
},'json');
}

经过上面的增加,短信宝的短信平台已经对接成功了,可以正常使用了。

报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。

另外:我们已经开发好完整的极致CMS系统短信宝插件,点击此链接 下载及查看安装流程。

开源插件

最新更新

电商类

CMS类

微信类

文章标签