• 4001-021-502
您的位置:短信宝 > 技术分享 > iWebShop商城系统新增短信宝短信接口
iWebShop商城系统新增短信宝短信接口
发表日期:2018-06-22    文章编辑:短信宝小编    浏览次数:

iWebShop是一款基于PHP语言及MYSQL数据库开发的B2B2C多用户开源免费的商城系统,系统支持平台自营和多商家入驻、集成微信商城、手机商城、移动端APP商城于一体。二次开发非常方便,小编对他还是比较了解的,今天小编为大家讲解iWebShop_5.1版本的短信接口替换,使用的接口是我们短信宝群发平台的短信接口,我们短信宝群发短信平台非常稳定,发送速度快,注册还送测试短信,推荐大家使用。

首先我们打开项目\views\sysdm\system\hsms.html文件,替换14~67行左右的代码,代码如下:

?
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
<div class="content">
 <form action="#" method="post" name='sms_conf'>
  <table class="table form-table">
   <colgroup>
    <col width="130px" />
    <col />
   </colgroup>
   <tr>
    <th>说明:</th>
    <td>
     还没有注册短信宝短信平台的用户,请点击
      <a href="http://www.smsbao.com" target="_blank" class="orange">注册</a>
     <p>商城所用的短信内容模板在【/classes/smstemplate.php】文件中,尽量用原始的短信模板,否则会导致短信发送延迟等问题</p>
     <p>如果想关闭某个短信发送环节,可以直接把相应方法的返回值设置为空</p>
    </td>
   </tr>
   <tr>
    <th>管理员手机号:</th>
    <td>【系统】——【网站设置】——【手机号】</td>
   </tr>
   <tr>
    <th>短信平台:</th>
    <td>
     <select name="sms_platform" class="form-control">
      <option value="smsbao">短信宝短信平台</option>
     </select>
    </td>
   </tr>
   <tr><th>用户名:</th>
    <td><input type='text' class='form-control' name='sms_username' pattern='required' alt='' /><p class="help-block">短信宝用户名</p></td>
   </tr>
   <tr><th>密码:</th>
    <td><input type='text' class='form-control' name='sms_pwd' pattern='required' alt='' /><p class="help-block">短信宝密码</p></td>
   </tr>
   <tr><th>签名:</th>
    <td><input type='text' class='form-control' name='sms_userid' pattern='required' alt='' /><p class="help-block">短信签名(3-8个字符)</p></td>
   </tr>
   <tr><th>测试手机号码:</th>
    <td><input type='text' class='form-control' name='mobile' pattern='mobi' empty alt='填写正确的手机号码' /><p class="help-block">必须先<保存>配置后,在测试短信发送的功能【可选】</p></td>
   </tr> <tr>
    <th></th>
    <td>
     <button type='button' class="btn btn-primary" onclick="submitConfig();">保存</button>
    <button class='btn btn-primary' type='button' onclick="test_sendhsms(this);"><span id='testmobile'>测试短信发送</span></button>
    </td>
   </tr>
  </table>
 </form>
</div>

接着我们打开项目\classes\hsms.php文件,替换 send的方法,代码如下:

?
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
public static function send($mobiles$content$delay = 1)
 {
  if(!$content)
  {
   return "短信内容不能为空";
  }
 
  if$delay == 1 && !isset($_SERVER['HTTP_USER_AGENT']) )
  {
   return "非客户端访问";
  }
 
  if(IClient::getIp() == '')
  {
   return "ip信息不合法";
  }
 
  $mobile_array explode(","$mobiles);
  foreach ($mobile_array as $key => $val)
  {
   if(false === IValidate::mobi($val))
   {
    unset($mobile_array[$key]);
   }
  }
 
  if(!$mobile_array)
  {
   return "非法手机号码";
  }
 
  if(count($mobile_array) > 200)
  {
   return "手机号超过200个";
  }
 
  //延迟机制
  if($delay == 1)
  {
   $cacheObj new ICache();
   $smsTime $cacheObj->get('smsDelay'.md5($mobiles));
   if($smsTime && time() - $smsTime < self::$sendStep)
   {
    return "短信发送频率太快,请稍候再试...";
   }
   //更新发送时间
   $cacheObj->set('smsDelay'.md5($mobiles),time());
  }
 
  if(self::$smsInstance == null)
  {
   $platform = self::getPlatForm();
   switch($platform)
   {
    case "smsbao":
    {
     $classFile = IWeb::$app->getBasePath().'plugins/hsms/smsbao.php';
     require($classFile);
     self::$smsInstance new smsbao();
    }
    break;
 
    default:
    {
     $classFile = IWeb::$app->getBasePath().'plugins/hsms/smsbao.php';
     require($classFile);
     self::$smsInstance new smsbao();
    }
   }
  }
  return self::$smsInstance->send($mobiles$content);
 }

最后我们在项目\plugins\hsms\目录下创建名为smsbao.php的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
<?php
/**
 * @copyright (c) 2016 smsbao.com
 * @file smsbao.php
 * @brief **短信发送接口
 * @author linf
 * @date 2016/11/21 11:10:38
 * @version 1.0
 */
 
 /**
 * @class smsbao
 * @brief 短信发送接口 http://api.smsbao.com/sms
 */
class smsbao extends hsmsBase
{
 private $submitUrl  "http://api.smsbao.com/sms";
 /**
 * @brief 获取config用户配置
 * @return array
 */
 public function getConfig()
 {
  $siteConfigObj new Config("site_config");
 
  return array(
   'username' => $siteConfigObj->sms_username,
   'userpwd'  => $siteConfigObj->sms_pwd,
   'sign'  => $siteConfigObj->sms_userid,
  );
 }
 
 /**
 * @brief 发送短信
 * @param string $mobile
 * @param string $content
 * @return
 */
 public function send($mobile,$content)
 {
  $config = self::getConfig();
 
  $post_data array(
   'u' => $config['username'],
   'p' => md5($config['userpwd']),
   'c'  => '【'.$config['sign'].'】'.$content,
   'm'   => $mobile,
  );
 
  $url    $this->submitUrl;
  $string '';
  foreach ($post_data as $k => $v)
  {
     $string .="$k=".urlencode($v).'&';
  }
 
  $post_string substr($string,0,-1);
 
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里,那加上这句。
  $result = curl_exec($ch);
  return $this->response($result);
 }
 
 /**
 * @brief 解析结果
 * @param $result 发送结果
 * @return string success or fail
 */
 public function response($result)
 {
  if(trim($result) =='0')
  {
   return 'success';
  }
  else
  {
   return $this->getMessage($result);
  }
 }
 
 /**
 * @brief 获取参数
 */
 public function getParam()
 {
  return array(
   "username" => "用户名",
   "userpwd"  => "密码",
   "usersign" => "短信签名",
  );
 }
 
 
 //返回消息提示
 public function getMessage($code)
 {
  $messageArray array(
   -1 =>"参数不全",
   30  =>"密码错误",
   40  =>"账号不存在",
   41  =>"余额不足",
   42  =>"账号过期",
   43  =>"IP地址限制",
   50  =>"内容含有敏感词",
   51 =>"手机号码不正确",
  );
  return isset($messageArray[$code]) ? $messageArray[$code] : "未知错误";
 }
}

好了,经过以上的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行发送测试:

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

另外:我们已经开发好完整的iwebshop商城系统短信宝插件,点击此链接 下载及查看安装流程。

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

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

展开