帝国CMS是基于B/S结构,安全、稳定、强大、灵活的网站管理系统.还提供了系统扩展框架。小编对这款系统还比较了解,今天就以替换短信接口为例为大家讲解一下如何进行二次开发,我们今天讲解的是7.5版本,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用!
打开项目upload/e/admin创建一个smsbao文件夹,在smsbao文件夹下创建一个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
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
|
<?php header( 'Content-Type:text/html;charset=UTF-8' ); /** * SmsBao实现类 * @category 帝国cms * @package 帝国cmsV7.2 * @subpackage Sms * @author linf */ class Sms{ private $account ; //短信宝账户 private $password ; //密码 private $balance ; //短信剩余条数 /** * 架构函数 * @access public * @param string $account 在短信宝注册的账户名 * @param string $password 在短信宝注册的账户名的密码 */ public function __construct( $account , $password ) { if ( empty ( $account ) || empty ( $password )) { echo "用户名和密码不可为空!" ; } $this ->account = $account ; $this ->password = $password ; } /** * 发送短信函数 * @access public * @param string $mobile 手机号,多个手机号用英文逗号分隔 * @param string $content 发送内容 * @return array 返回值为数组,其中status为0表明发送成功,其他情况下发送失败,失败原因为msg */ public function sendSms( $mobile , $content ){ $param [ 'u' ] = $this ->account; $param [ 'p' ] = md5( $this ->password); $param [ 'm' ] = $mobile ; $param [ 'c' ] = $content ; //$param['c'] = iconv('UTF-8', 'GBK2312//IGNORE', $content); $ret = self::http(self::sendSmsUrl, $param ); //$data['status'] = $ret; $data = $ret == 0 ? '0' : self::getResult( $ret ); return $data ; } /** * 获取短信剩余条数函数 * @access public */ public function getBalance(){ $param [ 'u' ] = $this ->account; $param [ 'p' ] = md5( $this ->password); $ret = self::http(self::queryBalanceUrl, $param ); $retArr = split( "\n" , $ret ); $balanceArr = split( "," , $retArr [1]); $this ->balance = $retArr [0] == 0 ? $balanceArr [1] : self::getResult( $ret ); return $this ->balance; } /** * 发送http请求 * @access protected * @param string $url 请求地址 * @param string $param get方式请求内容,数组形式,post方式时无效 * * @param string $data post请求方式时的内容,get方式时无效 * @param string $method 请求方式,默认get */ protected static function http( $url , $param , $data = '' , $method = 'GET' ){ $opts = array ( CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, ); /* 根据请求类型设置特定参数 */ $opts [CURLOPT_URL] = $url . '?' . http_build_query( $param ); if ( strtoupper ( $method ) == 'POST' ){ $opts [CURLOPT_POST] = 1; $opts [CURLOPT_POSTFIELDS] = $data ; if ( is_string ( $data )){ //发送JSON数据 $opts [CURLOPT_HTTPHEADER] = array ( 'Content-Type: application/json; charset=utf-8' , 'Content-Length: ' . strlen ( $data ), ); } } /* 初始化并执行curl请求 */ $ch = curl_init(); curl_setopt_array( $ch , $opts ); $data = curl_exec( $ch ); $error = curl_error( $ch ); curl_close( $ch ); //发生错误,抛出异常 if ( $error ) throw new Exception( '请求发生错误:' . $error ); return $data ; } private function getResult( $key ){ $rst [ '30' ] = '密码错误' ; $rst [ '40' ] = '账号不存在' ; $rst [ '41' ] = '余额不足' ; $rst [ '42' ] = '帐号过期' ; $rst [ '43' ] = 'IP地址限制' ; $rst [ '50' ] = '内容含有敏感词' ; $rst [ '51' ] = '手机号码不正确' ; return $rst [ $key ]; } } ?> |
然后我们修改前端的模板页面,打开项目upload/e/template/member/register.php文件,修改63~72行左右的代码:
1
2
3
4
5
6
7
8
9
10
11
|
<tr> <td height= "25" bgcolor= "#FFFFFF" > <div align= 'left' >手机号码</div></td> <td height= "25" bgcolor= "#FFFFFF" > <input name= 'phone' type= 'text' id= 'phone' maxlength= '50' value= "<?=$ecmsfirstpost==1?" ":ehtmlspecialchars(stripSlashes($addr[phone]))?>" > *</td> </tr> <tr> <td height= "25" bgcolor= "#FFFFFF" > <div align= 'left' >验证码</div></td> <td height= "25" bgcolor= "#FFFFFF" > <input name= 'rzm' type= 'text' id= 'rzm' maxlength= '30' > *<input type= "button" style= "font-size: 12px; height: 22px; line-height: 19px;" value= "发送验证码" onclick= "sendrzm()" id= "sendag" ></td> </tr> |
再引入jquery文件添加jquery代码:
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
|
<script src= "/js/jquery.min-1.7.2.js" ></script> <SCRIPT language=javascript> <!-- var secs = 120; function sendrzm(){ var tel=$( "#phone" ).val(); <? if ( $public_r [ 'regkey_ok' ]) { ?> if ($( '#key' ).val()== '' ||$( '#key' ).val().length<4){ alert( '请先填写图形验证码' ); return false; } <? } ?> if ($.trim(tel)== '' ||$.trim(tel).length!=11){ alert( '请填写正确的手机号码' ); } else { $.getJSON( '/e/member/doaction.php?enews=Rzsj&phone=' + tel + '' , function (data) { if (data.d== '2' ){ alert(data.n); } else { document.userinfoform.sendag.disabled=true; for (i=1;i<=secs;i++) { window.setTimeout( "update(" + i + ")" , i * 1000); } } }); } } function update(num) { if (num == secs) { document.userinfoform.sendag.value = "重新发送验证码" ; document.userinfoform.sendag.disabled=false; } else { printnr = secs-num; document.userinfoform.sendag.value = "(" + printnr + ")重新发送验证码" ; } } //--> </SCRIPT> |
修改项目upload/e/member/doaction.php文件的177~185行左右添加认证手机代码:
1
2
3
4
5
6
7
8
9
|
elseif ( $enews == 'Rzsj' ) //认证手机号码 { $tel = $_GET [ 'phone' ]; F_rzm( $tel ); } elseif ( $enews == 'Rzsjq' ) //认证手机号码取回密码 { $add = $_GET ; F_rzmq( $add ); } |
添加完成之后,我们修改upload/e/class/connet.php文件中的4958~5213行左右,添加短信接口验证代码:
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
//发送手机验证码 function F_rzm( $tel ){ global $empire , $dbtbpre , $public_r ; $ism =is_mobile( $tel ); if ( $ism == 2) { G_return( "手机号码为空或者错误!" ); } $time =time(); $zrc = $empire ->fetch1( "select * from {$dbtbpre}enewsmemberadd where phone='$tel' limit 1" ); if ( $zrc [ 'userid' ]){ G_return( "该号码注册,请更换其他号码!" ); } //识别码 $gzwc =RepPostVar(getcvar( 'gzwrz' )); $zr = $empire ->fetch1( "select * from {$dbtbpre}rz where bsm='$gzwc' and sj='$tel' limit 1" ); if ( $zr [ 'id' ]){ //验证次数 if ( $zr [ 'c' ]>10) { G_return( "该号码已多次接收不了验证码,建议更换手机重新注册!" ); } //验证间隔时间 $gtime2 = $zr [ 't' ]+60*2; if ( $time < $gtime2 ) { G_return( "发送过程中,手机接收验证码需要1-2分钟,请耐心等待!" ); } $rm =no_make_password(6); $content = $rm ; $fh =api_sendsms( $tel , $content ); if ( $fh ){ $sql = $empire ->query( "update {$dbtbpre}rz set t='$time',rzm='$rm',c=c+1 where id='$zr[id]'" ); $logincookie = $time +3600*2; $set1 =esetcookie( "gzwrz" , $gzwc , $logincookie ); G_return( "验证码已发送成功,请查收!" ,1); } else { G_return( "发送失败,请联系管理员!" ); } } else { $rm =no_make_password(6); $content = $rm ; $fh =api_sendsms( $tel , $content ); if ( $fh ){ $gzwc =make_password(20); $sql = $empire ->query( "insert into {$dbtbpre}rz(bsm,rzm,sj,t,c) values('$gzwc','$rm','$tel','$time','1');" ); $logincookie = $time +3600*2; $set1 =esetcookie( "gzwrz" , $gzwc , $logincookie ); G_return( "验证码已发送成功,请查收!" ,1); } else { G_return( "发送失败,请联系管理员!" ); } } } //发送手机验证码(找回密码)function F_rzmq($add){ global $empire , $dbtbpre , $public_r ; $tel = $add [ 'phone' ]; //if(!$public_r['opengetpass']) //{ // G_return("系统关闭取回密码!"); //} $username =trim( $add [username]); if (! $username ) { G_return( "用户名不能为空!" ); } $ism =is_mobile( $tel ); if ( $ism == 2) { G_return( "手机号码为空或者错误!" ); } $username =RepPostVar( $username ); $ur = $empire ->fetch1( "select " .eReturnSelectMemberF( 'userid,username' ). " from " .eReturnMemberTable(). " where " .egetmf( 'username' ). "='$username' limit 1" ); if (! $ur [ 'userid' ]) { G_return( "该用户不存在!" ); } $aur = $empire ->fetch1( "select phone from {$dbtbpre}enewsmemberadd where userid='" . $ur ['userid ']."' limit 1"); if ( $tel != $aur [ 'phone' ]) { G_return( "手机号码不正确!" ); } $time =time(); //识别码 $gzwc =RepPostVar(getcvar( 'gzwrzq' )); $zr = $empire ->fetch1( "select * from {$dbtbpre}rzq where sj='$tel' and username='" . $username . "' limit 1" ); if ( $zr [ 'id' ]){ //时间判断 $gtime1 = date ( "Y-m-d" , $zr [ 't' ]); $gddd = date ( "Y-m-d" ,time()); //验证次数 if ( $zr [ 'c' ]>10&& $gtime1 == $gddd ) { G_return( "该号码已多次接收不了验证码,建议更换手机重新注册!" ); } //验证间隔时间 $gtime2 = $zr [ 't' ]+60*2; if ( $time < $gtime2 ) { G_return( "发送过程中,手机接收验证码需要1-2分钟,请耐心等待!" ); } $rm =no_make_password(6); $content = $rm ; $fh =api_sendsms( $tel , $content ); if ( $fh ){ if ( $gtime1 != $gddd ){ $sql = $empire ->query( "update {$dbtbpre}rzq set t='$time',rzm='$rm',c=c+1 where id='$zr[id]'" ); } else { $sql = $empire ->query( "update {$dbtbpre}rzq set t='$time',rzm='$rm',c=1 where id='$zr[id]'" ); } $logincookie = $time +3600*2; $set1 =esetcookie( "gzwrzq" , $gzwc , $logincookie ); G_return( "认证码已发送成功,请查收!" ,1); } else { G_return( "发送失败,请联系管理员!" ); } } else { $rm =no_make_password(6); $content = $rm ; $fh =api_sendsms( $tel , $content ); if ( $fh ){ $gzwc =make_password(20); $sql = $empire ->query( "insert into {$dbtbpre}rzq(bsm,rzm,sj,t,c,username) values('$gzwc','$rm','$tel','$time','1','" . $username . "');" ); $logincookie = $time +3600*2; $set1 =esetcookie( "gzwrzq" , $gzwc , $logincookie ); G_return( "认证码已发送成功,请查收!" ,1); } else { G_return( "发送失败,请联系管理员!" ); } } } //替换手机短信内容变量 function T_sjn( $rzm ){ global $empire , $dbtbpre , $public_r ; $date = date ( "Y-m-d" ); $time = date ( "Y-m-d H:i:s" ); $content = $public_r [ 'add_rznr' ]; $content = str_replace ( '[!--rzm--]' , $rzm , $content ); return iconv( 'UTF-8' , 'GB2312' , $content ); //utf-8 //return $content; //gbk } //替换手机短信内容变量(找回密码) function T_sjnq( $rzm ){ global $empire , $dbtbpre , $public_r ; $date = date ( "Y-m-d" ); $time = date ( "Y-m-d H:i:s" ); $content = $public_r [ 'add_rznrq' ]; $content = str_replace ( '[!--rzm--]' , $rzm , $content ); return iconv( 'UTF-8' , 'GB2312' , $content ); //utf-8 //return $content; } //注册时手机验证 function C_sjrz( $tel , $rzm ){ global $empire , $dbtbpre , $public_r ; $ism =is_mobile( $tel ); if ( $ism ==2) { printerror( '手机号码为空或者错误!' . $tel . $rzm , '' ,1,0,1); } $zrc = $empire ->fetch1( "select * from {$dbtbpre}enewsmemberadd where phone='$tel' limit 1" ); if ( $zrc [ 'id' ]){ printerror( '该号码注册,请更换其他号码!' , '' ,1,0,1); } //识别码 $gzwc =RepPostVar(getcvar( 'gzwrz' )); $rzm =RepPostVar( $rzm ); $zr = $empire ->fetch1( "select * from {$dbtbpre}rz where bsm='$gzwc' and sj='$tel' and rzm='$rzm' order by id desc limit 1" ); if (! $zr [ 'id' ]){ printerror( '手机号码验证失败!' , '' ,1,0,1); } $time =time(); $gtime2 = $zr [ 't' ]+3600*12; if ( $time > $gtime2 ) { printerror( '手机验证码已超过有效期!' , '' ,1,0,1); } $set2 =esetcookie( "gzwrz" , "" ,0); $gsql = $empire ->query( "delete from {$dbtbpre}rz where id=$zr[id] " ); return '' ; } //验证手机号码 function is_mobile( $tel ){ if ( $tel == "" ) { return 2; } //验证长度 if ( strlen ( $tel )!= "11" ) { return 2; } //验证开头3位格式 if (!preg_match( "/13[0123456789]\d{8}|14[57]\d{8}|15[012356789]\d{8}|18[012356789]\d{8}|17[17]\d{8}/" , $tel )) ///1\d{10}/ { return 2; } return 1; } //返回信息 function G_return( $n , $d =2){ $gf [ 'n' ]=Gbkbm( $n ); $gf [ 'd' ]= $d ; echo json_encode( $gf ); exit (); } //中文编码 function Gbkbm( $g ){ //$g=iconv("GB2312","UTF-8//IGNORE",$g); return $g ; } //短信发送接口 function api_sendsms( $moblie , $content ){ global $empire , $dbtbpre , $public_r ; //$uid=mb_convert_encoding($uid,'GB2312','UTF-8'); //内容为UTF-8时转码成GB2312 //$msg=mb_convert_encoding($msg,'GB2312','UTF-8'); //内容为UTF-8时转码成GB2312 require ( "../admin/smsbao/Smsbao.php" ); //取出短信配置信息 $smsbao_config = file_get_contents ( "../admin/smsbao/config.txt" ); $smsbao_config = explode ( "," , $smsbao_config ); $smsbao = new Sms( $smsbao_config [0], $smsbao_config [1]); $content = "【" . $smsbao_config [2]. "】您好,你的验证码为:" . $content . ',请勿告诉他人' ; $sms_res = $smsbao ->sendSms( $moblie , $content ); if ( $sms_res == '0' ){ return true; } else { return false; } /* 提交成功返回值格式: 0发送成功! */ } |
好了经过以上的添加,短信宝短信接口就已经安装成功,可以正常使用了,最后我们进行发送测试:
报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。
另外:我们已经开发好完整的帝国CMS系统短信宝插件,点击此链接 下载及查看安装流程。
最新更新
电商类
CMS类
微信类