• 4001-021-502
您的位置:短信宝 > 技术分享 > 爱我拍众筹新增短信宝短信接口
爱我拍众筹新增短信宝短信接口
发表日期:2018-05-04    文章编辑:短信宝小编    浏览次数:

爱我拍众筹 是小商贷旗下的一个全新的融入互联网金融元素的多元化众筹汽车平台,是全国第一个集买车、卖车、租车、分期、众筹、投资价值、开网上店铺经营自己车辆等于一体的生态系统。小编今天就以替换短信接口为例带大家进行二次开发,我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。

下面我们进行开发,打开项目\webapps\www\library的文件夹,创建短信宝接口类文件,代码如下:

?
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
<?php
/** 短信接口类
 * Class Sms_Library
 */
 
class Sms_Library extends Lowxp_Model{
 
    /** 根据模板发送短信
     * @param $mobile
     * @param $template_code
     * @param $type 1返回短信内容
     * @param $content 发送内容
     * @param array $assignTpl
     */
    function sendSmsTpl($mobile$template_code$type=0, $content=''){
        if(empty($template_code) || empty($mobile)) return false;
 
        $sql "SELECT template_content,status,send_number FROM ###_templates WHERE `type`='sms' AND `status`='1' AND template_code = '$template_code'";
        if($row $this->db->get($sql)){
 
            #模板付值
            $assignTpl $this->smarty->get_template_vars();
            $smarty = smartyTpl();
            if(is_array($assignTpl) && !empty($assignTpl)){
                foreach($assignTpl as $k=>$v){
                    $smarty->assign($k$v);
                }
            }
 
            #获取模板解析内容
            $content $content?$content:$smarty->fetch($template_code.'.html');
            unset($smarty);
            if($type==1){ return $content; }
 
            $msg $this->sendSms($mobile$content);
            if($msg === true){
                #发送记录
                $data array(
                    'last_send'    => time(),
                    'send_number'  => intval($row['send_number']) + 1,
                );
                $this->db->save('###_templates',$data,'',array('template_code'=>$template_code));
 
                $data array(
                    'mobile' => $mobile,
                    'content' => $content,
                    'send_time' => time(),
                    'tpl' => $template_code
                );
                $this->db->save('###_sms',$data);
                return true;
            }
            else{
                return $msg; #提示信息
            }
 
        }
        return false;
    }
 
    /** 发送短信 基础函数
     * @param $mobile
     * @param $content
     * @param array $cfg
     */
    function sendSms($mobile$content$cfg array()){
        $config $this->setting->value("'sms_open','sms_type','sms_username','sms_password','site_name','sms_mod'");
        if(!$config['sms_open']) return false;
 
        if($cfg && is_array($cfg)) {
            $config $cfg;
        }
 
        #文件日志
        $this->load->library('dir');
        $log date('Y-m-d H:i:s')." | ".getIP()." | ".$mobile." | ".$content."\r\n";
        $this->dir->filePutContents(AppDir."data/sms_log_".$config['sms_username'].".log",$log,'ab+');
 
        /******上海互亿*****/
        if($config['sms_type'] == 1){
            $target "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
            //替换成自己的测试账号,参数顺序和wenservice对应
            $post_data "account=".$config['sms_username']."&password=".md5($config['sms_password'])."&md5=1&mobile=".$mobile."&content=".urlencode($content);
            $gets = '<?xml version="1.0" encoding="utf-8"?>
                    <SubmitResult xmlns="http://106.ihuyi.cn/">
                    <code>0</code>
                    <msg>调试成功</msg>
                    </SubmitResult>';
            if($config['sms_mod'] == 0){
                $gets $this->curlPost($post_data$target);
            }
 
            $gets_arr $this->xmlToArray($gets);
            //$this->dir->filePutContents(AppDir."data/sms_log_".$config['sms_username'].".log",'发送状态:'.$gets_arr['SubmitResult']['msg']."\r\n",'ab+');
 
            if ($gets_arr['SubmitResult']['code'] == 2){
                return true;
            }else{
                return $gets_arr['SubmitResult']['msg'];
            }
        }
        /******上海互亿 end*****/
        /******容联云通讯*****/
        elseif($config['sms_type'] == 2){
            include_once(AppDir.'library/CCPRestSDK.php');
            // 初始化REST SDK
            //$serverIP = 'sandboxapp.cloopen.com';
            $serverIP 'app.cloopen.com';
            $serverPort '8883';
            $softVersion '2013-12-26';
            //语音接口配置帐号
            $accountSid $this->site_config['voice_sid'];
            $accountToken $this->site_config['voice_token'];
            $appId $this->site_config['voice_appid'];
 
            $rest new REST($serverIP,$serverPort,$softVersion);
            $rest->setAccount($accountSid,$accountToken);
            $rest->setAppId($appId);
 
            //解析模板变量与模板ID
            $assignTpl $this->smarty->get_template_vars();
            $content explode('|'$content);
            $tempId = 0;
            $array array();
            if($content){
                $i = 0;
                foreach($content as $k=>$v){
                    $v = trim($v);
                    if($v){
                        if($i==0){ $tempId $v; }
                        else{
                            if(isset($assignTpl[$v])){
                                $array[] = $assignTpl[$v];
                                if($v=='verify_code'){
                                    $array[] = '5';
                                }
                            }
                        }
                        $i++;
                    }
                }
            }
 
            $result $rest->sendTemplateSMS($mobile,$array,$tempId);
            if($result == NULL ) {
                return 'result error!';
            }
            $result = (array$result;
            if($result['statusCode']!=0) {
                return $result['statusMsg'];
            }else{
                return true;
            }
        }elseif($config['sms_type'] == 3){
            $url 'http://api.smsbao.com/sms?u='.$config['sms_username']."&p=".md5($config['sms_password'])."&m=".$mobile."&c=".urlencode($content);
            $ret file_get_contents($url);
            if ($ret == 0) {
                return true;
            else {
                return $ret;
            }
            
        }
        /******容联云通讯 end*****/
        else{ #TODO:其它接口扩展
            return false;
        }
    }
 
    /** 生成短信随机验证码
     * @return string
     */
    function getVerifyCode() {
        $length = 6;
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        return $hash;
    }
 
    /** 远程连接
     * @param $curlPost
     * @param $url
     * @return mixed
     */
    function curlPost($curlPost,$url){
        //set_time_limit(60);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($curl, CURLOPT_TIMEOUT, 60);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }
 
    /** 简单解析xml
     * @param $xml
     * @return mixed
     */
    function xmlToArray($xml){
        $reg "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        $arr array();
        if(preg_match_all($reg$xml$matches)){
            $count count($matches[0]);
            for($i = 0; $i $count$i++){
                $subxml$matches[2][$i];
                $key $matches[1][$i];
                if(preg_match( $reg$subxml )){
                    $arr[$key] = $this->xmlToArray( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return @$arr;
    }
 
}

接着我们在项目\webapps\www\controller的控制器目录下创建申请短信接口安装的数据库文件:

?
1
UPDATE `zz_config` SET `tip`='推荐接口 申请短信接口账号,请点击这里(<a href="http://www.smsbao.com/reg" target="_blank">短信宝短信</a>)</br>云通讯调用其它接口中语音验证码的三个帐号,切换到云通讯后需要到短信模板按提示重新编辑模板', `step`='{"options":"上海互忆|1\\r\\n云通讯|2\\r\\n短信宝|3","fieldtype":"tinyint","numbertype":"1","labelwidth":"","default":"1"}' WHERE (`varname`='sms_type') AND (`title`='短信平台') AND (`group`='sms')

接着在\webapps\www\controller目录下创建短信接口安装文件,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?php
        include(AppDir.'/config/database.php');
        $sql file_get_contents(AppDir . '/controller/smsbao.sql');
        $comm = mysqli_connect($config['host'],$config['user'],$config['pass'],$config['dbname']) or die('Access Deny!(DB)');
        $obj = mysqli_query($comm,$sql);
        if ($obj) {
            echo '<h4>短信宝插件安装成功,请删除webapps\www\controller\smsbao.php和smsbao.sql文件。';
        else {
            echo '请联系短信宝客服。';
        }
        
       exit;

经过以上的替换,短信宝的短信平台已经替换成功了,我们去后台进行查看:

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

另外:我们已经开发好完整的爱我拍众筹短信宝插件,点击此链接 下载及查看安装流程。

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

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

展开