├── .gitignore ├── composer.json ├── LICENSE ├── src ├── Qiniu.php ├── Qcloud.php ├── Upyun.php ├── Aliyun.php ├── Ucloud.php ├── SkSms.php └── Huawei.php ├── config └── config.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /.idea 3 | .DS_Store -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "singka/singka-sms", 3 | "description": "适用于ThinkPHP6.0的各种短信接口集成服务,本项目集成了各大云服务厂商的短信业务平台,支持ThinkPHP5.0、ThinkPHP5.1和ThinkPHP6.0,由宁波晟嘉网络科技有限公司维护,目前支持阿里云、腾讯云、七牛云、又拍云、Ucloud和华为云。", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "宁波晟嘉网络科技有限公司 夏慧新", 9 | "email": "shycomet@singka.email" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=7.0", 14 | "guzzlehttp/guzzle": "~6.0@dev", 15 | "alibabacloud/client": "^1.5", 16 | "singka/ucloud-sms": "^1.8", 17 | "qcloudsms/qcloudsms_php": "0.1.*", 18 | "qiniu/php-sdk": "^7.2" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "SingKa\\Sms\\" : "src/" 23 | } 24 | }, 25 | "extra": { 26 | "think": { 27 | "config": { 28 | "sms": "config/config.php" 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 宁波晟嘉网络科技有限公司 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /src/Qiniu.php: -------------------------------------------------------------------------------- 1 | 10 | // +---------------------------------------------------------------------- 11 | 12 | namespace SingKa\Sms; 13 | use Qiniu\Auth; 14 | use Qiniu\Sms\Sms; 15 | 16 | 17 | class Qiniu 18 | { 19 | protected $config; 20 | protected $status; 21 | protected $sms; 22 | 23 | public function __construct($config=[]) 24 | { 25 | $this->config = $config; 26 | if ($this->config['AccessKey'] == null || $this->config['SecretKey'] == null) { 27 | $this->status = false; 28 | } else { 29 | $this->status = true; 30 | $auth = new Auth($this->config['AccessKey'], $this->config['SecretKey']); 31 | $this->sms = new Sms($auth); 32 | } 33 | } 34 | 35 | public function send($name, $arguments) 36 | { 37 | if ($this->status) { 38 | $conf = $this->config['actions'][$name]; 39 | $phoneNumbers = $arguments[0]; 40 | $templateId = $conf['template_id']; 41 | $result = $this->sms->sendMessage($templateId, $phoneNumbers, $arguments[1]); 42 | if (isset($result[0]['job_id'])) { 43 | $data['code'] = 200; 44 | $data['msg'] = '发送成功'; 45 | } else { 46 | $data['code'] = 102; 47 | $data['msg'] = '发送失败'; 48 | } 49 | } else { 50 | $data['code'] = 103; 51 | $data['msg'] = '请在后台设置AccessKey和SecretKey'; 52 | } 53 | return $data; 54 | } 55 | } -------------------------------------------------------------------------------- /src/Qcloud.php: -------------------------------------------------------------------------------- 1 | 10 | // +---------------------------------------------------------------------- 11 | 12 | namespace SingKa\Sms; 13 | 14 | use Qcloud\Sms\SmsSingleSender; 15 | 16 | class Qcloud 17 | { 18 | protected $config; 19 | protected $status; 20 | protected $sms; 21 | 22 | public function __construct($config=[]) 23 | { 24 | $this->config = $config; 25 | if ($this->config['appid'] == null || $this->config['appkey'] == null) { 26 | $this->status = false; 27 | } else { 28 | $this->status = true; 29 | $this->sms = new SmsSingleSender($this->config['appid'], $this->config['appkey']); 30 | } 31 | } 32 | 33 | public function send($name, $arguments) 34 | { 35 | if ($this->status) { 36 | $sms = new SmsSingleSender($this->config['appid'], $this->config['appkey']); 37 | $conf = $this->config['actions'][$name]; 38 | $phoneNumbers = $arguments[0]; 39 | $templateId = $conf['template_id']; 40 | $smsSign = $this->config['sign_name']; 41 | $result = $sms->sendWithParam("86", $phoneNumbers, $templateId, $arguments[1], $smsSign, "", ""); 42 | $result = json_decode($result,true); 43 | if ($result['result'] == 0) { 44 | $data['code'] = 200; 45 | $data['msg'] = '发送成功'; 46 | } else { 47 | $data['code'] = $result['result']; 48 | $data['msg'] = '发送失败,'.$result['errmsg']; 49 | } 50 | } else { 51 | $data['code'] = 103; 52 | $data['msg'] = '请在后台设置appid和appkey'; 53 | } 54 | return $data; 55 | } 56 | } -------------------------------------------------------------------------------- /src/Upyun.php: -------------------------------------------------------------------------------- 1 | 10 | // +---------------------------------------------------------------------- 11 | 12 | namespace SingKa\Sms; 13 | 14 | class Upyun 15 | { 16 | protected $config; 17 | protected $status; 18 | 19 | public function __construct($config=[]) 20 | { 21 | $this->config = $config; 22 | if ($this->config['token'] == null) { 23 | $this->status = false; 24 | } else { 25 | $this->status = true; 26 | } 27 | } 28 | 29 | public function send($name, $arguments) 30 | { 31 | if ($this->status) { 32 | $conf = $this->config['actions'][$name]; 33 | $msg['mobile'] = $arguments[0]; 34 | $msg['template_id'] = $conf['template_id']; 35 | $msg['vars'] = $arguments[1]; 36 | $url = $this->config['apiurl'] ?: 'https://sms-api.upyun.com/api/messages'; 37 | $options = array( 38 | 'http' => array( 39 | 'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: ".$this->config['token'], 40 | 'method' => 'POST', 41 | 'content' => http_build_query($msg) 42 | ) 43 | ); 44 | $context = stream_context_create($options); 45 | $result = file_get_contents($url, false, $context); 46 | $result = json_decode($result,true); 47 | if (isset($result['message_ids']['0']['message_id'])) { 48 | $data['code'] = 200; 49 | $data['msg'] = '发送成功'; 50 | } else { 51 | $data['code'] = 102; 52 | $data['msg'] = '发送失败'; 53 | } 54 | } else { 55 | $data['code'] = 103; 56 | $data['msg'] = '请在后台设置Token'; 57 | } 58 | return $data; 59 | } 60 | } -------------------------------------------------------------------------------- /src/Aliyun.php: -------------------------------------------------------------------------------- 1 | 10 | // +---------------------------------------------------------------------- 11 | 12 | namespace SingKa\Sms; 13 | 14 | use AlibabaCloud\Client\AlibabaCloud; 15 | use AlibabaCloud\Client\Exception\ClientException; 16 | use AlibabaCloud\Client\Exception\ServerException; 17 | 18 | class Aliyun 19 | { 20 | protected $config; 21 | protected $status; 22 | 23 | public function __construct($config=[]) 24 | { 25 | $this->config = $config; 26 | if (empty($this->config['access_key']) || empty($this->config['access_secret'])) { 27 | $this->status = false; 28 | } else { 29 | $this->status = true; 30 | AlibabaCloud::accessKeyClient($this->config['access_key'], $this->config['access_secret']) 31 | ->regionId($this->config['region_id']) 32 | ->asDefaultClient(); 33 | } 34 | } 35 | 36 | public function send($name, $arguments) 37 | { 38 | if ($this->status) { 39 | $conf = $this->config['actions'][$name]; 40 | $result = AlibabaCloud::rpc() 41 | ->product('Dysmsapi') 42 | ->version($this->config['version']) 43 | ->action('SendSms') 44 | ->method('POST') 45 | ->host($this->config['host']) 46 | ->options([ 47 | 'query' => [ 48 | 'RegionId' => $this->config['region_id'], 49 | 'PhoneNumbers' => $arguments[0], 50 | 'SignName' => $this->config['sign_name'], 51 | 'TemplateCode' => $conf['template_id'], 52 | 'TemplateParam' => json_encode($arguments[1]), 53 | ], 54 | ]) 55 | ->request(); 56 | $result = $result->toArray(); 57 | if ($result['Code'] == "OK") { 58 | $data['code'] = 200; 59 | $data['msg'] = '发送成功'; 60 | } else { 61 | $data['code'] = $result['Code']; 62 | $data['msg'] = '发送失败,'.$result['Message']; 63 | } 64 | } else { 65 | $data['code'] = 103; 66 | $data['msg'] = '请在后台设置accessKeyId和accessKeySecret'; 67 | } 68 | return $data; 69 | } 70 | } -------------------------------------------------------------------------------- /src/Ucloud.php: -------------------------------------------------------------------------------- 1 | 10 | // +---------------------------------------------------------------------- 11 | 12 | namespace SingKa\Sms; 13 | 14 | use Singka\UcloudSms\UcloudApiClient; 15 | 16 | class Ucloud 17 | { 18 | protected $config; 19 | protected $status; 20 | protected $sms; 21 | 22 | public function __construct($config=[]) 23 | { 24 | $this->config = $config; 25 | if ($this->config['public_key'] == '' || $this->config['private_key'] == '' || $this->config['project_id'] == '') { 26 | $this->status = false; 27 | } else { 28 | $this->status = true; 29 | $this->sms = new UcloudApiClient($this->config['base_url'], $this->config['public_key'], $this->config['private_key'], $this->config['project_id']); 30 | } 31 | } 32 | 33 | public function send($name, $arguments) 34 | { 35 | if ($this->status) { 36 | $conf = $this->config['actions'][$name]; 37 | $params['Action'] = "SendUSMSMessage"; 38 | $phoneNumbers = $arguments[0]; 39 | if(is_array($phoneNumbers)){ 40 | foreach($phoneNumbers as $key => $val){ 41 | $params["PhoneNumbers.".$key] = $val; 42 | } 43 | }else{ 44 | $params['PhoneNumbers.0'] = $phoneNumbers; 45 | } 46 | $params["SigContent"] = $this->config['sign_name']; 47 | $templates = $arguments[1]; 48 | $params["TemplateId"] = $conf['template_id']; 49 | if(is_array($templates)){ 50 | foreach($templates as $key => $val) { 51 | $params["TemplateParams.".$key] = $val; 52 | } 53 | }else{ 54 | $params["TemplateParams.0"] = $templates; 55 | } 56 | $result = $this->sms->get("/", $params); 57 | if ($result['RetCode'] == 0) { 58 | $data['code'] = 200; 59 | $data['msg'] = '发送成功'; 60 | } else { 61 | $data['code'] = $result['RetCode']; 62 | $data['msg'] = '发送失败,'.$result['Message']; 63 | } 64 | } else { 65 | $data['code'] = 103; 66 | $data['msg'] = '请在后台设置PUBLIC_KEY和PRIVATE_KEY'; 67 | } 68 | return $data; 69 | } 70 | } -------------------------------------------------------------------------------- /src/SkSms.php: -------------------------------------------------------------------------------- 1 | 10 | // +---------------------------------------------------------------------- 11 | 12 | namespace SingKa\Sms; 13 | 14 | use SingKa\Sms\Aliyun; 15 | use SingKa\Sms\Qcloud; 16 | use SingKa\Sms\Ucloud; 17 | use SingKa\Sms\Qiniu; 18 | use SingKa\Sms\Upyun; 19 | use SingKa\Sms\Huawei; 20 | 21 | class SkSms 22 | { 23 | protected $type; 24 | protected $config; 25 | 26 | public function __construct($type,$config) 27 | { 28 | if($type == '' || is_array($config) == false) { 29 | $data['code'] = 101; 30 | $data['msg'] = '参数错误'; 31 | return $data; 32 | } else { 33 | $this->type = $type; 34 | $this->config = $config; 35 | } 36 | } 37 | 38 | public function __call($name, $arguments) 39 | { 40 | if (empty($this->config['actions'][$name])) { 41 | $data['code'] = 104; 42 | $data['msg'] = '没有找到操作类型:'.$name; 43 | return $data; 44 | exit(); 45 | } else { 46 | switch ($this->type) { 47 | case 'aliyun': 48 | $sms = new Aliyun($this->config); 49 | return $sms->send($name, $arguments); 50 | break; 51 | case 'qcloud': 52 | $sms = new Qcloud($this->config); 53 | return $sms->send($name, $arguments); 54 | break; 55 | case 'ucloud': 56 | $sms = new Ucloud($this->config); 57 | return $sms->send($name, $arguments); 58 | break; 59 | case 'qiniu': 60 | $sms = new Qiniu($this->config); 61 | return $sms->send($name, $arguments); 62 | break; 63 | case 'upyun': 64 | $sms = new Upyun($this->config); 65 | return $sms->send($name, $arguments); 66 | break; 67 | case 'huawei': 68 | $sms = new Huawei($this->config); 69 | return $sms->send($name, $arguments); 70 | break; 71 | default: 72 | $data['code'] = 102; 73 | $data['msg'] = 'type类型不存在'; 74 | return $data; 75 | } 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /src/Huawei.php: -------------------------------------------------------------------------------- 1 | 10 | // +---------------------------------------------------------------------- 11 | 12 | namespace SingKa\Sms; 13 | use GuzzleHttp\Psr7; 14 | use GuzzleHttp\Client; 15 | use GuzzleHttp\Exception\RequestException; 16 | 17 | class Huawei 18 | { 19 | protected $config; 20 | protected $status; 21 | 22 | public function __construct($config=[]) 23 | { 24 | if (empty($config['appKey']) || empty($config['appSecret']) || empty($config['sender'])) { 25 | $this->status = false; 26 | } else { 27 | $this->status = true; 28 | $this->config = $config; 29 | } 30 | } 31 | 32 | public function send($name, $arguments) 33 | { 34 | if ($this->status) { 35 | $client = new Client(); 36 | $conf = $this->config['actions'][$name]; 37 | try { 38 | $response = $client->request('POST', $this->config['url'].'/sms/batchSendSms/v1', [ 39 | 'form_params' => [ 40 | 'from' => $this->config['sender'], 41 | 'to' => $arguments[0], 42 | 'templateId' => $conf['template_id'], 43 | 'templateParas' => $arguments[1], 44 | 'statusCallback' => $this->config['statusCallback'], 45 | 'signature' => $this->config['signature'] 46 | ], 47 | 'headers' => [ 48 | 'Authorization' => 'WSSE realm="SDP",profile="UsernameToken",type="Appkey"', 49 | 'X-WSSE' => $this->buildWsseHeader($this->config['appKey'], $this->config['appSecret']) 50 | ], 51 | 'verify' => false //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 52 | ]); 53 | $result = $response->getBody(); 54 | $result = json_decode($result,true); 55 | } catch (RequestException $e) { 56 | $result = $e->getResponse()->getBody(); 57 | $result = json_decode($result,true); 58 | } 59 | if ($result['code'] == '000000') { 60 | $data['code'] = 200; 61 | $data['msg'] = '发送成功'; 62 | } else { 63 | $data['code'] = $result['code']; 64 | $data['msg'] = '发送失败,'.$result['description']; 65 | } 66 | } else { 67 | $data['code'] = 103; 68 | $data['msg'] = '配置有误,请检查'; 69 | } 70 | return $data; 71 | } 72 | 73 | /** 74 | * 构造X-WSSE参数值 75 | * @param string $appKey 76 | * @param string $appSecret 77 | * @return string 78 | */ 79 | public function buildWsseHeader(string $appKey, string $appSecret) 80 | { 81 | $now = date('Y-m-d\TH:i:s\Z'); //Created 82 | $nonce = uniqid(); //Nonce 83 | $base64 = base64_encode(hash('sha256', ($nonce . $now . $appSecret))); //PasswordDigest 84 | return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"", $appKey, $base64, $nonce, $now); 85 | } 86 | } -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | 10 | // +---------------------------------------------------------------------- 11 | return [ 12 | 'aliyun' => [ 13 | 'version' => '2017-05-25', 14 | 'host' => 'dysmsapi.aliyuncs.com', 15 | 'scheme' => 'http', 16 | 'region_id' => 'cn-hangzhou', 17 | 'access_key' => '', 18 | 'access_secret' => '', 19 | 'sign_name' => '', 20 | 'actions' => [ 21 | 'register' => [ 22 | 'actions_name' => '注册验证', 23 | 'template_id' => 'SMS_53115055', 24 | ], 25 | 'login' => [ 26 | 'actions_name' => '登录验证', 27 | 'template_id' => 'SMS_53115057', 28 | ], 29 | 'changePassword' => [ 30 | 'actions_name' => '修改密码', 31 | 'template_id' => 'SMS_53115053', 32 | ], 33 | 'changeUserinfo' => [ 34 | 'actions_name' => '变更信息', 35 | 'template_id' => 'SMS_53115052', 36 | ], 37 | ], 38 | ], 39 | 'ucloud' => [ 40 | 'public_key' => '', 41 | 'private_key' => '', 42 | 'project_id' => '', 43 | 'base_url' => 'https://api.ucloud.cn', 44 | 'sign_name' => '', 45 | 'actions' => [ 46 | 'register' => [ 47 | 'actions_name' => '注册验证', 48 | 'template_id' => 'UTA1910164E29F4', 49 | ], 50 | 'login' => [ 51 | 'actions_name' => '登录验证', 52 | 'template_id' => 'UTA1910164E29F4', 53 | ], 54 | 'changePassword' => [ 55 | 'actions_name' => '修改密码', 56 | 'template_id' => 'UTA1910164E29F4', 57 | ], 58 | 'changeUserinfo' => [ 59 | 'actions_name' => '变更信息', 60 | 'template_id' => 'UTA1910164E29F4', 61 | ], 62 | ], 63 | ], 64 | 'qcloud' => [ 65 | 'appid' => '', 66 | 'appkey' => '', 67 | 'sign_name' => '', 68 | 'actions' => [ 69 | 'register' => [ 70 | 'actions_name' => '注册验证', 71 | 'template_id' => '566198', 72 | ], 73 | 'login' => [ 74 | 'actions_name' => '登录验证', 75 | 'template_id' => '566197', 76 | ], 77 | 'changePassword' => [ 78 | 'actions_name' => '修改密码', 79 | 'template_id' => '566199', 80 | ], 81 | 'changeUserinfo' => [ 82 | 'actions_name' => '变更信息', 83 | 'template_id' => '566200', 84 | ], 85 | ], 86 | ], 87 | 'qiniu' => [ 88 | 'AccessKey' => '', 89 | 'SecretKey' => '', 90 | 'actions' => [ 91 | 'register' => [ 92 | 'actions_name' => '注册验证', 93 | 'template_id' => '1246849772845797376', 94 | ], 95 | 'login' => [ 96 | 'actions_name' => '登录验证', 97 | 'template_id' => '1246849654881001472', 98 | ], 99 | 'changePassword' => [ 100 | 'actions_name' => '修改密码', 101 | 'template_id' => '1246849964902977536', 102 | ], 103 | 'changeUserinfo' => [ 104 | 'actions_name' => '变更信息', 105 | 'template_id' => '1246849860733243392', 106 | ], 107 | ], 108 | ], 109 | 'upyun' => [ 110 | 'id' => '', 111 | 'token' => '', 112 | 'apiurl' => '', 113 | 'actions' => [ 114 | 'register' => [ 115 | 'actions_name' => '注册验证', 116 | 'template_id' => '2591', 117 | ], 118 | 'login' => [ 119 | 'actions_name' => '登录验证', 120 | 'template_id' => '2592', 121 | ], 122 | 'changePassword' => [ 123 | 'actions_name' => '修改密码', 124 | 'template_id' => '2590', 125 | ], 126 | 'changeUserinfo' => [ 127 | 'actions_name' => '变更信息', 128 | 'template_id' => '2589', 129 | ], 130 | ], 131 | ], 132 | 'huawei' => [ 133 | 'url' => '', 134 | 'appKey' => '', 135 | 'appSecret' => '', 136 | 'sender' => '', 137 | 'signature' => '', 138 | 'statusCallback' => '', 139 | 'actions' => [ 140 | 'register' => [ 141 | 'actions_name' => '注册验证', 142 | 'template_id' => '2591', 143 | ], 144 | 'login' => [ 145 | 'actions_name' => '登录验证', 146 | 'template_id' => '2592', 147 | ], 148 | 'changePassword' => [ 149 | 'actions_name' => '修改密码', 150 | 'template_id' => '2590', 151 | ], 152 | 'changeUserinfo' => [ 153 | 'actions_name' => '变更信息', 154 | 'template_id' => '2589', 155 | ], 156 | ], 157 | ] 158 | ]; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ThinkPHP6集成短信发送平台 2 | 3 | #### 介绍 4 | 本项目是集成了各大云服务厂商的短信业务平台,支持ThinkPHP5.0、ThinkPHP5.1和ThinkPHP6.0,由宁波晟嘉网络科技有限公司维护,目前支持阿里云、腾讯云、七牛云、又拍云、Ucloud和华为云,您如果有其他厂商的集成需求,请通过邮件联系作者提交需求。 5 | 6 | #### 安装教程 7 | 8 | 使用 `composer require singka/singka-sms` 命令行安装即可。 9 | 10 | 安装完成后会自动生成 `config/sms.php` 配置文件,内容如下: 11 | 12 | ```php 13 | 22 | // +---------------------------------------------------------------------- 23 | return [ 24 | 'aliyun' => [ 25 | 'version' => '2017-05-25', 26 | 'host' => 'dysmsapi.aliyuncs.com', 27 | 'scheme' => 'http', 28 | 'region_id' => 'cn-hangzhou', 29 | 'access_key' => '', 30 | 'access_secret' => '', 31 | 'sign_name' => '', 32 | 'actions' => [ 33 | 'register' => [ 34 | 'actions_name' => '注册验证', 35 | 'template_id' => 'SMS_53115055', 36 | ], 37 | 'login' => [ 38 | 'actions_name' => '登录验证', 39 | 'template_id' => 'SMS_53115057', 40 | ], 41 | 'changePassword' => [ 42 | 'actions_name' => '修改密码', 43 | 'template_id' => 'SMS_53115053', 44 | ], 45 | 'changeUserinfo' => [ 46 | 'actions_name' => '变更信息', 47 | 'template_id' => 'SMS_53115052', 48 | ], 49 | ], 50 | ], 51 | 'ucloud' => [ 52 | 'public_key' => '', 53 | 'private_key' => '', 54 | 'project_id' => '', 55 | 'base_url' => 'https://api.ucloud.cn', 56 | 'sign_name' => '', 57 | 'actions' => [ 58 | 'register' => [ 59 | 'actions_name' => '注册验证', 60 | 'template_id' => 'UTA1910164E29F4', 61 | ], 62 | 'login' => [ 63 | 'actions_name' => '登录验证', 64 | 'template_id' => 'UTA1910164E29F4', 65 | ], 66 | 'changePassword' => [ 67 | 'actions_name' => '修改密码', 68 | 'template_id' => 'UTA1910164E29F4', 69 | ], 70 | 'changeUserinfo' => [ 71 | 'actions_name' => '变更信息', 72 | 'template_id' => 'UTA1910164E29F4', 73 | ], 74 | ], 75 | ], 76 | 'qcloud' => [ 77 | 'appid' => '', 78 | 'appkey' => '', 79 | 'sign_name' => '', 80 | 'actions' => [ 81 | 'register' => [ 82 | 'actions_name' => '注册验证', 83 | 'template_id' => '566198', 84 | ], 85 | 'login' => [ 86 | 'actions_name' => '登录验证', 87 | 'template_id' => '566197', 88 | ], 89 | 'changePassword' => [ 90 | 'actions_name' => '修改密码', 91 | 'template_id' => '566199', 92 | ], 93 | 'changeUserinfo' => [ 94 | 'actions_name' => '变更信息', 95 | 'template_id' => '566200', 96 | ], 97 | ], 98 | ], 99 | 'qiniu' => [ 100 | 'AccessKey' => '', 101 | 'SecretKey' => '', 102 | 'actions' => [ 103 | 'register' => [ 104 | 'actions_name' => '注册验证', 105 | 'template_id' => '1246849772845797376', 106 | ], 107 | 'login' => [ 108 | 'actions_name' => '登录验证', 109 | 'template_id' => '1246849654881001472', 110 | ], 111 | 'changePassword' => [ 112 | 'actions_name' => '修改密码', 113 | 'template_id' => '1246849964902977536', 114 | ], 115 | 'changeUserinfo' => [ 116 | 'actions_name' => '变更信息', 117 | 'template_id' => '1246849860733243392', 118 | ], 119 | ], 120 | ], 121 | 'upyun' => [ 122 | 'id' => '', 123 | 'token' => '', 124 | 'apiurl' => '', 125 | 'actions' => [ 126 | 'register' => [ 127 | 'actions_name' => '注册验证', 128 | 'template_id' => '2591', 129 | ], 130 | 'login' => [ 131 | 'actions_name' => '登录验证', 132 | 'template_id' => '2592', 133 | ], 134 | 'changePassword' => [ 135 | 'actions_name' => '修改密码', 136 | 'template_id' => '2590', 137 | ], 138 | 'changeUserinfo' => [ 139 | 'actions_name' => '变更信息', 140 | 'template_id' => '2589', 141 | ], 142 | ], 143 | ], 144 | 'huawei' => [ 145 | 'url' => '', 146 | 'appKey' => '', 147 | 'appSecret' => '', 148 | 'sender' => '', 149 | 'signature' => '', 150 | 'statusCallback' => '', 151 | 'actions' => [ 152 | 'register' => [ 153 | 'actions_name' => '注册验证', 154 | 'template_id' => '2591', 155 | ], 156 | 'login' => [ 157 | 'actions_name' => '登录验证', 158 | 'template_id' => '2592', 159 | ], 160 | 'changePassword' => [ 161 | 'actions_name' => '修改密码', 162 | 'template_id' => '2590', 163 | ], 164 | 'changeUserinfo' => [ 165 | 'actions_name' => '变更信息', 166 | 'template_id' => '2589', 167 | ], 168 | ], 169 | ] 170 | ]; 171 | ``` 172 | 173 | #### 使用示例(基于ThinkPHP6.0) 174 | 175 | 176 | ```php 177 | 186 | // +---------------------------------------------------------------------- 187 | namespace app\home\controller; 188 | 189 | use SingKa\Sms\SkSms; 190 | use think\facade\Config; 191 | 192 | class Index 193 | { 194 | /** 195 | * 短信发送示例 196 | * 197 | * @mobile 短信发送对象手机号码 198 | * @action 短信发送场景,会自动传入短信模板 199 | * @parme 短信内容数组 200 | */ 201 | public function sendSms($mobile, $action, $parme) 202 | { 203 | //$this->SmsDefaultDriver是从数据库中读取的短信默认驱动 204 | $SmsDefaultDriver = $this->SmsDefaultDriver ?: 'aliyun'; 205 | //$this->SmsConfig是从数据库中读取的短信配置 206 | $config = $this->SmsConfig ?: Config::get('sms.'.$SmsDefaultDriver); 207 | $sms = new sksms($SmsDefaultDriver, $config);//传入短信驱动和配置信息 208 | //判断短信发送驱动,非阿里云和七牛云,需将内容数组主键序号化 209 | if ($this->SmsDefaultDriver == 'aliyun') { 210 | $result = $sms->$action($mobile, $parme); 211 | } elseif ($this->SmsDefaultDriver == 'qiniu') { 212 | $result = $sms->$action([$mobile], $parme); 213 | } elseif ($this->SmsDefaultDriver == 'upyun') { 214 | $result = $sms->$action($mobile, implode('|', $this->restoreArray($parme))); 215 | } else { 216 | $result = $sms->$action($mobile, $this->restoreArray($parme)); 217 | } 218 | if ($result['code'] == 200) { 219 | $data['code'] = 200; 220 | $data['msg'] = '短信发送成功'; 221 | } else { 222 | $data['code'] = $result['code']; 223 | $data['msg'] = $result['msg']; 224 | } 225 | return $data; 226 | } 227 | 228 | /** 229 | * 数组主键序号化 230 | * 231 | * @arr 需要转换的数组 232 | */ 233 | public function restoreArray($arr) 234 | { 235 | if (!is_array($arr)){ 236 | return $arr; 237 | } 238 | $c = 0; 239 | $new = []; 240 | foreach ($arr as $key => $value) { 241 | $new[$c] = $value; 242 | $c++; 243 | } 244 | return $new; 245 | } 246 | } 247 | ``` 248 | 249 | 返回的$result['code']的值等于200,说明短信发送成功,否则可以根据错误码和错误提示去各个云服务查找相关信息。 250 | 251 | #### 其他说明 252 | 253 | 返回的相关错误码请查阅:[Ucloud](https://docs.ucloud.cn/management_monitor/usms/error_code) 254 | [阿里云](https://help.aliyun.com/document_detail/101346.html?spm=a2c4g.11186623.6.621.31fd2246LCMXWw) 255 | [腾讯云](https://cloud.tencent.com/document/product/382/3771) 256 | [七牛云](https://developer.qiniu.com/sms/api/5849/sms-error-code) 257 | [又拍云](https://help.upyun.com/knowledge-base/sms-api-error-code/) 258 | [华为云](https://support.huaweicloud.com/devg-msgsms/sms_04_0009.html) 259 | 260 | --------------------------------------------------------------------------------