待发短信

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

4001-021-502

工作时间

9:00-21:00

方维团购V3.07版本短信插件开发

方维团购系统是基于groupon模式开发的团购系统。它可以让用户高效、快速、低成本的构建个性化、专业化、强大功能的团购网站。采用php语言,便于二次开发,小编对它还是比较了解的,今天就以增加短信接口为例,一步步教大家如何进行二次开发,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。

首先第一步我们要在项目\system\sms下新建一个文件,文件名为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
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
<?php
// +----------------------------------------------------------------------
// | Fanweo2o商业系统 最新版V3.03.3285  含4个手机APP。
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
 
/* 模块的基本信息 */
if (isset($read_modules) && $read_modules == true)
{
    $module['class_name']    = 'smsbao';
    /* 名称 */
    $module['name']    = "短信宝增值平台 (<a href='https://www.smsbao.com/reg?r=5001' target='_blank'><font color='red'>还没账号?点击这免费注册</font></a>)";
    $module['server_url'] = 'http://api.smsbao.com/sms';
    
    if(ACTION_NAME == "install" || ACTION_NAME == "edit"){  
            $module['lang']  = array();
      $module['config'] = array();
    }
 
    return $module;
}
 
// 短信宝短信平台
require_once APP_ROOT_PATH."system/libs/sms.php";  //引入接口
require_once APP_ROOT_PATH."system/utils/sms_trans.php";
 
class smsbao_sms implements sms
{
        public $sms;
        public $message "";
        
        private $statusStr array(
                "0" => "短信发送成功",
                "-1" => "参数不全",
                "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
                "30" => "密码错误",
                "40" => "账号不存在",
                "41" => "余额不足",
                "42" => "帐户已过期",
                "43" => "IP地址限制",
                "50" => "内容含有敏感词"
        );
        
  public function __construct($smsInfo '')
    {                     
                if(!empty($smsInfo))
                {                        
                        $this->sms = $smsInfo;
                }
    }
        
        public function sendSMS($mobile_number,$content,$sendTime='',$is_adv=0)
        {
                if(is_array($mobile_number))
                {
                        $mobile_number = implode(",",$mobile_number);
                }
                $sms new sms_trans();
                                
                                $params array(
                                        "u"=>$this->sms['user_name'],
                                        "p"=>md5($this->sms['password']),
                                        "m"=>$mobile_number,
                                        "c"=>urlencode($content)
                                );
                                
                                $result $sms->request($this->sms['server_url'],$params);
                                $code $result['body'];
                                
                                if($code=='0')
                                {
                                                        $result['status'] = 1;
                                }
                                else
                                {
                                                        $result['status'] = 0;
                                                        $result['msg'] = $this->statusStr[$code];
                                }
                return $result;
        }
        
        public function getSmsInfo()
        {                
                        return "短信宝增值平台";
        }
        
        public function check_fee()
        {
                es_session::start();
                $last_visit intval(es_session::get("last_visit_smsbao"));
                if(get_gmtime() - $last_visit > 10)
                {
                        $sms new sms_trans();
                                
                        $params array(
                                                "u"=>$this->sms['user_name'],
                                                "p"=>md5($this->sms['password'])
                                        );
                                        
                        $url "http://api.smsbao.com/query";
                        $result $sms->request($url,$params);
        
                        $match explode(',',$result['body']);
            if ($match[0] != '')
            {
                            $remain = (int)$match[1];
                            $str = sprintf('短信宝增值平台&nbsp;&nbsp;剩余:%d 条'$remain);
            }
            else
            {
                            $str "短信宝增值平台 (<a href='https://www.smsbao.com/reg?r=5001' target='_blank'><font color='red'>还没账号?点击这免费注册</font></a>)";
            }
                
                        es_session::set("smsbao_info",$str);
                        es_session::set("last_visit_smsbao",get_gmtime());
                        return $str;
                }
                else
                {
                        $qxt_info = es_session::get("smsbao_info");
                        if($smsbao_info)
                        return $smsbao_info;
                        else
                        return "短信宝增值平台 (<a href='https://www.smsbao.com/reg?r=5001' target='_blank'><font color='red'>还没账号?点击这免费注册</font></a>)";
                }
 
        }
}
?>

第二步,我们要在项目\system\utils下新建一个文件,文件名为sms_trans.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
 
class sms_trans
{
    /**
     * 脚本执行时间。-1表示采用PHP的默认值。
     *
     * @access  private
     * @var     integer     $time_limit
     */
    var $time_limit                  = -1;
 
    /**
     * 在多少秒之内,如果连接不可用,脚本就停止连接。-1表示采用PHP的默认值。
     *
     * @access  private
     * @var     integer     $connect_timeout
     */
    var $connect_timeout             = -1;
 
    /**
     * 连接后,限定多少秒超时。-1表示采用PHP的默认值。此项仅当采用CURL库时启用。
     *
     * @access  private
     * @var     integer    $stream_timeout
     */
    var $stream_timeout              = -1;
 
    /**
     * 是否使用CURL库来连接。false表示采用fsockopen进行连接。
     *
     * @access  private
     * @var     boolean     $use_curl
     */
    var $use_curl                    = false;
 
    /**
     * 构造函数
     *
     * @access  public
     * @param   integer     $time_limit
     * @param   integer     $connect_timeout
     * @param   integer     $stream_timeout
     * @param   boolean     $use_curl
     * @return  void
     */
    function __construct($time_limit = -1, $connect_timeout = -1, $stream_timeout = -1, $use_curl = false)
    {
        //$this->transport($time_limit, $connect_timeout, $stream_timeout, $use_curl);
        $this->time_limit = $time_limit;
        $this->connect_timeout = $connect_timeout;
        $this->stream_timeout = $stream_timeout;
        $this->use_curl = $use_curl;        
    }
 
    /**
     * 请求远程服务器
     *
     * @access  public
     * @param   string      $url            远程服务器的URL
     * @param   mix         $params         查询参数,形如bar=foo&foo=bar;或者是一维关联数组,形如array('a'=>'aa',...)
     * @param   string      $method         请求方式,是POST还是GET
     * @param   array       $my_header      用户要发送的头部信息,为一维关联数组,形如array('a'=>'aa',...)
     * @return  array                       成功返回一维关联数组,形如array('header'=>'bar', 'body'=>'foo'),
     *                                      重大错误程序直接停止运行,否则返回false。
     */
    function request($url$params ''$method 'POST'$my_header '')
    {
        $fsock_exists = function_exists('fsockopen');
        $curl_exists = function_exists('curl_init');
 
        if (!$fsock_exists && !$curl_exists)
        {
            die('No method available!');
        }
 
        if (!$url)
        {
            die('Invalid url!');
        }
 
        if ($this->time_limit > -1)//如果为0,不限制执行时间
        {
            set_time_limit($this->time_limit);
        }
 
        $method $method === 'GET' $method 'POST';
        $response '';
        $temp_str '';
 
        /* 格式化将要发要送的参数 */
        if ($params && is_array($params))
        {
            foreach ($params AS $key => $value)
            {
                $temp_str .= '&' $key '=' $value;
            }
            $params = preg_replace('/^&/'''$temp_str);
        }
 
        /* 如果fsockopen存在,且用户不指定使用curl,则调用use_socket函数 */
        if ($fsock_exists && !$this->use_curl)
        {
            $response $this->use_socket($url$params$method$my_header);
        }
        /* 只要上述条件中的任一个不成立,流程就转向这里,这时如果curl模块可用,就调用use_curl函数 */
        elseif ($curl_exists)
        {
            $response $this->use_curl($url$params$method$my_header);
        }
 
        /* 空响应或者传输过程中发生错误,程序将返回false */
        if (!$response)
        {
            return false;
        }
 
        return $response;
    }
 
    /**
     * 使用fsockopen进行连接
     *
     * @access  private
     * @param   string      $url            远程服务器的URL
     * @param   string      $params         查询参数,形如bar=foo&foo=bar
     * @param   string      $method         请求方式,是POST还是GET
     * @param   array       $my_header      用户要发送的头部信息,为一维关联数组,形如array('a'=>'aa',...)
     * @return  array                       成功返回一维关联数组,形如array('header'=>'bar', 'body'=>'foo'),
     *                                      否则返回false。
     */
    function use_socket($url$params$method$my_header)
    {
        $query '';
        $auth '';
        $content_type '';
        $content_length '';
        $request_body '';
        $request '';
        $http_response '';
        $temp_str '';
        $error '';
        $errstr '';
        $crlf $this->generate_crlf();
 
        if ($method === 'GET')
        {
            $query $params "?$params" '';
        }
        else
        {
            $request_body  $params;
            $content_type 'Content-Type: application/x-www-form-urlencoded' $crlf;
            $content_length 'Content-Length: ' strlen($request_body) . $crlf $crlf;
        }
 
        $url_parts $this->parse_raw_url($url);
        $path $url_parts['path'] . $query;
 
        if (!empty($url_parts['user']))
        {
            $auth 'Authorization: Basic '
                    base64_encode($url_parts['user'] . ':' $url_parts['pass']) . $crlf;
        }
 
        /* 格式化自定义头部信息 */
        if ($my_header && is_array($my_header))
        {
            foreach ($my_header AS $key => $value)
            {
                $temp_str .= $key ': ' $value $crlf;
            }
            $my_header $temp_str;
        }
 
        /* 构造HTTP请求头部 */
        $request "$method $path HTTP/1.0$crlf"
                'Host: ' $url_parts['host'] . $crlf
                $auth
                $my_header
                $content_type
                $content_length
                $request_body;
 
        if ($this->connect_timeout > -1)
        {
            $fp = @fsockopen($url_parts['host'], $url_parts['port'], $error$errstr$connect_timeout);
        }
        else
        {
            $fp = @fsockopen($url_parts['host'], $url_parts['port'], $error$errstr);
        }
 
        if (!$fp)
        {
            return false;//打开失败
        }
 
        if (!@fwrite($fp$request))
        {
            return false;//写入失败
        }
 
        while (!feof($fp))
        {
            $http_response .= fgets($fp);
        }
 
        if (!$http_response)
        {
            return false;//空响应
        }
 
        $separator '/\r\n\r\n|\n\n|\r\r/';
        list($http_header$http_body) = preg_split($separator$http_response, 2);
 
        $http_response array('header' => $http_header,//header肯定有值
                               'body'   => $http_body);//body可能为空
        @fclose($fp);
 
        return $http_response;
    }
 
    /**
     * 使用curl进行连接
     *
     * @access  private
     * @param   string      $url            远程服务器的URL
     * @param   string      $params         查询参数,形如bar=foo&foo=bar
     * @param   string      $method         请求方式,是POST还是GET
     * @param   array       $my_header      用户要发送的头部信息,为一维关联数组,形如array('a'=>'aa',...)
     * @return  array                       成功返回一维关联数组,形如array('header'=>'bar', 'body'=>'foo'),
     *                                      失败返回false。
     */
    function use_curl($url$params$method$my_header)
    {
        /* 开始一个新会话 */
        $curl_session = curl_init();
 
        /* 基本设置 */
        curl_setopt($curl_session, CURLOPT_FORBID_REUSE, true); // 处理完后,关闭连接,释放资源
        curl_setopt($curl_session, CURLOPT_HEADER, true);//结果中包含头部信息
        curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);//把结果返回,而非直接输出
        curl_setopt($curl_session, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//采用1.0版的HTTP协议
 
        $url_parts $this->parse_raw_url($url);
 
        /* 设置验证策略 */
        if (!empty($url_parts['user']))
        {
            $auth $url_parts['user'] . ':' $url_parts['pass'];
            curl_setopt($curl_session, CURLOPT_USERPWD, $auth);
            curl_setopt($curl_session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        }
 
        $header array();
 
        /* 设置主机 */
        $header[] = 'Host: ' $url_parts['host'];
 
        /* 格式化自定义头部信息 */
        if ($my_header && is_array($my_header))
        {
            foreach ($my_header AS $key => $value)
            {
                $header[] = $key ': ' $value;
            }
        }
 
        if ($method === 'GET')
        {
            curl_setopt($curl_session, CURLOPT_HTTPGET, true);
            $url .= $params '?' $params '';
        }
        else
        {
            curl_setopt($curl_session, CURLOPT_POST, true);
            $header[] = 'Content-Type: application/x-www-form-urlencoded';
            $header[] = 'Content-Length: ' strlen($params);
            curl_setopt($curl_session, CURLOPT_POSTFIELDS, $params);
        }
 
        /* 设置请求地址 */
        curl_setopt($curl_session, CURLOPT_URL, $url);
 
        /* 设置头部信息 */
        curl_setopt($curl_session, CURLOPT_HTTPHEADER, $header);
 
        if ($this->connect_timeout > -1)
        {
            curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
        }
 
        if ($this->stream_timeout > -1)
        {
            curl_setopt($curl_session, CURLOPT_TIMEOUT, $this->stream_timeout);
        }
 
        /* 发送请求 */
        $http_response = curl_exec($curl_session);
 
        if (curl_errno($curl_session) != 0)
        {
            return false;
        }
 
        $separator '/\r\n\r\n|\n\n|\r\r/';
        list($http_header$http_body) = preg_split($separator$http_response, 2);
 
        $http_response array('header' => $http_header,//肯定有值
                               'body'   => $http_body); //可能为空
 
        curl_close($curl_session);
 
        return $http_response;
    }
 
    /**
     * Similar to PHP's builtin parse_url() function, but makes sure what the schema,
     * path and port keys are set to http, /, 80 respectively if they're missing
     *
     * @access     private
     * @param      string    $raw_url    Raw URL to be split into an array
     * @author     http://www.cpaint.net/
     * @return     array
     */
    function parse_raw_url($raw_url)
    {
        $retval   array();
        $raw_url  = (string) $raw_url;
 
        // make sure parse_url() recognizes the URL correctly.
        if (strpos($raw_url'://') === false)
        {
          $raw_url 'http://' $raw_url;
        }
 
        // split request into array
        $retval parse_url($raw_url);
 
        // make sure a path key exists
        if (!isset($retval['path']))
        {
          $retval['path'] = '/';
        }
 
        // set port to 80 if none exists
        if (!isset($retval['port']))
        {
          $retval['port'] = '80';
        }
 
        return $retval;
    }
 
    /**
     * 产生一个换行符,不同的操作系统会有不同的换行符
     *
     * @access     private
     * @return     string       用双引号引用的换行符
     */
    function generate_crlf()
    {
        $crlf '';
 
        if (strtoupper(substr(PHP_OS, 0, 3) === 'WIN'))
        {
            $crlf "\r\n";
        }
        elseif (strtoupper(substr(PHP_OS, 0, 3) === 'MAC'))
        {
            $crlf "\r";
        }
        else
        {
            $crlf "\n";
        }
 
        return $crlf;
    }
}
 
?>

经过刚才的替换,短信宝短信接口就可以正常使用了,这里再给大家说一些注意点:

①,短信签名在短信邮件->消息模版管理中配置,选中短信模版在最前面加上全角中括号,短信签名内容在3-8个字。例:【短信宝】你的手机号为{$verify.mobile},验证码为{$verify.code}。


②,有的人肯定会出现发送不出去的情况,这应该是前后台的队列没有开启。不开启队列是无法进行队列发送的,这是非常关键的一步。解决:登入后台 -> 系统设置 -> 基础配置 -> 开启前后台队列。


③,还有一种情况就是一直正常发送,突然接收不到短信了, 这应该是方维的后台队列BUG导致的,解决方法:登录方维管理后台 –> 短信邮件 –> 队列管理 — > 业务队列列表,做以下三个操作:1. 选择结果是失败的队列数据,点击彻底删除,2.点击 重置队列发送,3.清除缓存。
 

下面我们进行测试发送:

 

报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,并且免审核了,短信内容3~5秒就可送达。
开源插件

最新更新

电商类

CMS类

微信类

文章标签