likeshop是一款开源的企业级B2C电商系统,前端uni-app架构,极致节省开发成本,一次研发,多端发布。后端ThinkPHP架构,国内最流行的PHP研发框架,易用易学,好二开。小编带着大家一起开发likeshop商城3.0.3.20231204的短信宝插件接口。我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
首先执行以下sql
1
|
INSERT INTO `ls_sms_config` ( `name`, `describe`, `sign`, `app_key`, `secret_key`, `status`, `del`) VALUES ( '短信宝' , '短信宝短信服务' , '' , '' , '' , '0' , '0' ); |
接着我们打开项目application\common\behavior\SmsSend.php文件,在97行~112行增加以下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
}elseif ($ this ->channel == 3) { // 短信宝 $res = ( new Smsbao($ this ->config)) ->setMobile($ this ->mobile) ->setContent($ this ->content) ->sendSms(); if (isset($res[ 'Code' ]) && $res[ 'Code' ] == 'Ok' ) { $send_status = SmsLog::send_success; $ this ->updateSmsLog($send_status, $res); return true ; } else { $send_status = SmsLog::send_fail; $ this ->updateSmsLog($send_status, $res); $message = $res[ 'Message' ] ?? json_encode($res); throw new Exception( '短信配置错误:' . $message); } } |
接着打开项目application\common\server 目录,新增 Smsbao.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
|
<?php namespace app\common\server; use think\Exception; class Smsbao { private $app_key = '' ; private $secret_key = '' ; private $sign = '' ; private $mobile = '' ; private $content= '' ; public function __construct($config) { $ this ->sign = $config[ 'sign' ]; $ this ->app_key = $config[ 'app_key' ]; $ this ->secret_key = $config[ 'secret_key' ]; } public function setContent($template_code){ $ this ->content = $template_code; return $ this ; } public function setMobile($mobile){ $ this ->mobile = $mobile; return $ this ; } public function sendSms(){ try { $statusStr = array( "0" => "短信发送成功" , "-1" => "参数不全" , "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!" , "30" => "密码错误" , "40" => "账号不存在" , "41" => "余额不足" , "42" => "帐户已过期" , "43" => "IP地址限制" , "50" => "内容含有敏感词" ); $user = $ this ->app_key; //短信平台帐号 $pass = md5($ this ->secret_key); //短信平台密码 $sign = '【' .$ this ->sign. '】' ; //短信平台密码 $content=$sign.$ this ->content; //要发送的短信内容 $phone = $ this ->mobile; //要发送短信的手机号码 $sendurl = $smsapi. "sms?u=" .$user. "&p=" .$pass. "&m=" .$phone. "&c=" .urlencode($content); $result = $ this ->curl_get($sendurl); $res = []; if ($result == 0){ $res[ 'Code' ] = 'OK' ; } else { $res[ 'Code' ] = 'fail' ; $res[ 'Message' ] =$statusStr[$result]; } return $res; // return $result->toArray(); } catch (Exception $e){ return $e->getMessage() . PHP_EOL; } } private function curl_get($sendurl){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $sendurl); curl_setopt($ch, CURLOPT_TIMEOUT, 1); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[ "HTTP_USER_AGENT" ]); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书 // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书 $result = curl_exec($ch); curl_close($ch); return $result; } } |
好了经过以上的添加,短信宝的短信平台已经替换成功了,可以正常使用了
报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。
另外:我们已经开发好完整的likeshop系统短信宝插件,点击此链接 下载及查看安装流程。
最新更新
电商类
CMS类
微信类