齐博X1是齐博软件基于thinkphp5开发的内容管理系统,拓展性非常强,后台一键升级,后台提供丰富的频道模块云市插件市场、风格市场、钩子市场,所有都是一键在线安装。小编对他还是比较了解的,今天小编就以新增短信接口为例,给大家讲解一下如何进行二次开发,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台的接口非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
首先在项目:\plugins\smsbao中创建admin_menu.php文件,代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?phpreturn array( 'plugin'=>array( 'title'=>'plugin', 'sons'=>array( array( 'title'=>'短信宝短信接口', 'sons'=>array( array( 'title'=>'参数选项管理', 'link'=>'setting/index', 'power'=>[], ), ), ), ), ),); |
接着创建:\plugins\smsbao\Api.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
|
<?phpnamespace plugins\smsbao;class Api{ private $accessKeyId; private $accessKeySecret; function __construct($accessKeyId, $accessKeySecret) { $this->accessKeyId = $accessKeyId; $this->accessKeySecret = $accessKeySecret; } /** * 发送短信 * @param string $signName 必填, 短信签名 * @param string $templateCode 必填, 短信模板Code * @param string $phoneNumbers 必填, 短信接收号码 */ public function sendSms($signName, $templateCode, $phoneNumbers, $templateParam = null) { $key = array_keys($templateParam); $value = array_values($templateParam); $content = str_replace($key,$value,$templateCode); $url = "http://api.smsbao.com/sms?u=".$this->accessKeyId.'&p='.md5($this->accessKeySecret).'&m='.$phoneNumbers.'&c=【'.$signName.'】'.$content; return $this->http($url); } /** * 通用CURL请求 * @param $url 需要请求的url * @param null $data * return mixed 返回值 json格式的数据 */ private function http($url,$data=null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $info = curl_exec($curl); curl_close($curl); return $info; }} |
接着创建:\plugins\smsbao\admin\Setting.php文件,代码如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?phpnamespace plugins\smsbao\admin;use app\common\controller\admin\Setting AS _Setting;class Setting extends _Setting{ /** * 参数设置 * {@inheritDoc} * @see \app\common\controller\admin\Setting::index() */ public function index($group=null){ return parent::index($group); }} |
接着创建安装数据表的文件:\plugins\smsbao\install\install.sql,代码如下
INSERT INTO `qb_config` (`id`, `type`, `title`, `c_key`, `c_value`, `form_type`, `options`, `ifsys`, `htmlcode`, `c_descrip`, `list`, `sys_id`) VALUES('', -1, '短信宝模板', 'sms_template', '', 'text', '', 1, '', '例如:您的验证码为code,请妥善保存。', 7, 0);INSERT INTO `qb_config` (`id`, `type`, `title`, `c_key`, `c_value`, `form_type`, `options`, `ifsys`, `htmlcode`, `c_descrip`, `list`, `sys_id`) VALUES('', -1, '短信宝签名', 'smsbao_sign', '', 'text', '', 1, '', '', 8, 0);INSERT INTO `qb_config` (`id`, `type`, `title`, `c_key`, `c_value`, `form_type`, `options`, `ifsys`, `htmlcode`, `c_descrip`, `list`, `sys_id`) VALUES('', -1, '短信宝密码', 'smsbao_pass', '', 'text', '', 1, '', '即AccessKeySecret', 9, 0);INSERT INTO `qb_config` (`id`, `type`, `title`, `c_key`, `c_value`, `form_type`, `options`, `ifsys`, `htmlcode`, `c_descrip`, `list`, `sys_id`) VALUES('', -1, '短信宝用户名', 'smsbao_user', '', 'text', '', 1, '', '即AccessKeyId', 10, 0); |
最后打开项目:\application\common\util\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
|
<?phpnamespace app\common\util;class Sms{ public function send($phone='',$msg=''){ // 如果$msg长度大于6位的话,判断是营销信息的话,可以在这里更换其它接口 return $this->smsbao($phone,$msg); } private function smsbao($phone='',$msg=''){ if(!class_exists("\\plugins\\smsbao\\Api")){ return '短信接口不存在'; } $obj = new \plugins\smsbao\Api(config('webdb.smsbao_user'),config('webdb.smsbao_pass')); $signName = config('webdb.smsbao_sign'); //签名,比如齐博 $templateCode = config('webdb.sms_template'); //使用的模板,比如SMS_16830430 $phoneNumbers = $phone; $templateParam = ['code'=>$msg]; $result = $obj->sendSms($signName, $templateCode, $phoneNumbers, $templateParam); if($result=='0'){ return true; }else{ return $result; } } //其它短信接口 private function other_sms(){ } } |
经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行测试发送:
报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。
另外:我们已经开发好完整的齐博X1系统短信宝插件,点击此链接 下载及查看安装流程。
- 上一篇:禾匠小程序新增短信宝短信接口 下一篇:小鸟云-ZKEYS新增短信宝短信接口

