• 4001-021-502
您的位置:短信宝 > 技术分享 > UQCMS云商系统新增短信宝短信接口
UQCMS云商系统新增短信宝短信接口
发表日期:2018-08-10    文章编辑:短信宝小编    浏览次数:

UQCMS云商是一款B2B2C电子商务软件 ,采用PHP+MYSQL,模板采用smarty模板,二次开发,简单方便,无需学习其他框架就可以自行模板设计。永久免费使用,操作简单,安全稳定。今天小编一步一步教大家如何开发,进行替换的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信接口非常稳定,发送速度快,注册还送测试短信,推荐大家使用。

首先我们在项目\home\controls\目录下创建smsbao.class.php文件,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
class smsbao_uqcms extends control{
 public function index() {
  $sql[] =  "insert into ".table('api_sms')." (`id`,`alias`,`name`,`describe`,`app_key`,`app_secret`,`sign`,`status`,`left_delimiter`,`right_delimiter`) values ('','smsbao','短信宝短信','短信宝短信:http://www.smsbao.com/','','','','0','{','}')";
  $sql[] = "insert into ".table('api_notice_tpl')." (`id`, `nid`, `type`, `type2`, `name`,`tpl_id`, `param`, `param2`,`content`,`status`) values ('','1','sms','smsbao','验证码短信','','smscode','code','你的验证码为:{code} 为保证您的账户安全,请勿泄露.','1')";
  foreach ($sql as $val) {
   if (!$query $query=$this->db->query($val)) {
    echo '执行sql语句成功';
    exit;
   }
  }
  echo "<h4>UQCMS短信宝短信插件安装成功,请删除此文件。</h4>";
 }
}

接着我们打开项目\module\api.mod.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
class api_mod extends module {
    function __construct() {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    function notice($type$tpl_name$arr) {
        $query $this->notice_sms($row['username'], $row['tpl_name'], $arr);
        if ($type == 'sms' || $type == 'email' || $type == 'wxtpl') {
            if (!empty($this->uid)) {$setarr['uid'] = $this->uid;
            else { $setarr['uid'] = '0'; }
            $setarr['type'] = $type;
            $setarr['tpl_name'] = $tpl_name;
            $setarr['username'] = $arr['username'];
            unset($arr['username']);
            $setarr['content'] = serialize($arr);
            $setarr['addtime'] = time();
            $query $this->db->add(table('notice'), $setarr);
            if ($query) {
                $insert_id $this->db->insert_id();
                curl::asynGET("index.php?m=ajax&a=notice_run&id=" $insert_id);
            }
        else {   return false; }
    }
    function notice_run($id) {
        $row $this->db->get_one("select * from " . table('notice') . " where status = 0 and id = " $id);
        if ($row['id']) {
            $arr = unserialize($row['content']);
            if ($row['type'] == 'sms') {
                $query $this->notice_sms($row['username'], $row['tpl_name'], $arr);
            elseif ($row['type'] == 'wxtpl') {
                $query $this->notice_wxtpl($row['username'], $row['tpl_name'], $arr);
            elseif ($row['type'] == 'email') {
                $query $this->notice_email($row['username'], $row['tpl_name'], $arr);
            }
            if ($query['error'] == '0') {
                $starr['status'] = '1';
                $query $this->db->update(table('notice'), $starr'id = ' $id);
                if ($query) {
                    return true;
                else {
                    return false;
                }
            else {
                $starr['status'] = '2';
                $starr['remark'] = $query['msg'];
                $query $this->db->update(table('notice'), $starr'id = ' $id);
                if ($query) {
                    return true;
                else {
                    return false;
                }
            }
        }
    }
    function notice_sms($mobile$tpl_name$arr) {
        $sms_row $this->sms_row();
        $wsql " and type = 'sms' and type2 = '" $sms_row['alias'] . "'";
        $an_row $this->db->get_one("select * from " . table("api_notice") . " where alias = '" $tpl_name "' limit 1");
        if ($an_row) {
            $tpl_row $this->db->get_one("select * from " . table("api_notice_tpl") . " where nid = '" $an_row['id'] . "' " . $wsql " limit 1");
        }
        $class_name 'sms_' $sms_row['alias'];
        $sms new $class_name($sms_row['app_key'], $sms_row['app_secret']);
        $aaa explode(','$tpl_row['param']);
        $aaa2 explode(','$tpl_row['param2']);
        foreach ($aaa2 as $kk => $vv) {$parr[$vv] = $arr[$aaa[$kk]];}
        if ($sms_row['sign'] == '' || $sms_row['app_key'] == '' || $sms_row['app_secret'] == '') {
            return array('error' => '1''msg' => $sms_row['alias'] . 'APP KEY, APP SECRET, SIGN签名不能为空');
        else {
            if (isset($parr)) {
                if ($sms_row['alias'] == 'smsbao') {
                    $exec $sms->send($mobile,$parr,$tpl_row['content'],$sms_row['sign']);
                }else{
                $exec $sms->send($mobile$parr$tpl_row['tpl_id'], $sms_row['sign']);                    
                }
                if ($exec['error'] == '0') {
                    return array('error' => '0''msg' => '发送成功');
                else {
                    return array('error' => '1''msg' => $exec['msg']);
                }
            else {
                return array('error' => '1''msg' => '系统提交参数错误');
            }
        }
    }
    function notice_email($email$tpl_name$arr) {
        $wsql " and type = 'email'";
        $an_row $this->db->get_one("select * from " . table("api_notice") . " where alias = '" $tpl_name "' limit 1");
        if ($an_row) {
            $tpl_row $this->db->get_one("select * from " . table("api_notice_tpl") . " where nid = '" $an_row['id'] . "' " . $wsql " limit 1");
        }
        $aaa explode(','$tpl_row['param']);
        $aaa2 explode(','$tpl_row['param2']);
        $content $tpl_row['content'];
        foreach ($aaa2 as $kk => $vv) {
            $value = @$arr[$aaa[$kk]];
            $content str_replace('{' $vv '}'$value$content);
        }
        $query $this->email($email$tpl_row['name'], $content);
        if ($query) {
            return array('error' => '0''msg' => '发送成功');
        else {
            return array('error' => '1''msg' => '发送失败');
        }
    }
    function notice_wxtpl($openid$tpl_name$arr) {
        $wsql " and type = 'wxtpl'";
        $an_row $this->db->get_one("select * from " . table("api_notice") . " where alias = '" $tpl_name "' limit 1");
        if ($an_row) {
            $tpl_row $this->db->get_one("select * from " . table("api_notice_tpl") . " where nid = '" $an_row['id'] . "' " . $wsql " limit 1");
        }
        $aaa explode(','$tpl_row['param']);
        $aaa2 explode(','$tpl_row['param2']);
        preg_match_all('#{([^}]*)#i'$tpl_row['content'], $matches);
        $aaaa $matches[1];
        foreach ($aaa2 as $kk => $vv) {
            $dat[$vv] = array('value' => isset($arr[$aaa[$kk]]) ? $arr[$aaa[$kk]] : $aaaa[$kk], 'color' => '#666666');
        }
        $data['touser'] = $openid;
        $data['url'] = $arr['link'];
        $data['template_id'] = $tpl_row['tpl_id'];
        $data['data'] = $dat;
        $url 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' $this->module('login_wchat')->access_token();
        $postdata = (object)$data;
        $json_data = urldecode(json_encode($postdata));
        $result = curl::post($url$json_data);
        $result = json_decode($result);
        if ($result->errcode == '0') {
            return array('error' => '0''msg' => '发送成功');
        else {
            return array('error' => '1''msg' => $result->errmsg);
        }
    }
    function notice_list($tpl_name$cname) {
        $notice_name "config/api_notice_" $tpl_name;
        $cache_arr = cache::get($notice_name);
        if (!$cache_arr) {
            $row $this->db->get_one("select * from " . table('api_notice') . " where alias = '" $tpl_name "'");
            $query $this->db->get_all("select * from " . table('api_notice_tpl') . " where nid = " $row['id'] . " and type = " $cname " and status = 1");
            if ($query) {
                foreach ($query as $k => $v) {
                    $data[] = $v;
                }
                if ($data) {
                    cache::add($notice_name$data);
                else {
                    $data array();
                    cache::add($notice_name$data);
                }
                return $data;
            else {
                return false;
            }
        else {
            return $cache_arr;
        }
    }
    function sms_row() {
        $sms_name "config/api_sms";
        $cache_arr = cache::get($sms_name);
        if (!$cache_arr) {
            $sms_row $this->db->get_one("select * from " . table('api_sms') . " where status = 1");
            cache::add($sms_name$sms_row);
            return $sms_row;
        else {
            return $cache_arr;
        }
    }
    function login_list($arr) {
        $cache_arr = cache::get("config/api_login");
        if (!$cache_arr) {
            $cache_arr $this->db->get_all("select * from " . table('api_login') . " where status = 1");
            if ($cache_arr) {
                cache::add("config/api_login"$cache_arr);
            }
        }
        $user_status = isset($arr['user_status']) ? $arr['user_status'] : false;
        if ($user_status) {
            $row $this->db->get_one("select uid,qq_uid,weibo_uid,wchat_uid from " . table('member') . " where uid = " $this->uid);
            foreach ($cache_arr as $k => $v) {
                $apiname $v['alias'] . '_uid';
                if ($row[$apiname]) {
                    $v['bind_status'] = '1';
                }
                $data[] = $v;
            }
            return $data;
        else {
            return $cache_arr;
        }
    }
    function email($from_to$subject = null, $body = null, $attachment = null) {
        require UQCMS_PATH . 'UQframework/phpmailer/class.phpmailer.php';
        $mail new PHPMailer();
        $mail->CharSet = 'UTF-8';
        $mail->IsSMTP();
        $mail->SMTPDebug = 0;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->Host = cfg('email_host');
        $mail->Port = cfg('email_port');
        $mail->Username = cfg('email_username');
        $mail->Password = cfg('email_password');
        $mail->SetFrom(cfg('email_username'), cfg('email_name'));
        $mail->Subject = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($from_to);
        if ($mail->Send()) {
            return true;
        else {
            return false;
        }
    }
    public function notice_type($str = null) {
        $arr array('wxtpl' => '微信模板''sms' => '短信通知''email' => '邮件通知',);
        if ($str) {
            return $arr[$str];
        else {
            return $arr;
        }
    }
}

接着打开项目\UQframework\core\common2.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
function error_404($msg) {
    header('HTTP/1.1 404 Not Found');
    header("status: 404 Not Found");
    $file_404 = UQCMS_PATH . 'public/404.html';
    if (file_exists($file_404)) {
        include $file_404;
    else {
        exit($msg);
    }
    exit;
}
function db() {
    static $dbconnect array();
    if (isset($dbconnect['db'])) {
        return $dbconnect['db'];
    else {
        $file = UQCMS_PATH . 'UQframework/core/db.class.php';
        if (file_exists($file)) {
            require ($file);
        }
        $db_cfg require_once UQCMS_PATH . 'data/dbconfig.php';
        $dbconnect['db'] = new dbi($db_cfg);
        $dbconnect['db']->conn();
        return $dbconnect['db'];
    }
}
function autoload($class) {
    $libs array('file_cache' => 'cache/file_cache''mem_cache' => 'cache/mem_cache''login_qq' => 'api/login_qq''login_weibo' => 'api/login_weibo''login_wchat' => 'api/login_wchat''oss_qiniu' => 'api/oss_qiniu''oss_aliyun' => 'api/oss_aliyun''oss_qcloud' => 'api/oss_qcloud''sms_aliyun' => 'api/sms_aliyun''sms_qcloud' => 'api/sms_qcloud''pay_alipay' => 'api/pay_alipay''pay_wxpay' => 'api/pay_wxpay''wchat' => 'api/wchat','sms_smsbao'=>'api/sms_smsbao');
    $file = UQCMS_PATH . 'UQframework' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . $class '.class.php';
    if (file_exists($file)) {
        include_once ($file);
    else {
        if (isset($libs[$class])) {
            $select_file = UQCMS_PATH . 'UQframework' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . $libs[$class] . '.class.php';
            if (file_exists($select_file)) {
                include_once ($select_file);
            else {
                error_404('autoload加载错误, ERROR:' $class);
            }
        else {
            error_404('autoload加载错误, ERROR:' $class);
        }
    }
}
function cfg($str = null) {
    static $cfg array();
    if (isset($cfg['cfg'])) {
        $cfg['cfg'] = $cfg['cfg'];
    else {
        $file = UQCMS_PATH . 'data' . DIRECTORY_SEPARATOR . 'site_configs.php';
        if (file_exists($file)) {
            $cfg['cfg'] = require_once $file;
        else {
            $cfglist = db()->get_all("select * from " . table('config'));
            if ($cfglist) {
                foreach ($cfglist as $k => $v) {
                    $cfg['cfg'][$v['name']] = $v['value'];
                }
            }
        }
        $develop_file = UQCMS_PATH . 'UQframework/core/development.php';
        if (file_exists($develop_file)) {
            $cfg require_once $develop_file;
        else {
            error_reporting(0);
            $cfg['cfg']['site_local'] = false;
        }
        $UQCMS_PATH str_replace('\\''/', UQCMS_PATH);
        $site_path substr($UQCMS_PATHstrlen($_SERVER['DOCUMENT_ROOT']), strlen($UQCMS_PATH));
        $site_path = ltrim($site_path'/');
        if ($site_path == '' || $site_path == '/') {
            $cfg['cfg']['site_path'] = '/';
            $cfg['cfg']['site_url'] = 'http://' $_SERVER['HTTP_HOST'] . '/';
        else {
            if (isset($cfg['cfg']['site_addr'])) {
                $cfg['cfg']['site_path'] = '/' $site_path;
                $cfg['cfg']['site_url'] = 'http://' $_SERVER['HTTP_HOST'] . '/' $site_path;
            else {
                $cfg['cfg']['site_path'] = '/';
                $cfg['cfg']['site_url'] = 'http://' $_SERVER['HTTP_HOST'] . '/';
            }
        }
        $request_addr $_SERVER['REQUEST_URI'];
        $request_url = urlencode('http://' $_SERVER['HTTP_HOST'] . $request_addr);
        $cfg['cfg']['request_url'] = $request_url;
        $user_agent $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent'MicroMessenger') === false) {
            if (is_mobile()) {
                $cfg['cfg']['browser'] = 'wap';
            else {
                $cfg['cfg']['browser'] = 'pc';
            }
        else {
            $cfg['cfg']['browser'] = 'wchat';
        }
    }
    if ($str == null) {
        return $cfg['cfg'];
    else {
        if (isset($cfg['cfg'][$str])) {
            return $cfg['cfg'][$str];
        else {
            return false;
        }
    }
}
function _browser_pid($position) {
    $pst array('pc' => '1''wap' => '2''wchat' => '3');
    if (isset($pst[$position])) {
        return $pst[$position];
    else {
        $bname = cfg('browser');
        return $pst[$bname];
    }
}
function mod($module_name) {
    static $mod_class array();
    if (isset($mod_class[$module_name])) {
        return $mod_class[$module_name];
    else {
        $file_mod = UQCMS_PATH . 'module/' $module_name ".mod.php";
        if (file_exists($file_mod)) {
            require_once ($file_mod);
            $modExt $module_name '_mod';
            $mod_class[$module_name] = new $modExt();
        else {
            echo '读取的模块不纯在!';
        }
    }
    return $mod_class[$module_name];
}
function error_json($msg$name = null) {
    if (!empty($name)) {
        $arr array('error' => '1''msg' => $msg'name' => $name);
    else {
        $arr array('error' => '1''msg' => $msg);
    }
    $arrjson = json_encode($arr);
    exit($arrjson);
}
function right_json($arr1 array()) {
    $arr2 array('error' => '0');
    if (!empty($arr1)) {
        $arr array_merge($arr1$arr2);
    else {
        $arr $arr2;
    }
    $arrjson = json_encode($arr);
    exit($arrjson);
}
function sys_log($msg) {
    file_put_contents(UQCMS_PATH . 'temp/log/sys.txt'$msg . PHP_EOL, FILE_APPEND);
}
function db_log($sql) {
    file_put_contents(UQCMS_PATH . 'temp/log/mysql.txt'$sql . PHP_EOL, FILE_APPEND);
}
function img_host() {
    if (isset($imgArr)) {
        return $imgArr;
    else {
        $file = UQCMS_PATH . 'temp/config/imgHost.php';
        if (file_exists($file)) {
            $imgArr require_once $file;
        else {
            $imgArr = img_config();
        }
        return $imgArr['host'];
    }
}
function img_size($size$string = null) {
    if (isset($string_link[$size])) {
        return $string_link[$size];
    else {
        $imgArr = img_config();
        if ($imgArr['alias'] == '') {
            if ($string) {
                return $string_link[$size] = $string;
            else {
                return $string_link[$size] = '';
            }
        else {
            $sizeArr explode('x'$size);
            $ossname 'oss_' $imgArr['alias'];
            $conn_link $ossname::img_size($sizeArr[0], $sizeArr[1]);
            if ($string) {
                return $string_link[$size] = $string $conn_link;
            else {
                return $string_link[$size] = $conn_link;
            }
        }
    }
}
function img_config() {
    $file = UQCMS_PATH . 'temp/config/imgOss.php';
    if (file_exists($file)) {
        $imgArr include $file;
    else {
        $row = db()->get_one("select id,alias,host from " . table('api_oss') . " where status = 1");
        if ($row['host']) {
            $imgArr['alias'] = $row['alias'];
            $imgArr['host'] = '//' $row['host'] . '/';
        else {
            $imgArr['alias'] = '';
            $imgArr['host'] = '';
        }
        file_put_contents('temp/config/imgOss.php'"<?php \r\n return " . var_export($imgArr, true) . ";");
    }
    return $imgArr;
}
function P($name$msg ''$default_value '') {
    if ($msg === false || $msg === 0) {
        return !empty($_POST[$name]) ? trim($_POST[$name]) : false;
    else {
        if ($default_value || $default_value == '0') {
            return !empty($_POST[$name]) ? trim($_POST[$name]) : $default_value;
        else {
            if (empty($msg)) {
                return !empty($_POST[$name]) ? trim($_POST[$name]) : '';
            else {
                return !empty($_POST[$name]) ? trim($_POST[$name]) : error_json($msg$name);
            }
        }
    }
}
function _date($time) {
    if ($time == '0' || $time == '') {
    else {
        return date('Y-m-d H:s'$time);
    }
}

最后我们在项目\UQframework\class\api\下创建sms_smsbao.class.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
<?php
 
class sms_smsbao extends module
{
 
 function __construct($app_id,$app_key)
 {
  $this->app_id = $app_id;
  $this->app_key = $app_key;
  $this->url = 'http://api.smsbao.com/sms?';
 }
 
 function send($mobile,$param_arr,$content,$sign=null)
 {
 
  foreach ($param_arr as $key => $value) {
   $content str_replace("{".$key."}"$value$content);
  }
  $contents "【".$sign."】".$content
  
  $url $this->url.'u='.$this->app_id.'&p='.md5($this->app_key).'&m='.$mobile.'&c='.$contents;
 
 
        if (function_exists('file_get_contents')) {
            $ret file_get_contents($url);
        }else{
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $ret = curl_exec($ch);
            curl_close($ch);
        }
 
        if ($ret == 0) {
         $result_info['error'] = '0';
         $result_info['msg'] = '发送成功';
         return $result_info;
        }else{
         $result_info['error'] = '1';
         $result_info['msg'] = $ret;
         return $result_info;
        }
 
 
 
 }
 
 
 private static $instance;
    public static function init() {
        if (is_null(self::$instance)) {
            self::$instance new self();
        }
        return self::$instance;
    }
}

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

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

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

短信宝-做更好用的短信验证码短信营销短信群发服务平台。超过9,000个网站始终信任短信宝。通过稳定快捷的短信,短信宝帮助他们缩短了90%的人工服务处理时间,同时降低了81%由于通讯故障导致的废单率,一切变得轻松可控!

Copyright © 2010-2014 smsbao.com All Rights Reserved
上海子橙电子科技有限公司 沪ICP备14008182号-2 上海市松江区广富林路658弄215号

展开