待发短信

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

4001-021-502

工作时间

9:00-21:00

emlog pro_1.2.2新增短信宝短信接口

emlog是一款基于PHP和MySQL的功能强大的博客及CMS建站系统,追求快速、稳定、简单、舒适的建站体验。小编对这款软件还是比较了解的,小编今天就以新增短信接口为例为大家讲解一下如何进行二次开发,我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
1:打开项目:admin\views\setting.php,以及同目录下的setting_mail.php,setting_seo.php,setting_user.php,blogger.php增加短信设置页面

1
2
3
4
5
6
7
8
        <ul class="nav nav-pills">
            <li class="nav-item"><a class="nav-link" href="./setting.php">基础设置</a></li>
            <li class="nav-item"><a class="nav-link" href="./setting.php?action=user">用户设置</a></li>
            <li class="nav-item"><a class="nav-link" href="./setting.php?action=mail">邮件通知</a></li>
            <li class="nav-item"><a class="nav-link" href="./setting.php?action=seo">SEO优化</a></li>
            <li class="nav-item"><a class="nav-link active" href="./blogger.php">个人信息</a></li>
            <li class="nav-item"><a class="nav-link" href="./setting.php?action=sms">短信通知</a></li>
        </ul>

2:打开项目:admin\views目录新增短信宝设置页面 setting_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php if (!defined('EMLOG_ROOT')) {
    exit('error!');
} ?>
<?php if (isset($_GET['activated'])): ?>
    <div class="alert alert-success">设置保存成功</div><?php endif ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">设置</h1>
</div>
<div class="panel-heading">
    <ul class="nav nav-pills">
        <li class="nav-item"><a class="nav-link" href="./setting.php">基础设置</a></li>
        <li class="nav-item"><a class="nav-link" href="./setting.php?action=user">用户设置</a></li>
        <li class="nav-item"><a class="nav-link " href="./setting.php?action=mail">邮件通知</a></li>
        <li class="nav-item"><a class="nav-link" href="./setting.php?action=seo">SEO优化</a></li>
        <li class="nav-item"><a class="nav-link" href="./blogger.php">个人信息</a></li>
        <li class="nav-item"><a class="nav-link active" href="./setting.php?action=sms">短信通知</a></li>
    </ul>
</div>
<div class="card shadow mb-4 mt-2">
    <div class="card-body">
        <form action="setting.php?action=sms_save" method="post" name="input" id="sms_config">
            <h4>短信发送</h4>
            <div class="form-group form-check">
                <input class="form-check-input" type="checkbox" value="y" name="is_smsup" id="is_smsup" <?= $conf_is_signup ?> />
                <label class="form-check-label">开启短信注册</label>
            </div>
            <div class="form-group">
                <label>短信宝账号</label>
                <input  class="form-control" value="<?= $sms_user ?>" name="sms_user" required>
                <a target="_blank" href="https://console.smsbao.com/#/register" style="color: red">还没有注册短信宝账号!马上注册</a>
            </div>
            <div class="form-group">
                <label>短信宝密码</label>
                <input type="password" name="sms_pwd" cols="" rows="3" class="form-control" value="<?= $sms_pwd ?>" required>
            </div>
            <div class="form-group">
                <label>短信宝签名</label>
                <input class="form-control" value="<?= $sms_sign ?>" name="sms_sign" required>
            </div>
            <div class="form-group">
                <label>注册短信模板</label>
                <input class="form-control" value="<?= $sms_tmp ?>" name="sms_tmp" required>
            </div>
            <div class="form-group">
                <input name="token" id="token" value="<?= LoginAuth::genToken() ?>" type="hidden"/>
                <input type="submit" value="保存设置" class="btn btn-sm btn-success"/>
                <input type="button" value="发送测试" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#testSms"/>
            </div>
            <!-- 设置接收短信的模态框 -->
            <div class="modal fade" id="testSms">
                <div class="modal-dialog modal-sm">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">发送测试</h4>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                        <div class="modal-body">
                            <div class="form-group">
                                <input class="form-control" type="email" name="testTo" placeholder="输入接收手机号">
                            </div>
                        </div>
                        <div class="modal-footer">
                            <div id="testMailMsg"></div>
                            <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-success btn-sm" id="testSendBtn">发送</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    $("#menu_category_sys").addClass('active');
    $("#menu_sys").addClass('show');
    $("#menu_setting").addClass('active');
    setTimeout(hideActived, 2600);
    $("#testSendBtn").click(function () {
        $("#testMailMsg").html("<small class='text-secondary'>发送中...<small>");
        $.post("setting.php?action=sms_test", $("#sms_config").serialize(), function (data) {
            if (data == 0) {
                $("#testMailMsg").html("<small class='text-success'>发送成功</small>");
            else {
                $("#testMailMsg").html(data);
            }
        });
    })
</script>

3:打开admin\views\signup.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
 <form method="post" class="user" action="./account.php?action=dosignup">
                                    <div class="form-group">
                                        <input type="email" class="form-control form-control-user" id="mail"
                                               name="mail" aria-describedby="emailHelp" placeholder="邮箱" required
                                               autofocus>
                                    </div>
                                    <?php if ($is_smsup): ?>
                                        <div class="form-group form-inline">
                                                <input  type="text" id="userPhone" class="form-control form-control-user" placeholder="请输入手机号码">
                                                <button  type="button" class="btn btn-info"  id="second">点击获取验证码</button>
                                        </div>
                                        <div class="form-group">
                                            <input type="text" name="securityCode" class="form-control form-control-user"  placeholder="请输入验证码" required autofocus>
                                        </div>
                                    <?php endif; ?>
                                    <div class="form-group">
                                        <input type="password" class="form-control form-control-user" minlength="6"
                                               id="passwd" name="passwd" placeholder="密码" required>
                                    </div>
                                    <div class="form-group">
                                        <input type="password" class="form-control form-control-user" minlength="6"
                                               id="repasswd" name="repasswd" placeholder="再次输入密码" required>
                                    </div>
                                    <?php if ($login_code): ?>
                                        <div class="form-group form-inline">
                                            <input type="text" name="login_code" class="form-control form-control-user"
                                                   id="login_code" placeholder="验证码" required>
                                            <img src="../include/lib/checkcode.php" id="checkcode" class="mx-2">
                                        </div>
                                    <?php endif ?>
                                    <button class="btn btn-success btn-user btn-block" type="submit">注册</button>
                                    <hr>
                                    <div class="text-center"><a href="/admin">登录</a></div>
                                    <hr>
                                    <div class="text-center"><a href="../" class="small" role="button">&larr;返回首页</a>
                                    </div>
                                </form>

此页面同时也增加有关60秒倒计时的js校验

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
<script type="text/javascript">
    $("#second").click(function (){
        sendyzm($("#second"));
    });
    //用ajax提交到后台的发送短信接口
    function sendyzm(obj){
        var phone = $("#userPhone").val();
        var result = isPhoneNum();
        if(result) {
            $.ajax({
                url:"/admin/account.php?action=smsYzm&phone="+phone,
                type:"get",
                processData: false,
                contentType: false,
                success:function(data){
                    if (data == 0) {
                        setTime(obj);
                    }else if (data == 1){
                        alert('验证码发送频繁')
                    }else{
                        alert('验证码发送失败')
                    }
                },
                error:function(){
                    alert("验证码发送失败")
                }
            })
        }
    }
    //60s倒计时实现逻辑
    var countdown = 60;
    function setTime(obj) {
        if (countdown == 0) {
            obj.prop('disabled'false);
            obj.text("点击获取验证码");
            countdown = 60;//60秒过后button上的文字初始化,计时器初始化;
            return;
        else {
            obj.prop('disabled'true);
            obj.text("("+countdown+"s)后重新发送") ;
            countdown--;
        }
        setTimeout(function() { setTime(obj) },1000) //每1000毫秒执行一次
    }
    //校验手机号是否合法
    function isPhoneNum(){
        var phonenum = $("#userPhone").val();
        var reg = /^1[34578]\d{9}$/;
        if(!reg.test(phonenum)){
            alert('请输入有效的手机号码!');
            return false;
        }else{
            return true;
        }
    }
</script>

4:打开admin\account.php修改signup注册方法

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
if ($action == 'signup') {
    loginAuth::checkLogged();
    $login_code = Option::get('login_code') === 'y';
    $is_smsup = Option::get('is_smsup') === 'y';
    $error_msg = '';
    if (Option::get('is_signup') !== 'y') {
        emMsg('系统已关闭注册!');
    }
    $page_title = '注册账号';
    include View::getAdmView('user_head');
    require_once View::getAdmView('signup');
    View::output();
}
if ($action == 'smsYzm') {
    $data = [];
    //做检测
    $phone = isset($_GET['phone']) ? addslashes(trim($_GET['phone'])) : '';
    if (!isset($_SESSION)) {
        session_start();
    }
    if (isset($_SESSION['tel']) && time()-$_SESSION['tel'] < 60){
        echo 1;
        exit;
    }
    $login_code = isset($_POST['login_code']) ? addslashes(strtoupper(trim($_POST['login_code']))) : ''//登录注册验证码
    if (!User::checkLoginCode($login_code)) {
        emDirect('./account.php?action=signup&err_ckcode=1');
    }
    $rand = mt_rand(0,9999);
    $test_temp = Option::get('sms_tmp') ;
    $content = str_replace('{$code}',$rand,$test_temp);
    $res = (new SmsBao())->send($phone,$content);
//    $res = '发送成功';
    if ($res != "发送成功"){
        echo $res;
      exit;
    }else{
        if (!isset($_SESSION)) {
            session_start();
        }
        $_SESSION['tel'] = time();
        $_SESSION['sms_yzm_code'] = $rand;
        echo 0;
        exit;
    }
}

5:打开admin\setting.php新增sms、sms_test、sms_save方法

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
if ($action == 'sms') {
    $options_cache = $CACHE->readCache('options');
    $sms_user = $options_cache['sms_user'];
    $sms_pwd = $options_cache['sms_pwd'];
    $sms_sign = $options_cache['sms_sign'];
    $sms_tmp = $options_cache['sms_tmp'];
    $conf_is_signup = $options_cache['is_smsup'] == 'y' 'checked="checked"' '';
    include View::getAdmView('header');
    require_once(View::getAdmView('setting_sms'));
    include View::getAdmView('footer');
    View::output();
}
if ($action == 'sms_test') {
    $data = [
        'testTo'      => $_POST['testTo'] ?? '',
    ];
    $test_temp = '您的验证码为1234';
    try {
//       $res = (new SmsBao())->send($data['testTo'],$test_temp);
       $res = '发送成功';
        if ($res != "发送成功"){
            exit("<small class='text-danger'>$res</small>");
            return false;
        }
    catch (Exception $exc) {
        exit("<small class='text-danger'>发送失败</small>");
        return false;
    }
}
if ($action == 'sms_save') {
    LoginAuth::checkToken();
    $data = [
        'is_smsup'   => isset($_POST['is_smsup']) ? addslashes($_POST['is_smsup']) : '',
        'sms_user'   => isset($_POST['sms_user']) ? addslashes($_POST['sms_user']) : '',
        'sms_pwd'     => isset($_POST['sms_pwd']) ? addslashes($_POST['sms_pwd']) : '',
        'sms_tmp'     => isset($_POST['sms_tmp']) ? addslashes($_POST['sms_tmp']) : '',
        'sms_sign' => isset($_POST['sms_sign']) ? addslashes($_POST['sms_sign']) : '',
    ];
    foreach ($data as $key => $val) {
        Option::updateOption($key, $val);
    }
    $CACHE->updateCache(array('options'));
    header('Location: ./setting.php?action=sms&activated=1');
}

6:最后打开项目include\lib目录新增smsbao.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
<?php
class SmsBao {
    public $sms_user;
    public $sms_pwd;
    public $sms_sign;
    public function __construct() {
        $this->sms_user = Option::get('sms_user');
        $this->sms_pwd = Option::get('sms_pwd');
        $this->sms_sign = Option::get('sms_sign');
    }
    function send($to,$content) {
        try {
            $statusStr = array(
                "0" => "发送成功",
                "-1" => "参数不全",
                "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
                "30" => "密码错误",
                "40" => "账号不存在",
                "41" => "余额不足",
                "42" => "帐户已过期",
                "43" => "IP地址限制",
                "50" => "内容含有敏感词"
            );
            $smsapi = "http://api.smsbao.com/";
            $user = $this->sms_user; //短信平台帐号
            $pass = md5($this->sms_pwd); //短信平台密码
            $content="【".$this->sms_sign."】".$content;//要发送的短信内容
            $phone = $to;//要发送短信的手机号码
            $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
            $res = file_get_contents($sendurl);
            return $statusStr[$res];
        catch (Exception $exc) {
            return false;
        }
    }
}

好了,经过以上的替换,短信宝的短信平台已经替换成功了,可以正常使用了。我们进行测试发送。

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

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

开源插件

最新更新

电商类

CMS类

微信类

文章标签