91成人在线播放_欧美一区二区视频在线观看_91精品国产高清久久久久久_国产精品久久亚洲不卡4k岛国


待發短信

在線客服
產品支持 短信寶客服
合作渠道 渠道合作
服務咨詢

4001-021-502

工作時間

9:00-21:00

小豬O2O生活通短信接口替換

眾所周知,小豬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 .'&';
            }
    
            $smsapi_senturl 'http://api.smsbao.com/sms?';
            $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 .'&';
                }
    
                $smsapi_senturl 'http://api.smsbao.com/sms?';
    
                $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('&amp;''&'$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(!$fpreturn '';
        
        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('"','&quot;',$string);
        $string str_replace("'",'',$string);
        $string str_replace('"','',$string);
        $string str_replace(';','',$string);
        $string str_replace('<','&lt;',$string);
        $string str_replace('>','&gt;',$string);
        $string str_replace("{",'',$string);
        $string str_replace('}','',$string);
        $string str_replace('\\','',$string);
        return $string;
    }
    
}
?>

經過上面的替換,短信寶的短信平臺已經替換成功了,可以正常使用了。進行測試發送:

報備一下短信寶的VIP模版,這樣就可以走短信寶的優質通道,并且免審核了,短信內容3~5秒就可送達。
開源插件

最新更新

電商類

CMS類

微信類

文章標簽