微擎框架系统新增短信宝短信接口
首先我们打开项目:\web\themes\default\system\user-setting.html 文件,添加以下代码
{if $do == 'sms'} <div id="sms"> <div class="form-group"> <label class="col-sm-2 control-label" style="text-align:left;">短信宝账号</label> <div class="col-sm-8"> <input type="text" name="sms_name" class="form-control" value="{$settings['sms_name']}" /> <span class="help-block">没有短信宝账号?点击<a class="btn btn-primary span3" target="_blank" href="http://www.smsbao.com">免费注册</a></span> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" style="text-align:left;">短信宝密码</label> <div class="col-sm-8"> <input type="password" name="sms_password" class="form-control" value="{$settings['sms_password']}" /> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" style="text-align:left;">短信签名</label> <div class="col-sm-8"> <input type="text" name="sms_sign" class="form-control" value="{$settings['sms_sign']}" /> </div> </div> </div> {/if}
接着在项目:\web\themes\default\system\user-setting-header.html文件中添加以下代码
<li {if $do == 'sms'}class="active"{/if}><a href="{php echo url('system/usersetting', array('do' => 'sms'))}">短信配置</a></li>
接着打开项目:\web\source\system\usersetting.ctrl.php 文件替换8~28行代码
$dos = array('login', 'binding','sms'); $do = in_array($do, $dos) ? $do : 'login'; $settings = $_W['setting']['copyright']; if(empty($settings) || !is_array($settings)) { $settings = array(); } else { $settings['slides'] = iunserializer($settings['slides']); } if ($do == 'sms') { $_W['page']['title'] = '短信配置'; if (checksubmit('submit')) { $settings['sms_name'] = $_GPC['sms_name']; $settings['sms_password'] = $_GPC['sms_password']; $settings['sms_sign'] = $_GPC['sms_sign']; setting_save($settings, 'copyright'); itoast('更新设置成功!', '', 'success'); } }
最后我们打开项目:\framework\model\cloud.mod.php 文件,替换cloud_sms_send方法
function cloud_sms_send($mobile, $content, $postdata = array(), $custom_sign = '') { global $_W; if(!preg_match('/^1\d{10}$/', $mobile) || empty($content)) { return error(1, '发送短信失败, 原因: 手机号错误或内容为空.'); } if (empty($_W['uniacid'])) { $sms_info = cloud_sms_info(); $balance = empty($sms_info['sms_count']) ? 0 : $sms_info['sms_count']; if (!empty($custom_sign)) { $sign = $custom_sign; } } else { $row = pdo_get('uni_settings' , array('uniacid' => $_W['uniacid']), array('notify')); $row['notify'] = @iunserializer($row['notify']); $config = $row['notify']['sms']; $balance = intval($config['balance']); $sign = $config['signature']; } if(empty($sign)) { $sign = '短信宝'; } //判断剩余条数 if($balance<1){ return error(-1, '短信发送失败, 原因:余额不足'); } //短信宝账号 // $smsbao_info=pdo_get('uni_settings' , array('uniacid' => $_W['uniacid']), array('copyright')); //$smsbao_info=setting_load($key = 'copyright'); $sms_param['u']=$_W['setting']['copyright']['sms_name']; $sms_param['p']=md5($_W['setting']['copyright']['sms_password']); $sms_param['m']=$mobile; $sms_param['c']="【".$_W['setting']['copyright']['sms_sign']."】".$content; $statusStr = array( "0" => "短信发送成功", "-1" => "参数不全", "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!", "30" => "密码错误", "40" => "账号不存在", "41" => "余额不足", "42" => "帐户已过期", "43" => "IP地址限制", "50" => "内容含有敏感词" ); $response = file_get_contents("http://api.smsbao.com/sms?".http_build_query($sms_param)); if (trim($response)!='0') { return error($response, '短信发送失败, 原因:'.$statusStr[$response]); } if (trim($response)=='0') { if (!empty($_W['uniacid'])) { $row['notify']['sms']['balance'] = $row['notify']['sms']['balance'] - 1; if ($row['notify']['sms']['balance'] < 0) { $row['notify']['sms']['balance'] = 0; } pdo_update('uni_settings', array('notify' => iserializer($row['notify'])), array('uniacid' => $_W['uniacid'])); uni_setting_save('notify', $row['notify']); } } return true; }
经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行测试发送: