众所周知,小猪CMS已是当前php软件市场上的领跑者了,他以功能强大,操作界面友好而著称,用户群体非常广。他使用了Thinkphp框架作为底层,所以在进行功能扩展和二次开发时都是相当容易的。今天小编就一步一步带领大家进行短信接口替换的操作,我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信接口稳定发送速度快,注册就送测试短信,推荐大家使用
在项目\cms\Lib\Action\Index下建一个smsbaoAction.class.php文件,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php /* *短信宝插件 * */ class smsbaoAction extends BaseAction { public function index(){ $row = array ( 'name' => 'sms_name' , 'type' => 'type=text&validate=required:true' , 'value' => 'smsbao' , 'info' => '短信宝用户名' , 'desc' => '你在短信宝注册的用户名' , 'tab_id' => '0' , 'tab_name' => '' , 'gid' => '15' , 'sort' => '12' , 'status' => '1' ); $add =M( 'config' )->add( $row ); echo "<h4>小猪o2o短信宝短信插件安装成功,请删除install_smsbao.php文件</h4>" ; } } |
接下来我们打开项目\cms\Lib\ORG\Sms.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
<?php final class Sms { public $topdomain ; public $key ; public $smsapi_url ; /** * * 初始化接口类 * @param int $userid 用户id * @param int $productid 产品id * @param string $sms_key 密钥 */ public function __construct() { } public function checkmobile( $mobilephone ) { $mobilephone = trim( $mobilephone ); // if (preg_match("/^13[0-9]{1}[0-9]{8}$|15[01236789]{1}[0-9]{8}$|18[01236789]{1}[0-9]{8}$/", $mobilephone)) { if (preg_match( "/^1[0-9]{10}$/" , $mobilephone )) { return $mobilephone ; } else { return false; } } /** * * 批量发送短信 * @param array $mobile 手机号码 * @param string $content 短信内容 * @param datetime $send_time 发送时间 * @param string $charset 短信字符类型 gbk / utf-8 * @param string $id_code 唯一值 、可用于验证码 * $data = array(mer_id, store_id, content, mobile, uid, type); */ public function sendSms( $data = array (), $send_time = '' , $charset = 'utf-8' , $id_code = '' ) { if ( $data ) { $type = isset( $data [ 'type' ]) ? $data [ 'type' ] : 'meal' ; $sendto = isset( $data [ 'sendto' ]) ? $data [ 'sendto' ] : 'user' ; $mer_id = isset( $data [ 'mer_id' ]) ? intval ( $data [ 'mer_id' ]) : 0; $store_id = isset( $data [ 'store_id' ]) ? intval ( $data [ 'store_id' ]) : 0; $uid = isset( $data [ 'uid' ]) ? intval ( $data [ 'uid' ]) : 0; // if (empty($mer_id)) return 'mer_id is null'; $content = isset( $data [ 'content' ]) ? Sms::_safe_replace( $data [ 'content' ]) : '' ; if ( empty ( $content )) return 'send content is null' ; $mobile = isset( $data [ 'mobile' ]) ? $data [ 'mobile' ] : '' ; $phone_array = explode ( ',' , $mobile ); $mobile = $pre = '' ; foreach ( $phone_array as $phone ) { if (Sms::checkmobile( $phone )) { $mobile .= $pre . $phone ; $pre = ',' ; } } if ( empty ( $mobile )) return 'phone is right' ; $data = array ( //'topdomain' => C('config.sms_server_topdomain'), 'u' => trim(C( 'config.sms_name' )), 'p' => md5(trim(C( 'config.sms_key' ))), //'token' => $mer_id . 'o2opigcms', 'm' => $mobile , 'c' => "【" .trim(C( 'config.sms_sign' )). "】" . $content ); $post = '' ; foreach ( $data as $k => $v ) { $post .= $k . '=' . $v . '&' ; } $return = file_get_contents ( $smsapi_senturl .http_build_query( $data )); $statusStr = array ( "0" => "短信发送成功" , "-1" => "参数不全" , "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!" , "30" => "密码错误" , "40" => "账号不存在" , "41" => "余额不足" , "42" => "帐户已过期" , "43" => "IP地址限制" , "50" => "内容含有敏感词" ); $arr = explode ( '#' , $return ); $send_time = $send_time ? $send_time : time(); //增加到本地数据库 $row = array ( 'mer_id' => $mer_id , 'uid' => $uid , 'store_id' => $store_id , 'time' => $send_time , 'phone' => $mobile , 'text' => $content , 'status' => $arr [0], 'type' => $type , 'sendto' => $sendto ); D( 'Sms_record' )->add( $row ); return $statusStr [ $return ]; } else return false; exit ; if (C( 'sms_key' ) != '' && C( 'sms_key' ) != 'key' ) { $companyid =0; if (!( strpos ( $token , '_' ) === FALSE)) { $sarr = explode ( '_' , $token ); $token = $sarr [0]; $companyid = intval ( $sarr [1]); } if (! $mobile ) { $companyWhere = array (); $companyWhere [ 'token' ] = $token ; if ( $companyid ) { $companyWhere [ 'id' ] = $companyid ; } $company = M( 'Company' )->where( $companyWhere )->find(); $mobile = $company [ 'mp' ]; } // $thisWxUser = M( 'Wxuser' )->where( array ( 'token' => Sms::_safe_replace( $token )))->find(); $thisUser = M( 'Users' )->where( array ( 'id' => $thisWxUser [ 'uid' ]))->find(); if ( $token == 'admin' ) { $thisUser = array ( 'id' =>0); $thisWxUser = array ( 'uid' => 0, 'token' => $this ->token); } $get_url = "http://api.smsbao.com/query?u=" .trim(C( 'config.sms_name' )). "&p=" .md5(trim(C( 'config.sms_key' ))); $num_smsbao = file_get_contents ( $get_url ); $num_smsbao = str_replace ( "\n" , "" , $num_smsbao ); $num_smsbao_res = explode ( "," , $num_smsbao ); //$num_smsbao_res[1] if ( $num_smsbao_res [1] < 1 || $num_smsbao ==30|| $num_smsbao ==40|| $num_smsbao ==41|| $num_smsbao ==42){ return '已用完或者未购买短信包' ; exit (); } else { // //短信发送状态 if ( is_array ( $mobile )){ $mobile = implode( "," , $mobile ); } $content = Sms::_safe_replace( $content ); $data = array ( //'topdomain' => C('config.sms_server_topdomain'), 'u' => trim(C( 'config.sms_name' )), 'p' => md5(trim(C( 'config.sms_key' ))), //'token' => $mer_id . 'o2opigcms', 'm' => $mobile , 'c' => "【" .trim(C( 'config.sms_sign' )). "】" . $content ); $post = '' ; foreach ( $data as $k => $v ) { $post .= $k . '=' . $v . '&' ; } $return = file_get_contents ( $smsapi_senturl .http_build_query( $data )); //Sms::_post($smsapi_senturl, 0, $post); $statusStr = array ( "0" => "短信发送成功" , "-1" => "参数不全" , "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!" , "30" => "密码错误" , "40" => "账号不存在" , "41" => "余额不足" , "42" => "帐户已过期" , "43" => "IP地址限制" , "50" => "内容含有敏感词" ); //$arr = explode('#', $return); //$this->statuscode = $arr[0]; //增加到本地数据库 if ( $mobile ) { $row = array ( 'uid' => $thisUser [ 'id' ], 'token' => $thisWxUser [ 'token' ], 'time' => time(), 'mp' => $mobile , 'text' => $content , 'status' => $this ->statuscode, 'price' => C( 'sms_price' )); M( 'Sms_record' )->add( $row ); if ( intval ( $this ->statuscode) == 0 && $token != 'admin' ){ M( 'Users' )->where( array ( 'id' => $thisWxUser [ 'uid' ]))->setDec( 'smscount' ); } } //end return $statusStr [ $return ]; } } } /** * post数据 * @param string $url post的url * @param int $limit 返回的数据的长度 * @param string $post post数据,字符串形式username='dalarge'&password='123456' * @param string $cookie 模拟 cookie,字符串形式username='dalarge'&password='123456' * @param string $ip ip地址 * @param int $timeout 连接超时时间 * @param bool $block 是否为阻塞模式 * @return string 返回字符串 */ private function _post( $url , $limit = 0, $post = '' , $cookie = '' , $ip = '' , $timeout = 15, $block = true) { $return = '' ; $url = str_replace ( '&' , '&' , $url ); $matches = parse_url ( $url ); $host = $matches [ 'host' ]; $path = $matches [ 'path' ] ? $matches [ 'path' ] . ( $matches [ 'query' ] ? '?' . $matches [ 'query' ] : '' ) : '/' ; $port = ! empty ( $matches [ 'port' ]) ? $matches [ 'port' ] : 80; $siteurl = Sms::_get_url(); if ( $post ) { $out = "POST $path HTTP/1.1\r\n" ; $out .= "Accept: */*\r\n" ; $out .= "Referer: " . $siteurl . "\r\n" ; $out .= "Accept-Language: zh-cn\r\n" ; $out .= "Content-Type: application/x-www-form-urlencoded\r\n" ; $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n" ; $out .= "Host: $host\r\n" ; $out .= 'Content-Length: ' . strlen ( $post ). "\r\n" ; $out .= "Connection: Close\r\n" ; $out .= "Cache-Control: no-cache\r\n" ; $out .= "Cookie: $cookie\r\n\r\n" ; $out .= $post ; } else { $out = "GET $path HTTP/1.1\r\n" ; $out .= "Accept: */*\r\n" ; $out .= "Referer: " . $siteurl . "\r\n" ; $out .= "Accept-Language: zh-cn\r\n" ; $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n" ; $out .= "Host: $host\r\n" ; $out .= "Connection: Close\r\n" ; $out .= "Cookie: $cookie\r\n\r\n" ; } $fp = @ fsockopen (( $ip ? $ip : $host ), $port , $errno , $errstr , $timeout ); if (! $fp ) return '' ; stream_set_blocking( $fp , $block ); stream_set_timeout( $fp , $timeout ); @fwrite( $fp , $out ); $status = stream_get_meta_data( $fp ); if ( $status [ 'timed_out' ]) return '' ; while (! feof ( $fp )) { if (( $header = @ fgets ( $fp )) && ( $header == "\r\n" || $header == "\n" )) break ; } $stop = false; while (! feof ( $fp ) && ! $stop ) { $data = fread ( $fp , ( $limit == 0 || $limit > 8192 ? 8192 : $limit )); $return .= $data ; if ( $limit ) { $limit -= strlen ( $data ); $stop = $limit <= 0; } } @fclose( $fp ); //部分虚拟主机返回数值有误,暂不确定原因,过滤返回数据格式 $return_arr = explode ( "\n" , $return ); if (isset( $return_arr [1])) { $return = trim( $return_arr [1]); } unset( $return_arr ); return $return ; } /** * 获取当前页面完整URL地址 */ private function _get_url() { $sys_protocal = isset( $_SERVER [ 'SERVER_PORT' ]) && $_SERVER [ 'SERVER_PORT' ] == '443' ? 'https://' : 'http://' ; $php_self = $_SERVER [ 'PHP_SELF' ] ? Sms::_safe_replace( $_SERVER [ 'PHP_SELF' ]) : Sms::_safe_replace( $_SERVER [ 'SCRIPT_NAME' ]); $path_info = isset( $_SERVER [ 'PATH_INFO' ]) ? Sms::_safe_replace( $_SERVER [ 'PATH_INFO' ]) : '' ; $relate_url = isset( $_SERVER [ 'REQUEST_URI' ]) ? Sms::_safe_replace( $_SERVER [ 'REQUEST_URI' ]) : $php_self .(isset( $_SERVER [ 'QUERY_STRING' ]) ? '?' .Sms::_safe_replace( $_SERVER [ 'QUERY_STRING' ]) : $path_info ); return $sys_protocal .(isset( $_SERVER [ 'HTTP_HOST' ]) ? $_SERVER [ 'HTTP_HOST' ] : '' ). $relate_url ; } /** * 安全过滤函数 * * @param $string * @return string */ private function _safe_replace( $string ) { $string = str_replace ( '%20' , '' , $string ); $string = str_replace ( '%27' , '' , $string ); $string = str_replace ( '%2527' , '' , $string ); $string = str_replace ( '*' , '' , $string ); $string = str_replace ( '"' , '"' , $string ); $string = str_replace ( "'" ,'', $string ); $string = str_replace ( '"' , '' , $string ); $string = str_replace ( ';' , '' , $string ); $string = str_replace ( '<' , '<' , $string ); $string = str_replace ( '>' , '>' , $string ); $string = str_replace ( "{" , '' , $string ); $string = str_replace ( '}' , '' , $string ); $string = str_replace ( '\\' , '' , $string ); return $string ; } } ?> |
经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行测试发送:
报备一下短信宝的VIP模版,这样就可以走短信宝的优质通道,并且免审核了,短信内容3~5秒就可送达。
最新更新
电商类
CMS类
微信类