├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── src ├── Agents │ ├── ALiYunAgent.php │ ├── LuoSiMaoAgent.php │ ├── QQYunAgent.php │ ├── SubMailAgent.php │ ├── YunPianAgent.php │ └── YunTongXunAgent.php ├── Contracts │ └── Sms.php ├── Facades │ └── Sms.php ├── Factory.php ├── Manager.php ├── SmsServiceProvider.php └── config │ └── sms.php └── tests ├── PhpSmsUnitTest.php └── laravel5.5 └── PhpSmsUnitTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | /coverage 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog for Laravel Sms 2 | 3 | ## ## 1.0 (2017-3-28) 4 | 5 | - First versioned release of Laravel Sms 6 | 7 | ## ## 1.0 (2018-1-13) 8 | 9 | - Add cloud.tencent.com sms api 10 | - other optimized 11 | 12 | ## ## 1.0 (2018-1-14) 13 | 14 | - Add phpunit for laravel5.5 15 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2017 *.discovery 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel sms 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Build Status][ico-travis]][link-travis] 6 | [![Coverage Status][ico-scrutinizer]][link-scrutinizer] 7 | [![Quality Score][ico-code-quality]][link-code-quality] 8 | [![Total Downloads][ico-downloads]][link-downloads] 9 | 10 | 11 | 基于 laravel 5.3+ 开发的轻量化手机短信服务包,特点:简单,灵活 12 | 13 | 14 | > 支持:阿里云短信、云片网络、容联·云通讯、赛邮·云通讯、Luosimao、腾讯云短信 15 | 16 | > 适合场景:手机验证、订单消息、通知提醒等 17 | 18 | 19 | 20 | ## 安装 21 | 22 | Via Composer 23 | 24 | ``` php 25 | composer require laravelsms/sms:dev-master 26 | 27 | or 28 | 29 | composer require laravelsms/sms 30 | ``` 31 | 32 | composer.json 33 | 34 | 35 | ``` php 36 | "laravelsms/sms": "dev-master" 37 | ``` 38 | 39 | ## 配置 40 | 41 | ``` php 42 | //服务提供者 43 | 'providers' => [ 44 | // ... 45 | Laravelsms\Sms\SmsServiceProvider::class, 46 | ] 47 | 48 | //别名 49 | 'aliases' => [ 50 | //... 51 | 'Sms' => Laravelsms\Sms\Facades\Sms::class, 52 | ] 53 | 54 | //创建配置文件 55 | php artisan vendor:publish --tag=phpsms 56 | ``` 57 | 58 | 59 | ## 配置项(.env) 60 | 61 | > 以下为本程序所支持的短信代理平台的配置参考: 62 | 63 | 64 | ``` php 65 | 66 | //默认短信平台 67 | SMS_DEFAULT=aLiYun 68 | 69 | //备用短信平台(非自动化,仅作为变量提供,需自行程序处理,适合当主平台出现状况后使用。) 70 | SMS_FALLBACK=subMail 71 | 72 | //默认签名(重要:签名要用{}括起来) 73 | SMS_SIGNNAME={辣妈羊毛党} 74 | 75 | YUNPIAN_APIKEY=your-appkey 76 | YUNPIAN_TEMPLATECONTENT=模板内容 //参考:您的验证码是{verifyCode},有效期为{time}分钟,请尽快验证 77 | 78 | LUOSIMAO_APIKEY=your-appkey 79 | LUOSIMAO_TEMPLATECONTENT=模板内容 //参考:{verifyCode}是您请求的验证码 80 | 81 | SUBMAIL_APPID=your-appid 82 | SUBMAIL_APIKEY=your-appkey 83 | SUBMAIL_TEMPLATEID=模板ID //(project Id)参考:3OZtl 84 | 85 | YUNTONGXUN_ACCOUNTSID=your-account-sid 86 | YUNTONGXUN_ACCOUNTTOKEN=your-account-token 87 | YUNTONGXUN_APPID=your-appid 88 | YUNTONGXUN_TEMPLATEID=模板ID //参考:1~N 89 | 90 | ALIYUN_APPKEY=your-appkey 91 | ALIYUN_APPSECRET=your-appsecret 92 | ALIYUN_TEMPLATEID=模板ID //参考:SMS_57930028 93 | 94 | QQYUN_APPID=your-sdkappid 95 | QQYUN_APPKEY=your-appkey 96 | QQYUN_TEMPLATEID=模板ID //参考:76068 97 | 98 | ``` 99 | ## 使用示例 100 | 101 | ### 1、使用不同的短信平台 102 | 103 | ``` php 104 | //调用默认短信平台 105 | $smsDriver = Sms::driver(); 106 | 107 | //调用备用短信平台 108 | $smsDriver = Sms::driver('fallback'); 109 | 110 | $smsDriver = Sms::driver('aLiYun'); 111 | 112 | $smsDriver = Sms::driver('yunPian'); 113 | 114 | $smsDriver = Sms::driver('yunTongXun'); 115 | 116 | $smsDriver = Sms::driver('subMail'); 117 | 118 | $smsDriver = Sms::driver('luoSiMao'); 119 | 120 | $smsDriver = Sms::driver('qqYun'); 121 | 122 | ``` 123 | ### 2、程序自带标签变量说明 124 | 125 | ``` php 126 | {verifyCode} 模板数据验证码变量 127 | {time} 模板数据有效时间变量 128 | ['yzm' => 'verifyCode'] 说明:'yzm'表示官方模板变量名,'verifyCode' 表示使用程序自带的生成验证码功能 129 | ``` 130 | 131 | ### 3、用户定义模板说明(重要) 132 | 133 | > 官方模板变量格式多样,如###、#变量名#、@var(变量名)等,本包统一命名格式为:{变量名},变量名和位置应与官方模板变量名和位置保持一致 134 | 135 | 136 | ### 4、基本发送方式 137 | 138 | ``` php 139 | $mobile = '13*********'; //手机号 140 | ``` 141 | 142 | > 使用模板方式发送,无需设置content(如:容联·云通讯、赛邮·云通讯、阿里云短信、腾讯云短信) 143 | 144 | ``` php 145 | $templateVar = ['yzm' => 'verifyCode']; //verifyCode表示使用程序自动生成的验证码 146 | $smsDriver->setTemplateVar($templateVar, true); //替换模板变量,true表示返回键值对数组,false表示返回值数组 147 | $result = $smsDriver->singlesSend($mobile); //发送短信,返回结果 148 | 149 | ``` 150 | 151 | > 腾讯云短信,默认国家码为86,其它国家和地区的编码必须填写, 152 | > 国内短信格式:$mobile = '13******' 或 $mobile = ['86', '13*********'], 153 | > 其它国家和地区短信格式:$mobile = ['82', '016********'] 154 | 155 | ``` php 156 | $result = $smsDriver->singlesSend($mobile); 157 | 158 | or 159 | 160 | $mobile = ['82', '016********']; 161 | $result = $smsDriver->singlesSend($mobile); 162 | 163 | ``` 164 | 165 | > 使用内容方式发送,无需设置模板id和模板var(如:云片网络、luosimao) 166 | ``` php 167 | $smsDriver->setContentByVerifyCode(); 168 | //假设模板内容为:“{verifyCode}是您请求的验证码”,程序转化为:761888是您请求的验证码 169 | $result = $smsDriver->singlesSend($mobile); 170 | 171 | Or 172 | 173 | $smsDriver->setContentByVerifyCode(20); 174 | //假设模板内容为:“您的验证码是{verifyCode},有效期为{time}分钟”,程序转化为:您的验证码是761888,有效期为20分钟 175 | $result = $smsDriver->singlesSend($mobile); 176 | 177 | Or 178 | 179 | $content = '尊敬的用户,您的域名已到期,请及时续费'; //设置短信内容 180 | $smsDriver->setContent($content); 181 | $result = $smsDriver->singlesSend($mobile); 182 | ``` 183 | 184 | ### 5、组合发送方式 185 | 186 | > 设置签名 187 | ``` php 188 | $smsDriver->setSignName('雷神'); 189 | ``` 190 | 191 | > 设置内容 192 | ``` php 193 | $smsDriver->setContent($content); 194 | ``` 195 | 196 | > 替换内容中验证码变量(程序生成验证码方式) 197 | ``` php 198 | $smsDriver->setContentByVerifyCode(); 199 | ``` 200 | 201 | > 替换内容中验证码变量(程序生成验证码方式)及有效时间 202 | ``` php 203 | $smsDriver->setContentByVerifyCode(20); 204 | ``` 205 | 206 | > 替换内容中自定义变量(array $templateVar) 207 | ``` php 208 | $smsDriver->setContentByCustomVar($templateVar); 209 | ``` 210 | 211 | > 设置内容并替换内容的变量(array $templateVar) 212 | ``` php 213 | $content = '{name},您的帐号异地登录,如要不是你本人操作,请及时修改密码'; //设置短信内容 214 | $templateVar = ['name' => 'discovery']; //替换模板内容变量 215 | $smsDriver->setContent($content); 216 | $smsDriver->setContentByCustomVar($templateVar); 217 | //程序转化为:discovery,您的帐号异地登录,如要不是你本人操作,请及时修改密码 218 | ``` 219 | 220 | > 设置模板ID 221 | ``` php 222 | $smsDriver->setTemplateId(1); 223 | ``` 224 | 225 | > 替换模板变量,返回值数组(array $templateVar) 226 | ``` php 227 | $smsDriver->setTemplateVar($templateVar); 228 | 229 | //参考 230 | array(2) { 231 | [0]=> 232 | string(8) "931101" 233 | [1]=> 234 | string(4) "10" 235 | } 236 | ``` 237 | 238 | 239 | > 替换模板变量,true表示返回键值对数组(array $templateVar) 240 | ``` php 241 | $smsDriver->setTemplateVar($templateVar, true); 242 | 243 | //参考 244 | .array(2) { 245 | ["verifyCode"]=> 246 | string(6) "859432" 247 | ["time"]=> 248 | string(2) "15" 249 | } 250 | ``` 251 | 252 | > 发送 253 | ``` php 254 | $smsDriver->singlesSend($mobile); 255 | ``` 256 | 257 | > 返回拼接后的发送数据 258 | ``` php 259 | $smsDriver->singlesSend($mobile, false); 260 | ``` 261 | 262 | ### 6、更多组合详细请参见单元测试文件:PhpSmsUnitTest.php 263 | 264 | ### 7、判断短信发送成功还是失败 265 | 266 | >return array $result 返回数组结果 267 | 268 | >return int $result[].code 返回0则成功,返回其它则错误 269 | 270 | >return string $result[].msg 返回消息: "发送成功" Or 短信代理平台提示消息 271 | 272 | >return int $result[].verifyCode 返回程序自动生成的验证码,如果不使用程序自带的验证码生成功能,此变量值为NULL, 273 | 需自行传递验证码值 274 | 275 | 276 | ``` php 277 | array(3) { 278 | ["verifyCode"]=> 279 | int(977178) 280 | ["code"]=> 281 | int(0) 282 | ["msg"]=> 283 | string(12) "发送成功" 284 | } 285 | ``` 286 | 287 | ## Change log 288 | 289 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 290 | 291 | ## Testing 292 | 293 | 拷贝单元测试文件PhpSmsUnitTest.php到根目录tests文件中 294 | 295 | ``` bash 296 | $ vendor/bin/phpunit tests/PhpSmsUnitTest.php 297 | ``` 298 | 299 | ## Security 300 | 301 | > If you discover any security related issues, please email xzadv@126.com instead of using the issue tracker. 302 | 303 | > 如果你发现任何相关的问题,请把问题以邮件的形式发送至xzadv@126.com。 304 | 305 | 306 | ## License 307 | 308 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 309 | 310 | [ico-version]: https://img.shields.io/packagist/v/laravelsms/sms.svg?style=flat-square 311 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 312 | [ico-travis]: https://scrutinizer-ci.com/g/phper2013/laravel-sms/badges/build.png?b=master 313 | [ico-scrutinizer]: https://scrutinizer-ci.com/g/phper2013/laravel-sms/badges/quality-score.png?b=master 314 | [ico-code-quality]: https://scrutinizer-ci.com/g/phper2013/laravel-sms/badges/coverage.png?b=master 315 | [ico-downloads]: https://img.shields.io/packagist/dt/laravelsms/sms.svg?style=flat-square 316 | 317 | [link-packagist]: https://packagist.org/packages/laravelsms/sms 318 | [link-travis]: https://scrutinizer-ci.com/g/phper2013/laravel-sms/build-status/master 319 | [link-scrutinizer]: https://scrutinizer-ci.com/g/laravelsms/sms/code-structure 320 | [link-code-quality]: https://scrutinizer-ci.com/g/phper2013/laravel-sms/badges/coverage.png?b=master 321 | [link-downloads]: https://packagist.org/packages/laravelsms/sms 322 | [link-author]: https://github.com/phper2013 323 | [link-contributors]: ../../contributors 324 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravelsms/sms", 3 | "type": "package", 4 | "description": "Laravel 5 PhpSms Package", 5 | "keywords": ["laravel-sms", "laravel sms", "sms", "phpsms"], 6 | "homepage": "https://github.com/phper2013/laravel-sms", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "*.discovery", 11 | "email": "xzadv@126.com", 12 | "homepage": "https://github.com/phper2013", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "php": "~5.6|~7.0", 18 | "illuminate/support": "~5.1" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit" : "~4.0||~5.0", 22 | "squizlabs/php_codesniffer": "^2.3" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Laravelsms\\Sms\\": "src/" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "classmap": [ 31 | "tests" 32 | ], 33 | "psr-4": { 34 | "Laravelsms\\Sms\\": "tests/" 35 | } 36 | }, 37 | "scripts": { 38 | "test": "phpunit", 39 | "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", 40 | "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" 41 | }, 42 | "extra": { 43 | "branch-alias": { 44 | "dev-master": "1.0-dev" 45 | } 46 | }, 47 | "config": { 48 | "sort-packages": true 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Agents/ALiYunAgent.php: -------------------------------------------------------------------------------- 1 | config = $config; 23 | $this->transformConfig(); 24 | } 25 | 26 | protected function transformConfig() 27 | { 28 | $credentials = Arr::pull($this->config, 'credentials'); 29 | $this->appKey = Arr::pull($credentials, 'appKey'); 30 | $this->appSecret = Arr::pull($credentials, 'appSecret'); 31 | $this->setSignName(); 32 | $this->setTemplateId(Arr::pull($this->config, 'templateId')); 33 | } 34 | 35 | protected function percentEncode($str) 36 | { 37 | $res = urlencode($str); 38 | $res = preg_replace('/\+/', '%20', $res); 39 | $res = preg_replace('/\*/', '%2A', $res); 40 | $res = preg_replace('/%7E/', '~', $res); 41 | return $res; 42 | } 43 | 44 | private function computeSignature($parameters) 45 | { 46 | ksort($parameters); 47 | $canonicalizedQueryString = ''; 48 | foreach ($parameters as $key => $value) { 49 | $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value); 50 | } 51 | $stringToSign = $this->method . '&%2F&' . $this->percentEncode(substr($canonicalizedQueryString, 1)); 52 | $signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->appSecret . '&', true)); 53 | 54 | return $signature; 55 | } 56 | 57 | public function singlesSend($mobile, $send = true) 58 | { 59 | $url = $this->host; 60 | 61 | $requestParams = array( 62 | 'TemplateParam' => json_encode($this->templateVar), 63 | 'PhoneNumbers' => $mobile, 64 | 'SignName' => $this->signName, 65 | 'TemplateCode' => $this->templateId 66 | ); 67 | 68 | if ($send) { 69 | return $this->curl($url, $requestParams); 70 | } 71 | 72 | return $requestParams; 73 | } 74 | 75 | /** 76 | * @param $url 77 | * @param array $requestParams 78 | * @return array $result 79 | * @return int $result[].code 返回0则成功,返回其它则错误 80 | * @return string $result[].msg 返回消息 81 | * @return mixed $result[].verifyCode 验证码 82 | */ 83 | protected function curl($url, $requestParams) 84 | { 85 | // 注意使用GMT时间 86 | $timezone = date_default_timezone_get(); 87 | date_default_timezone_set("GMT"); 88 | $timestamp = date('Y-m-d\TH:i:s\Z'); 89 | date_default_timezone_set($timezone); 90 | 91 | // 其他请求参数公共参数 92 | $publicParams = array( 93 | 'RegionId' => $this->region, 94 | 'Format' => 'JSON', 95 | 'Version' => '2017-05-25', 96 | 'SignatureVersion' => '1.0', 97 | 'SignatureMethod' => 'HMAC-SHA1', 98 | 'SignatureNonce' => uniqid(), 99 | 'AccessKeyId' => $this->appKey, 100 | 'Timestamp' => $timestamp, 101 | 'Action' => $this->singleSendUrl 102 | ); 103 | 104 | $publicParams = array_merge($requestParams, $publicParams); 105 | $signature = $this->computeSignature($publicParams); 106 | $publicParams = array_merge(['Signature' => $signature], $publicParams); 107 | 108 | $ch = curl_init(); 109 | 110 | curl_setopt($ch, CURLOPT_URL, $url . http_build_query($publicParams)); 111 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); 112 | curl_setopt($ch, CURLOPT_FAILONERROR, false); 113 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 114 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 115 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 116 | $httpResponse = $this->httpResponse($ch); 117 | $result = $this->transformerResponse($httpResponse); 118 | curl_close($ch); 119 | 120 | return $result; 121 | } 122 | 123 | protected function transformerResponse($httpResponse) 124 | { 125 | if (empty($httpResponse['error'])) { 126 | $response = json_decode($httpResponse['jsonData'], true); 127 | if ($response['Code'] == 'OK') { 128 | $result = ['code' => 0, 'msg' => '发送成功', 'verifyCode' => $this->verifyCode]; 129 | } else { 130 | $result = ['code' => time(), 'msg' => $response['Code']]; 131 | } 132 | unset($response); 133 | } else { 134 | $result = ['code' => time(), 'msg' => $httpResponse['error']]; 135 | } 136 | 137 | return $result; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/Agents/LuoSiMaoAgent.php: -------------------------------------------------------------------------------- 1 | config = $config; 20 | $this->transformConfig(); 21 | } 22 | 23 | protected function transformConfig() 24 | { 25 | $this->apiKey = Arr::pull($this->config, 'apiKey'); 26 | $this->setSignName(); 27 | } 28 | 29 | public function singlesSend($mobile, $send = true) 30 | { 31 | $url = $this->host . $this->singleSendUrl; 32 | 33 | $postText = $this->content . "【{$this->signName}】"; 34 | 35 | $postData = [ 36 | 'mobile' => $mobile, 37 | 'message' => $postText, 38 | ]; 39 | 40 | if ($send) { 41 | return $this->curl($url, $postData); 42 | } 43 | 44 | return $postData; 45 | } 46 | 47 | /** 48 | * @param $url 49 | * @param array $postData 50 | * @return array $result 51 | * @return int $result[].code 返回0则成功,返回其它则错误 52 | * @return string $result[].msg 返回消息 53 | * @return mixed $result[].verifyCode 验证码 54 | */ 55 | protected function curl($url, $postData) 56 | { 57 | $ch = curl_init(); 58 | curl_setopt($ch, CURLOPT_URL, $url); 59 | curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 60 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 61 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 62 | curl_setopt($ch, CURLOPT_HEADER, FALSE); 63 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 64 | curl_setopt($ch, CURLOPT_USERPWD, $this->apiKey); 65 | curl_setopt($ch, CURLOPT_POST, TRUE); 66 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 67 | $httpResponse = $this->httpResponse($ch); 68 | $result = $this->transformerResponse($httpResponse); 69 | curl_close($ch); 70 | 71 | return $result; 72 | } 73 | 74 | protected function transformerResponse($httpResponse) 75 | { 76 | if (empty($httpResponse['error'])) { 77 | $response = json_decode($httpResponse['jsonData'], true); 78 | $result = ['code' => $response['error'], 'msg' => $response['msg'], 'verifyCode' => $this->verifyCode]; 79 | unset($response); 80 | } else { 81 | $result = ['code' => time(), 'msg' => $httpResponse['error']]; 82 | } 83 | 84 | return $result; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Agents/QQYunAgent.php: -------------------------------------------------------------------------------- 1 | config = $config; 21 | $this->transformConfig(); 22 | } 23 | 24 | protected function transformConfig() 25 | { 26 | $credentials = Arr::pull($this->config, 'credentials'); 27 | $this->appId = Arr::pull($credentials, 'appId'); 28 | $this->appKey = Arr::pull($credentials, 'appKey'); 29 | $this->setSignName(); 30 | $this->setTemplateId(Arr::pull($this->config, 'templateId')); 31 | $this->strRand = $this->makeRandom(); 32 | } 33 | 34 | private function computeSignature($parameters) 35 | { 36 | $canonicalizedQueryString = ''; 37 | foreach ($parameters as $key => $value) { 38 | $canonicalizedQueryString .= '&' . $key . '=' . $value; 39 | } 40 | 41 | return hash("sha256", "appkey=" . $this->appKey . $canonicalizedQueryString); 42 | } 43 | 44 | private function formatMobile($phoneNumber) 45 | { 46 | if (is_array($phoneNumber)) { 47 | $to = array( 48 | 'nationcode' => $phoneNumber[0], 49 | 'mobile' => $phoneNumber[1] 50 | ); 51 | } else { 52 | $to = array( 53 | 'nationcode' => '86', 54 | 'mobile' => $phoneNumber 55 | ); 56 | } 57 | 58 | return $to; 59 | } 60 | 61 | public function singlesSend($mobile, $send = true) 62 | { 63 | 64 | $mobile = $this->formatMobile($mobile); 65 | 66 | $url = "{$this->host}?sdkappid={$this->appId}&random={$this->strRand}"; 67 | 68 | $requestParams = array( 69 | 'tel' => $mobile, 70 | 'sign' => $this->signName, 71 | 'tpl_id' => (int)($this->templateId), 72 | 'params' => $this->templateVar 73 | ); 74 | 75 | if ($send) { 76 | return $this->curl($url, $requestParams); 77 | } 78 | 79 | return $requestParams; 80 | } 81 | 82 | /** 83 | * @param $url 84 | * @param array $requestParams 85 | * @return array $result 86 | * @return int $result[].code 返回0则成功,返回其它则错误 87 | * @return string $result[].msg 返回消息 88 | * @return mixed $result[].verifyCode 验证码 89 | */ 90 | protected function curl($url, $requestParams) 91 | { 92 | $timestamp = time(); 93 | 94 | $publicParams = array( 95 | 'time' => $timestamp, 96 | 'extend' => '', 97 | 'ext' => '' 98 | ); 99 | 100 | $parameters = array( 101 | 'random' => $this->strRand, 102 | 'time' => $timestamp, 103 | 'mobile' => $requestParams['tel']['mobile'] 104 | ); 105 | 106 | $signature = $this->computeSignature($parameters); 107 | $publicParams = array_merge(['sig' => $signature], $publicParams); 108 | $publicParams = array_merge($requestParams, $publicParams); 109 | 110 | $ch = curl_init(); 111 | 112 | curl_setopt($ch, CURLOPT_URL, $url); 113 | curl_setopt($ch, CURLOPT_HEADER, 0); 114 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 115 | curl_setopt($ch, CURLOPT_POST, 1); 116 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($publicParams)); 117 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 118 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 119 | $httpResponse = $this->httpResponse($ch); 120 | $result = $this->transformerResponse($httpResponse); 121 | curl_close($ch); 122 | 123 | return $result; 124 | } 125 | 126 | protected function transformerResponse($httpResponse) 127 | { 128 | if (empty($httpResponse['error'])) { 129 | $response = array_except( 130 | json_decode($httpResponse['jsonData'], true), 131 | ['ext', 'sid', 'fee'] 132 | ); 133 | if ($response['result'] === 0) { 134 | $result = ['code' => 0, 'msg' => '发送成功', 'verifyCode' => $this->verifyCode]; 135 | } else { 136 | $errmsg = 'result; ' . $response['result'] . ', errmsg: ' . $response['errmsg']; 137 | $result = ['code' => time(), 'msg' => $errmsg]; 138 | } 139 | unset($response); 140 | } else { 141 | $result = ['code' => time(), 'msg' => $httpResponse['error']]; 142 | } 143 | 144 | return $result; 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/Agents/SubMailAgent.php: -------------------------------------------------------------------------------- 1 | config = $config; 22 | $this->transformConfig(); 23 | } 24 | 25 | protected function transformConfig() 26 | { 27 | $credentials = Arr::pull($this->config, 'credentials'); 28 | $this->appId = Arr::pull($credentials, 'appid'); 29 | $this->apiKey = Arr::pull($credentials, 'apiKey'); 30 | $this->setTemplateId(Arr::pull($this->config, 'templateId')); 31 | $this->setSignName(); 32 | } 33 | 34 | public function singlesSend($mobile, $send = true) 35 | { 36 | $url = $this->host . $this->singleSendUrl; 37 | 38 | $timestamp = $this->getTimestamp(); 39 | 40 | $postData = [ 41 | 'appid' => $this->appId, 42 | 'to' => $mobile, 43 | 'project' => $this->templateId, 44 | 'vars' => json_encode($this->templateVar), 45 | 'timestamp' => $timestamp, 46 | 'sign_type' => 'sha1', 47 | ]; 48 | $signature = $this->computeSignature($postData); 49 | 50 | $postData = array_merge(['signature' => $signature], $postData); 51 | 52 | if ($send) { 53 | return $this->curl($url, $postData); 54 | } 55 | 56 | return $postData; 57 | } 58 | 59 | /** 60 | * @param $url 61 | * @param array $postData 62 | * @return array $result 63 | * @return int $result[].code 返回0则成功,返回其它则错误 64 | * @return string $result[].msg 返回消息 65 | * @return mixed $result[].verifyCode 验证码 66 | */ 67 | protected function curl($url, $postData) 68 | { 69 | $headers = array('X-HTTP-Method-Override: post'); 70 | 71 | $ch = curl_init(); 72 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); 73 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 74 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 75 | curl_setopt($ch, CURLOPT_URL, $url); 76 | curl_setopt($ch, CURLOPT_POST, true); 77 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 78 | $httpResponse = $this->httpResponse($ch); 79 | $result = $this->transformerResponse($httpResponse); 80 | curl_close($ch); 81 | 82 | return $result; 83 | } 84 | 85 | protected function transformerResponse($httpResponse) 86 | { 87 | if (empty($httpResponse['error'])) { 88 | $output = trim($httpResponse['jsonData'], "\xEF\xBB\xBF"); 89 | $response = json_decode($output, true); 90 | if ($response['status'] == 'success') { 91 | $result = ['code' => 0, 'msg' => '发送成功', 'verifyCode' => $this->verifyCode]; 92 | } else { 93 | $result = ['code' => $response['code'], 'msg' => $response['msg']]; 94 | } 95 | unset($response); 96 | } else { 97 | $result = ['code' => time(), 'msg' => $httpResponse['error']]; 98 | } 99 | 100 | return $result; 101 | } 102 | 103 | private function computeSignature($parameters) 104 | { 105 | ksort($parameters); 106 | reset($parameters); 107 | 108 | $canonicalizedQueryString = ""; 109 | foreach ($parameters as $key => $value) { 110 | $canonicalizedQueryString .= $key . "=" . $value . "&"; 111 | } 112 | $canonicalizedQueryString = rtrim($canonicalizedQueryString, '&'); 113 | 114 | $signature = sha1($this->appId . $this->apiKey . $canonicalizedQueryString . $this->appId . $this->apiKey); 115 | 116 | return $signature; 117 | } 118 | 119 | private function getTimestamp() 120 | { 121 | $url = $this->host . $this->timestampUrl; 122 | $ch = curl_init($url); 123 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 124 | curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 125 | $output = curl_exec($ch); 126 | $timestamp = json_decode($output, true); 127 | 128 | return $timestamp['timestamp']; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/Agents/YunPianAgent.php: -------------------------------------------------------------------------------- 1 | config = $config; 20 | $this->transformConfig(); 21 | } 22 | 23 | protected function transformConfig() 24 | { 25 | $this->apiKey = Arr::pull($this->config, 'apiKey'); 26 | $this->setSignName(); 27 | } 28 | 29 | public function singlesSend($mobile, $send = true) 30 | { 31 | $url = $this->host . $this->singleSendUrl; 32 | 33 | $postText = "【{$this->signName}】" . $this->content; 34 | 35 | $postData = [ 36 | 'text' => $postText, 37 | 'apikey' => $this->apiKey, 38 | 'mobile' => $mobile, 39 | ]; 40 | 41 | if ($send) { 42 | return $this->curl($url, $postData); 43 | } 44 | 45 | return $postData; 46 | } 47 | 48 | /** 49 | * @param $url 50 | * @param array $postData 51 | * @return array $result 52 | * @return int $result[].code 返回0则成功,返回其它则错误 53 | * @return string $result[].msg 返回消息 54 | * @return mixed $result[].verifyCode 验证码 55 | */ 56 | protected function curl($url, $postData) 57 | { 58 | $headers = array( 59 | 'Accept:text/plain;charset=utf-8', 60 | 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8' 61 | ); 62 | 63 | $ch = curl_init(); 64 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); 65 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 66 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 67 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 68 | curl_setopt($ch, CURLOPT_URL, $url); 69 | curl_setopt($ch, CURLOPT_POST, 1); 70 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); 71 | $httpResponse = $this->httpResponse($ch); 72 | $result = $this->transformerResponse($httpResponse); 73 | curl_close($ch); 74 | 75 | return $result; 76 | } 77 | 78 | protected function transformerResponse($httpResponse) 79 | { 80 | if (empty($httpResponse['error'])) { 81 | $result = array_except( 82 | json_decode($httpResponse['jsonData'], true), 83 | ['count', 'fee', 'sid', 'mobile', 'unit'] 84 | ); 85 | $result = array_merge(['verifyCode' => $this->verifyCode], $result); 86 | } else { 87 | $result = ['code' => time(), 'msg' => $httpResponse['error']]; 88 | } 89 | 90 | return $result; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/Agents/YunTongXunAgent.php: -------------------------------------------------------------------------------- 1 | config = $config; 24 | $this->transformConfig(); 25 | } 26 | 27 | protected function transformConfig() 28 | { 29 | $postDateTime = date("YmdHis"); 30 | 31 | $this->setTemplateId(Arr::pull($this->config, 'templateId')); 32 | 33 | $credentials = Arr::pull($this->config, 'credentials'); 34 | $this->accountSid = Arr::pull($credentials, 'accountSid'); 35 | $accountToken = Arr::pull($credentials, 'accountToken'); 36 | $this->appId = Arr::pull($credentials, 'appId'); 37 | $this->sigParameter = strtoupper(md5($this->accountSid . $accountToken . $postDateTime)); 38 | 39 | $authorization = base64_encode($this->accountSid . ":" . $postDateTime); 40 | $this->header = array( 41 | 'Content-Type:application/json;charset=utf-8', 42 | 'Accept:application/json', 43 | 'Authorization:' . $authorization 44 | ); 45 | } 46 | 47 | public function singlesSend($mobile, $send = true) 48 | { 49 | $url = $this->host . $this->singleSendUrl; 50 | $url = str_replace('{accountSid}', $this->accountSid, $url); 51 | $url = str_replace('{SigParameter}', $this->sigParameter, $url); 52 | 53 | $data = ''; 54 | while (list(, $value) = each($this->templateVar)) { 55 | $data .= $value . ","; 56 | } 57 | $data = rtrim($data, ','); 58 | 59 | $postData = "{'to':'$mobile','templateId':'$this->templateId','appId':'$this->appId','datas':[" . $data . "]}"; 60 | 61 | if ($send) { 62 | return $this->curl($url, $postData); 63 | } 64 | 65 | return $postData; 66 | } 67 | 68 | /** 69 | * @param $url 70 | * @param array $postData 71 | * @return array $result 72 | * @return int $result[].code 返回0则成功,返回其它则错误 73 | * @return string $result[].msg 返回消息 74 | * @return mixed $result[].verifyCode 验证码 75 | */ 76 | protected function curl($url, $postData) 77 | { 78 | $ch = curl_init(); 79 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); 80 | curl_setopt($ch, CURLOPT_URL, $url); 81 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 82 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 83 | curl_setopt($ch, CURLOPT_HEADER, 0); 84 | curl_setopt($ch, CURLOPT_POST, true); 85 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 86 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 87 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header); 88 | $httpResponse = $this->httpResponse($ch); 89 | $result = $this->transformerResponse($httpResponse); 90 | curl_close($ch); 91 | 92 | return $result; 93 | } 94 | 95 | protected function transformerResponse($httpResponse) 96 | { 97 | if (empty($httpResponse['error'])) { 98 | $response = json_decode($httpResponse['jsonData'], true); 99 | if ($response['statusCode'] == '000000') { 100 | $result = ['code' => 0, 'msg' => '发送成功', 'verifyCode' => $this->verifyCode]; 101 | } else { 102 | $result = ['code' => $response['statusCode'], 'msg' => 'YunTongXun:' . $response['statusCode']]; 103 | } 104 | unset($response); 105 | } else { 106 | $result = ['code' => time(), 'msg' => $httpResponse['error']]; 107 | } 108 | 109 | return $result; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/Contracts/Sms.php: -------------------------------------------------------------------------------- 1 | 0) { 20 | $agentFileName = end($formattedClassName); 21 | $agents = config("sms.agents"); 22 | foreach ($agents as $key => $value) { 23 | if (strcmp($value['executableFile'], $agentFileName) == 0) 24 | return $key; 25 | } 26 | } 27 | throw new \InvalidArgumentException("Unauthorized access."); 28 | } 29 | 30 | /** 31 | * @return string 32 | */ 33 | protected function getTemplateContentByConfig() 34 | { 35 | $name = $this->getAgentName(); 36 | return config("sms.agents.{$name}.templateContent"); 37 | } 38 | 39 | /** 40 | * @param integer $time 41 | */ 42 | public function setContentByVerifyCode($time = null) 43 | { 44 | $this->verifyCode = $this->makeRandom(); 45 | if (empty($this->content)) { 46 | $this->content = $this->getTemplateContentByConfig(); 47 | } 48 | $this->content = str_replace('{verifyCode}', $this->verifyCode, $this->content); 49 | if (!empty($time)) { 50 | $this->content = str_replace('{time}', $time, $this->content); 51 | } 52 | } 53 | 54 | /** 55 | * @param array $templateVar 56 | */ 57 | public function setContentByCustomVar($templateVar = []) 58 | { 59 | if (empty($this->content)) { 60 | $this->content = $this->getTemplateContentByConfig(); 61 | } 62 | 63 | $count = count($templateVar); 64 | if (is_array($templateVar) && $count > 0) { 65 | foreach ($templateVar as $key => $value) { 66 | $this->content = str_replace("{" . $key . "}", $value, $this->content); 67 | } 68 | } else { 69 | $this->content = ''; 70 | } 71 | } 72 | 73 | /** 74 | * @param string $content 75 | */ 76 | public function setContent($content) 77 | { 78 | $this->content = is_string(trim($content)) ? $content : ''; 79 | } 80 | 81 | /** 82 | * @return string 83 | */ 84 | public function getContent() 85 | { 86 | return $this->content; 87 | } 88 | 89 | /** 90 | * @param array $templateVar 91 | * @param bool $hasKey 92 | */ 93 | public function setTemplateVar($templateVar = [], $hasKey = false) 94 | { 95 | $count = count($templateVar); 96 | 97 | if (is_array($templateVar) && $count > 0) { 98 | foreach ($templateVar as $key => $value) { 99 | if ($hasKey) { 100 | if ($value == 'verifyCode') { 101 | $this->verifyCode = $this->makeRandom(); 102 | $this->templateVar[$key] = "{$this->verifyCode}"; 103 | } else { 104 | $this->templateVar[$key] = "{$value}"; 105 | } 106 | } else { 107 | if ($value == 'verifyCode') { 108 | $this->verifyCode = $this->makeRandom(); 109 | $this->templateVar[] = "{$this->verifyCode}"; 110 | } else { 111 | $this->templateVar[] = "{$value}"; 112 | } 113 | } 114 | } 115 | } else { 116 | $this->templateVar = []; 117 | } 118 | } 119 | 120 | /** 121 | * @return string 122 | */ 123 | public function getTemplateVar() 124 | { 125 | return $this->templateVar; 126 | } 127 | 128 | /** 129 | * @param string $signName 130 | */ 131 | public function setSignName($signName = null) 132 | { 133 | $this->signName = trim($signName) ?: trim(config('sms.signName'), '{}'); 134 | } 135 | 136 | /** 137 | * @return string 138 | */ 139 | public function getSignName() 140 | { 141 | return $this->signName; 142 | } 143 | 144 | /** 145 | * @param mixed $id 146 | */ 147 | public function setTemplateId($id = null) 148 | { 149 | $this->templateId = $id ?: 1; 150 | } 151 | 152 | /** 153 | * @return string 154 | */ 155 | public function getTemplateId() 156 | { 157 | return $this->templateId; 158 | } 159 | 160 | /** 161 | * @return void 162 | */ 163 | abstract protected function transformConfig(); 164 | 165 | /** 166 | * @param string $mobile 167 | * @param bool $send 168 | * @return mixed 169 | */ 170 | abstract protected function singlesSend($mobile, $send = true); 171 | 172 | /** 173 | * @param string $url 174 | * @param array $params 175 | * @return array 176 | */ 177 | abstract protected function curl($url, $params); 178 | 179 | /** 180 | * @param $ch 181 | * @return array 182 | */ 183 | abstract protected function transformerResponse($ch); 184 | 185 | /** 186 | * @param $ch 187 | * @return array 188 | */ 189 | protected function httpResponse($ch) 190 | { 191 | $retry = 0; 192 | do { 193 | $jsonData = curl_exec($ch); 194 | $retry++; 195 | } while (curl_errno($ch) && $retry < 3); 196 | 197 | if (curl_errno($ch)) { 198 | $response = ['error' => 1, 'msg' => curl_error($ch)]; 199 | } else { 200 | $response = ['error' => 0, 'jsonData' => $jsonData]; 201 | } 202 | 203 | return $response; 204 | } 205 | 206 | /** 207 | * @return int 208 | */ 209 | protected function makeRandom() 210 | { 211 | return random_int(100000, 999999); 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /src/Facades/Sms.php: -------------------------------------------------------------------------------- 1 | app = $app; 12 | } 13 | 14 | public function getFactories() 15 | { 16 | $agents = $this->app['config']['sms.agents']; 17 | 18 | $factories = []; 19 | 20 | foreach ($agents as $key => $value) { 21 | $factories[$key] = __NAMESPACE__ . '\Agents\\' . $value['executableFile']; 22 | } 23 | 24 | return $factories; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Manager.php: -------------------------------------------------------------------------------- 1 | app = $app; 15 | $this->factories = new Factory($app); 16 | } 17 | 18 | public function driver($name = null) 19 | { 20 | $factories = $this->factories->getFactories(); 21 | 22 | if ($name == 'fallback') { 23 | $name = $this->getFallbackDriver(); 24 | } else { 25 | $name = $name ?: $this->getDefaultDriver(); 26 | } 27 | 28 | if (!array_key_exists($name, $factories)) { 29 | throw new InvalidArgumentException("Driver '$name' is not supported."); 30 | } 31 | 32 | $className = $factories[$name]; 33 | $config = $this->getConfig($name); 34 | 35 | return new $className($config); 36 | } 37 | 38 | protected function getConfig($name) 39 | { 40 | return $this->app['config']["sms.agents.{$name}"]; 41 | } 42 | 43 | protected function getDefaultDriver() 44 | { 45 | return $this->app['config']['sms.default']; 46 | } 47 | 48 | protected function getFallbackDriver() 49 | { 50 | return $this->app['config']['sms.fallback']; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/SmsServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 25 | __DIR__ . '/config/sms.php' => config_path('sms.php'), 26 | ], 'phpsms'); 27 | } 28 | 29 | /** 30 | * Register the application services. 31 | * 32 | * @return void 33 | */ 34 | public function register() 35 | { 36 | $this->app->singleton('sms', function () { 37 | return new Manager($this->app); 38 | }); 39 | } 40 | 41 | /** 42 | * Get the services provided by the provider. 43 | * 44 | * @return array 45 | */ 46 | public function provides() 47 | { 48 | return ['sms']; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/config/sms.php: -------------------------------------------------------------------------------- 1 | env('SMS_DEFAULT'), 16 | 17 | 'fallback' => env('SMS_FALLBACK'), 18 | 19 | 'signName' => env('SMS_SIGNNAME'), 20 | 21 | 'agents' => [ 22 | 23 | 'aLiYun' => [ 24 | 'credentials' => [ 25 | 'appKey' => env('ALIYUN_APPKEY'), 26 | 'appSecret' => env('ALIYUN_APPSECRET') 27 | ], 28 | 'templateId' => env('ALIYUN_TEMPLATEID'), 29 | 'executableFile' => 'ALiYunAgent', 30 | ], 31 | 32 | 'yunPian' => [ 33 | 'apiKey' => env('YUNPIAN_APIKEY'), 34 | 'templateContent' => env('YUNPIAN_TEMPLATECONTENT'), 35 | 'executableFile' => 'YunPianAgent', 36 | ], 37 | 38 | 'yunTongXun' => [ 39 | 'credentials' => [ 40 | 'accountSid' => env('YUNTONGXUN_ACCOUNTSID'), 41 | 'accountToken' => env('YUNTONGXUN_ACCOUNTTOKEN'), 42 | 'appId' => env('YUNTONGXUN_APPID'), 43 | ], 44 | 'templateId' => env('YUNTONGXUN_TEMPLATEID'), 45 | 'executableFile' => 'YunTongXunAgent', 46 | ], 47 | 48 | 'subMail' => [ 49 | 'credentials' => [ 50 | 'appid' => env('SUBMAIL_APPID'), 51 | 'apiKey' => env('SUBMAIL_APIKEY'), 52 | ], 53 | 'templateId' => env('SUBMAIL_TEMPLATEID'), 54 | 'executableFile' => 'SubMailAgent', 55 | ], 56 | 57 | 'luoSiMao' => [ 58 | 'apiKey' => env('LUOSIMAO_APIKEY'), 59 | 'templateContent' => env('LUOSIMAO_TEMPLATECONTENT'), 60 | 'executableFile' => 'LuoSiMaoAgent', 61 | ], 62 | 63 | 'qqYun' => [ 64 | 'credentials' => [ 65 | 'appId' => env('QQYUN_APPID'), 66 | 'appKey' => env('QQYUN_APPKEY') 67 | ], 68 | 'templateId' => env('QQYUN_TEMPLATEID'), 69 | 'executableFile' => 'QQYunAgent', 70 | ], 71 | 72 | ], 73 | 74 | ]; 75 | -------------------------------------------------------------------------------- /tests/PhpSmsUnitTest.php: -------------------------------------------------------------------------------- 1 | app = $this->createApplication(); 13 | } 14 | 15 | public function manager() 16 | { 17 | return new Manager($this->app); 18 | } 19 | 20 | public function testDriver() 21 | { 22 | $manager = $this->manager(); 23 | $smsDriver = $manager->driver('yunTongXun'); 24 | $isObject = is_object($smsDriver); 25 | 26 | $this->assertTrue($isObject); 27 | } 28 | 29 | public function testGetDefaultSignName() 30 | { 31 | $signName = '辣妈羊毛党'; 32 | 33 | $manager = $this->manager(); 34 | $smsDriver = $manager->driver('subMail'); 35 | $smsDriver->setSignName(); 36 | 37 | $this->assertEquals($signName, $smsDriver->getSignName()); 38 | } 39 | 40 | public function testSetSignName() 41 | { 42 | $signName = '钢铁战士'; 43 | 44 | $manager = $this->manager(); 45 | $smsDriver = $manager->driver('subMail'); 46 | $smsDriver->setSignName($signName); 47 | 48 | $this->assertEquals($signName, $smsDriver->getSignName()); 49 | } 50 | 51 | public function testSetTemplateId() 52 | { 53 | $id = 2; 54 | 55 | $manager = $this->manager(); 56 | $smsDriver = $manager->driver('yunTongXun'); 57 | $smsDriver->setTemplateId($id); 58 | 59 | $this->assertEquals($id, $smsDriver->getTemplateId()); 60 | } 61 | 62 | public function testFactories() 63 | { 64 | $factory = new Factory($this->app); 65 | $factories = $factory->getFactories(); 66 | 67 | $this->assertArrayHasKey('aLiYun', $factories); 68 | } 69 | 70 | public function testSetTemplateVarByDefault() 71 | { 72 | $templateVar = ['1' => 'verifyCode', '2' => 10]; 73 | 74 | $manager = $this->manager(); 75 | $smsDriver = $manager->driver('yunTongXun'); 76 | $smsDriver->setTemplateVar($templateVar); 77 | 78 | $this->assertContains('10', $smsDriver->getTemplateVar()); 79 | } 80 | 81 | public function testSetTemplateVarByHasKey() 82 | { 83 | $templateVar = ['verifyCode' => 'verifyCode', 'time' => 15]; 84 | 85 | $manager = $this->manager(); 86 | $smsDriver = $manager->driver('subMail'); 87 | $smsDriver->setTemplateVar($templateVar, true); 88 | 89 | $this->assertArrayHasKey('verifyCode', $smsDriver->getTemplateVar()); 90 | } 91 | 92 | public function testSetTemplateVarByCustomVar() 93 | { 94 | $templateVar = ['1' => '123456', '2' => 21]; 95 | $get_templateVar = ['0' => '123456', '1' => 21]; 96 | 97 | $manager = $this->manager(); 98 | $smsDriver = $manager->driver('aLiYun'); 99 | $smsDriver->setTemplateVar($templateVar); 100 | 101 | $this->assertEquals($get_templateVar, $smsDriver->getTemplateVar()); 102 | } 103 | 104 | public function testSetContentByVerifyCode() 105 | { 106 | $manager = $this->manager(); 107 | $smsDriver = $manager->driver('luoSiMao'); 108 | $smsDriver->setContentByVerifyCode(); 109 | 110 | $this->assertTrue(true); 111 | } 112 | 113 | public function testSetContentByVerifyCodeAndTime() 114 | { 115 | $manager = $this->manager(); 116 | $smsDriver = $manager->driver('yunPian'); 117 | $smsDriver->setContentByVerifyCode(20); 118 | 119 | $this->assertContains('20',$smsDriver->getContent()); 120 | } 121 | 122 | public function testSetContentByCustomVar() 123 | { 124 | $templateVar = ['verifyCode' => random_int(100000, 999999), 'time' => 360]; 125 | 126 | $manager = $this->manager(); 127 | $smsDriver = $manager->driver('yunPian'); 128 | $smsDriver->setContentByCustomVar($templateVar); 129 | 130 | $this->assertContains('360', $smsDriver->getContent()); 131 | } 132 | 133 | public function testSetContent() 134 | { 135 | $content = '您的帐号异地登录,如要不是你本人操作,请及时修改密码'; //设置短信内容 136 | 137 | $manager = $this->manager(); 138 | $smsDriver = $manager->driver('yunPian'); 139 | $smsDriver->setContent($content); 140 | 141 | $this->assertEquals($content, $smsDriver->getContent()); 142 | } 143 | 144 | public function testSetContentAndSetContentByCustomVar() 145 | { 146 | $content = '{name}, 您的帐号异地登录,如要不是你本人操作,请及时修改密码'; //设置短信内容 147 | $templateVar = ['name' => 'discovery']; 148 | $get_content = 'discovery, 您的帐号异地登录,如要不是你本人操作,请及时修改密码'; 149 | 150 | $manager = $this->manager(); 151 | $smsDriver = $manager->driver('luoSiMao'); 152 | $smsDriver->setContent($content); 153 | $smsDriver->setContentByCustomVar($templateVar); 154 | 155 | $this->assertEquals($get_content, $smsDriver->getContent()); 156 | } 157 | 158 | /******************************* 以下请根据实际需要打开测试 ******************************************/ 159 | 160 | /******************************* 返回拼接后的待发送数据 分割线 ***************************************/ 161 | 162 | /** 163 | * 【ALiYun】 164 | * 官方模板格式:${verifyCode}是您请求的验证码 165 | * 如果使用系统内验证码,请使用verifyCode标签 166 | */ 167 | public function testGetALiYunData() 168 | { 169 | var_dump('【ALiYun】'); 170 | 171 | $templateVar = ['verifyCode' => 'verifyCode']; 172 | 173 | $manager = $this->manager(); 174 | $smsDriver = $manager->driver('aLiYun'); 175 | $smsDriver->setSignName('雷神'); 176 | $smsDriver->setTemplateVar($templateVar, true); 177 | $result = $smsDriver->singlesSend($this->mobile, false); 178 | 179 | var_dump($result); 180 | $this->assertTrue(true); 181 | } 182 | 183 | /** 184 | * 【luoSiMao】返回拼接后的待发送数据 185 | * 不支持营销短信、全变量短信模板 186 | * 官方模板格式:###是您请求的验证码【{signName}】,代码中签名在后面,发送到手机上自动变为在前面。 187 | * 程序中替换变量名:{verifyCode}是您请求的验证码 188 | * 请用标准变量名代替###,以方便程序拼接数据,变量顺序和官方模板###位置顺序保持一致 189 | */ 190 | public function testGetLuoSiMaoData() 191 | { 192 | var_dump('【luoSiMao】'); 193 | 194 | $manager = $this->manager(); 195 | $smsDriver = $manager->driver('luoSiMao'); 196 | $smsDriver->setContentByVerifyCode(); 197 | $result = $smsDriver->singlesSend($this->mobile, false); 198 | 199 | var_dump($result); 200 | $this->assertTrue(true); 201 | } 202 | 203 | /** 204 | * 【赛邮云通信xsend发送方式】返回拼接后的待发送数据 205 | * 官方模板格式:您的验证码是@var(verifyCode),有效期为@var(minute)分钟,请尽快验证 206 | * 如果使用系统默认验证码,请使用verifyCode标签 207 | */ 208 | public function testGetSubMailData() 209 | { 210 | var_dump('【赛邮云通信】'); 211 | 212 | $templateVar = ['verifyCode' => random_int(100000, 999999), 'minute' => 60]; 213 | 214 | $manager = $this->manager(); 215 | $smsDriver = $manager->driver('subMail'); 216 | $smsDriver->setSignName('辣妈测试'); 217 | $smsDriver->setTemplateVar($templateVar, true); 218 | $result = $smsDriver->singlesSend($this->mobile, false); 219 | 220 | var_dump($result); 221 | $this->assertTrue(true); 222 | } 223 | 224 | /** 225 | * 【云片网】返回拼接后的待发送数据 226 | * 官方模板格式:您的验证码是#code#,有效期为#time#分钟,请尽快验证 227 | * 程序中替换变量名:您的验证码是{verifyCode},有效期为{time}分钟,请尽快验证 228 | */ 229 | public function testGetYunPianData() 230 | { 231 | var_dump('【云片网】'); 232 | 233 | $manager = $this->manager(); 234 | $smsDriver = $manager->driver('yunPian'); 235 | $smsDriver->setContentByVerifyCode(20); 236 | $result = $smsDriver->singlesSend($this->mobile, false); 237 | 238 | var_dump($result); 239 | $this->assertTrue(true); 240 | } 241 | 242 | /** 243 | * 【云通讯】返回拼接后的待发送数据 244 | * 官方测试模板ID:1 245 | * 官方测试模板:【云通讯】您使用的是云通讯短信模板,您的验证码是{1},请于{2}分钟内正确输入。 246 | * 如果使用系统默认验证码,请使用verifyCode标签 247 | */ 248 | public function testGetYunTongXunData() 249 | { 250 | var_dump('【云通讯】'); 251 | 252 | $templateVar = ['1' => 'verifyCode', '2' => 15]; 253 | 254 | $manager = $this->manager(); 255 | $smsDriver = $manager->driver('yunTongXun'); 256 | $smsDriver->setTemplateId(1); 257 | $smsDriver->setTemplateVar($templateVar); 258 | $result = $smsDriver->singlesSend($this->mobile, false); 259 | 260 | var_dump($result); 261 | $this->assertTrue(true); 262 | } 263 | 264 | /** 265 | * 【腾讯云短信 cloud.tencent.com】 266 | * 官方模板格式:验证码{1},请于{2}分钟内填写。如非本人操作,请忽略。 267 | * 如果使用系统内验证码,请使用verifyCode标签 268 | * 默认国家码为86,其它国家和地区的编码必须填写 269 | * 国内短信格式:$mobile = '13******' 或 $mobile = ['86', '13*********'] 270 | * 其它国家和地区短信格式:$mobile = ['82', '016********'] 271 | */ 272 | public function testGetQQYunData() 273 | { 274 | var_dump('【QQYun】'); 275 | 276 | $templateVar = ['1' => 'verifyCode', '2' => 15]; 277 | 278 | $manager = $this->manager(); 279 | $smsDriver = $manager->driver('qqYun'); 280 | $smsDriver->setSignName('雷神的号'); 281 | $smsDriver->setTemplateVar($templateVar); 282 | $result = $smsDriver->singlesSend($this->mobile, false); 283 | 284 | var_dump($result); 285 | $this->assertTrue(true); 286 | } 287 | 288 | /******************************* 实际发送 分割线 **********************************************/ 289 | 290 | /** 291 | * 【ALiYun】 292 | * 官方模板格式:${verifyCode}是您请求的验证码 293 | * 如果使用系统内验证码,请使用verifyCode标签 294 | */ 295 | // public function testALiYunAgent() 296 | // { 297 | // var_dump('【ALiYun】'); 298 | // 299 | // $templateVar = ['verifyCode' => 'verifyCode']; 300 | // 301 | // $manager = $this->manager(); 302 | // $smsDriver = $manager->driver('aLiYun'); 303 | // $smsDriver->setSignName('雷神'); 304 | // $smsDriver->setTemplateVar($templateVar, true); 305 | // $result = $smsDriver->singlesSend($this->mobile); 306 | // 307 | // var_dump($result); 308 | // $this->assertTrue(true); 309 | // } 310 | 311 | /** 312 | * 【luoSiMao】 313 | * 不支持营销短信、全变量短信模板 314 | * 官方模板格式:###是您请求的验证码【{signName}】,代码中签名在后面,发送到手机上自动变为在前面。 315 | * 程序中替换变量名:{verifyCode}是您请求的验证码 316 | * 请用标准变量名代替###,以方便程序拼接数据,变量顺序和官方模板###位置顺序保持一致 317 | */ 318 | // public function testLuoSiMaoAgent() 319 | // { 320 | // var_dump('【luoSiMao】'); 321 | // 322 | // $manager = $this->manager(); 323 | // $smsDriver = $manager->driver('luoSiMao'); 324 | // $smsDriver->setContentByVerifyCode(); 325 | // $result = $smsDriver->singlesSend($this->mobile); 326 | // 327 | // var_dump($result); 328 | // $this->assertTrue(true); 329 | // } 330 | 331 | // public function testLuoSiMaoAgentByCustomContent() 332 | // { 333 | // var_dump('【luoSiMao Custom Content】'); 334 | // 335 | // $content = '{name},您的帐号异地登录,如要不是你本人操作,请及时修改密码'; //设置短信内容 336 | // $templateVar = ['name' => 'discovery']; 337 | // 338 | // $manager = $this->manager(); 339 | // $smsDriver = $manager->driver('luoSiMao'); 340 | // $smsDriver->setContent($content); 341 | // $smsDriver->setContentByCustomVar($templateVar); 342 | // $result = $smsDriver->singlesSend($this->mobile); 343 | // 344 | // var_dump($result); 345 | // $this->assertTrue(true); 346 | // } 347 | 348 | /** 349 | * 【赛邮云通信xsend发送方式】 350 | * 官方模板格式:您的验证码是@var(verifyCode),有效期为@var(minute)分钟,请尽快验证 351 | * 如果使用系统默认验证码,请使用verifyCode标签 352 | */ 353 | // public function testSubMailAgent() 354 | // { 355 | // var_dump('【赛邮云通信】'); 356 | // 357 | // $templateVar = ['verifyCode' => random_int(100000, 999999), 'minute' => 60]; 358 | // 359 | // $manager = $this->manager(); 360 | // $smsDriver = $manager->driver('subMail'); 361 | // $smsDriver->setSignName('辣妈测试'); 362 | // $smsDriver->setTemplateVar($templateVar, true); 363 | // $result = $smsDriver->singlesSend($this->mobile); 364 | // 365 | // var_dump($templateVar['verifyCode']); 366 | // var_dump($result); 367 | // $this->assertTrue(true); 368 | // } 369 | 370 | /** 371 | * 【云片网】 372 | * 官方模板格式:您的验证码是#code#,有效期为#time#分钟,请尽快验证 373 | * 程序中替换变量名:您的验证码是{verifyCode},有效期为{time}分钟,请尽快验证 374 | */ 375 | // public function testYunPianAgent() 376 | // { 377 | // var_dump('【云片网】'); 378 | // 379 | // $manager = $this->manager(); 380 | // $smsDriver = $manager->driver('yunPian'); 381 | // $smsDriver->setContentByVerifyCode(20); 382 | // $result = $smsDriver->singlesSend($this->mobile); 383 | // 384 | // var_dump($result); 385 | // $this->assertTrue(true); 386 | // } 387 | 388 | /** 389 | * 【云通讯】 390 | * 官方测试模板ID:1 391 | * 官方测试模板:【云通讯】您使用的是云通讯短信模板,您的验证码是{1},请于{2}分钟内正确输入。 392 | * 如果使用系统默认验证码,请使用verifyCode标签 393 | */ 394 | // public function testYunTongXunAgent() 395 | // { 396 | // var_dump('【云通讯】'); 397 | // 398 | // $templateVar=['1'=>'verifyCode','2'=>15]; 399 | // 400 | // $manager = $this->manager(); 401 | // $smsDriver = $manager->driver('yunTongXun'); 402 | // $smsDriver->setTemplateId(1); 403 | // $smsDriver->setTemplateVar($templateVar); 404 | // $result = $smsDriver->singlesSend($this->mobile); 405 | // 406 | // var_dump($result); 407 | // $this->assertTrue(true); 408 | // } 409 | 410 | /** 411 | * 【腾讯云短信 cloud.tencent.com】 412 | * 官方模板格式:验证码{1},请于{2}分钟内填写。如非本人操作,请忽略。 413 | * 如果使用系统内验证码,请使用verifyCode标签 414 | * 默认国家码为86,其它国家和地区的编码必须填写 415 | * 国内短信格式:$mobile = '13******' 或 $mobile = ['86', '13*********'] 416 | * 其它国家和地区短信格式:$mobile = ['82', '016********'] 417 | */ 418 | // public function testQQYunAgent() 419 | // { 420 | // var_dump('【QQYun】'); 421 | // 422 | // $templateVar = ['1' => 'verifyCode', '2' => 15]; 423 | // 424 | // $manager = $this->manager(); 425 | // $smsDriver = $manager->driver('qqYun'); 426 | // $smsDriver->setSignName('雷神的号'); 427 | // $smsDriver->setTemplateVar($templateVar); 428 | // $result = $smsDriver->singlesSend($this->mobile); 429 | // 430 | // var_dump($result); 431 | // $this->assertTrue(true); 432 | // } 433 | } 434 | -------------------------------------------------------------------------------- /tests/laravel5.5/PhpSmsUnitTest.php: -------------------------------------------------------------------------------- 1 | app = $this->createApplication(); 16 | } 17 | 18 | public function manager() 19 | { 20 | return new Manager($this->app); 21 | } 22 | 23 | public function testDriver() 24 | { 25 | $manager = $this->manager(); 26 | $smsDriver = $manager->driver('yunTongXun'); 27 | $isObject = is_object($smsDriver); 28 | 29 | $this->assertTrue($isObject); 30 | } 31 | 32 | public function testGetDefaultSignName() 33 | { 34 | $signName = '辣妈羊毛党'; 35 | 36 | $manager = $this->manager(); 37 | $smsDriver = $manager->driver('subMail'); 38 | $smsDriver->setSignName(); 39 | 40 | $this->assertEquals($signName, $smsDriver->getSignName()); 41 | } 42 | 43 | public function testSetSignName() 44 | { 45 | $signName = '钢铁战士'; 46 | 47 | $manager = $this->manager(); 48 | $smsDriver = $manager->driver('subMail'); 49 | $smsDriver->setSignName($signName); 50 | 51 | $this->assertEquals($signName, $smsDriver->getSignName()); 52 | } 53 | 54 | public function testSetTemplateId() 55 | { 56 | $id = 2; 57 | 58 | $manager = $this->manager(); 59 | $smsDriver = $manager->driver('yunTongXun'); 60 | $smsDriver->setTemplateId($id); 61 | 62 | $this->assertEquals($id, $smsDriver->getTemplateId()); 63 | } 64 | 65 | public function testFactories() 66 | { 67 | $factory = new Factory($this->app); 68 | $factories = $factory->getFactories(); 69 | 70 | $this->assertArrayHasKey('aLiYun', $factories); 71 | } 72 | 73 | public function testSetTemplateVarByDefault() 74 | { 75 | $templateVar = ['1' => 'verifyCode', '2' => 10]; 76 | 77 | $manager = $this->manager(); 78 | $smsDriver = $manager->driver('yunTongXun'); 79 | $smsDriver->setTemplateVar($templateVar); 80 | 81 | $this->assertContains('10', $smsDriver->getTemplateVar()); 82 | } 83 | 84 | public function testSetTemplateVarByHasKey() 85 | { 86 | $templateVar = ['verifyCode' => 'verifyCode', 'time' => 15]; 87 | 88 | $manager = $this->manager(); 89 | $smsDriver = $manager->driver('subMail'); 90 | $smsDriver->setTemplateVar($templateVar, true); 91 | 92 | $this->assertArrayHasKey('verifyCode', $smsDriver->getTemplateVar()); 93 | } 94 | 95 | public function testSetTemplateVarByCustomVar() 96 | { 97 | $templateVar = ['1' => '123456', '2' => 21]; 98 | $get_templateVar = ['0' => '123456', '1' => 21]; 99 | 100 | $manager = $this->manager(); 101 | $smsDriver = $manager->driver('aLiYun'); 102 | $smsDriver->setTemplateVar($templateVar); 103 | 104 | $this->assertEquals($get_templateVar, $smsDriver->getTemplateVar()); 105 | } 106 | 107 | public function testSetContentByVerifyCode() 108 | { 109 | $manager = $this->manager(); 110 | $smsDriver = $manager->driver('luoSiMao'); 111 | $smsDriver->setContentByVerifyCode(); 112 | 113 | $this->assertTrue(true); 114 | } 115 | 116 | public function testSetContentByVerifyCodeAndTime() 117 | { 118 | $manager = $this->manager(); 119 | $smsDriver = $manager->driver('yunPian'); 120 | $smsDriver->setContentByVerifyCode(20); 121 | 122 | $this->assertContains('20',$smsDriver->getContent()); 123 | } 124 | 125 | public function testSetContentByCustomVar() 126 | { 127 | $templateVar = ['verifyCode' => random_int(100000, 999999), 'time' => 360]; 128 | 129 | $manager = $this->manager(); 130 | $smsDriver = $manager->driver('yunPian'); 131 | $smsDriver->setContentByCustomVar($templateVar); 132 | 133 | $this->assertContains('360', $smsDriver->getContent()); 134 | } 135 | 136 | public function testSetContent() 137 | { 138 | $content = '您的帐号异地登录,如要不是你本人操作,请及时修改密码'; //设置短信内容 139 | 140 | $manager = $this->manager(); 141 | $smsDriver = $manager->driver('yunPian'); 142 | $smsDriver->setContent($content); 143 | 144 | $this->assertEquals($content, $smsDriver->getContent()); 145 | } 146 | 147 | public function testSetContentAndSetContentByCustomVar() 148 | { 149 | $content = '{name}, 您的帐号异地登录,如要不是你本人操作,请及时修改密码'; //设置短信内容 150 | $templateVar = ['name' => 'discovery']; 151 | $get_content = 'discovery, 您的帐号异地登录,如要不是你本人操作,请及时修改密码'; 152 | 153 | $manager = $this->manager(); 154 | $smsDriver = $manager->driver('luoSiMao'); 155 | $smsDriver->setContent($content); 156 | $smsDriver->setContentByCustomVar($templateVar); 157 | 158 | $this->assertEquals($get_content, $smsDriver->getContent()); 159 | } 160 | 161 | /******************************* 以下请根据实际需要打开测试 ******************************************/ 162 | 163 | /******************************* 返回拼接后的待发送数据 分割线 ***************************************/ 164 | 165 | /** 166 | * 【ALiYun】 167 | * 官方模板格式:${verifyCode}是您请求的验证码 168 | * 如果使用系统内验证码,请使用verifyCode标签 169 | */ 170 | public function testGetALiYunData() 171 | { 172 | var_dump('【ALiYun】'); 173 | 174 | $templateVar = ['verifyCode' => 'verifyCode']; 175 | 176 | $manager = $this->manager(); 177 | $smsDriver = $manager->driver('aLiYun'); 178 | $smsDriver->setSignName('雷神'); 179 | $smsDriver->setTemplateVar($templateVar, true); 180 | $result = $smsDriver->singlesSend($this->mobile, false); 181 | 182 | var_dump($result); 183 | $this->assertTrue(true); 184 | } 185 | 186 | /** 187 | * 【luoSiMao】返回拼接后的待发送数据 188 | * 不支持营销短信、全变量短信模板 189 | * 官方模板格式:###是您请求的验证码【{signName}】,代码中签名在后面,发送到手机上自动变为在前面。 190 | * 程序中替换变量名:{verifyCode}是您请求的验证码 191 | * 请用标准变量名代替###,以方便程序拼接数据,变量顺序和官方模板###位置顺序保持一致 192 | */ 193 | public function testGetLuoSiMaoData() 194 | { 195 | var_dump('【luoSiMao】'); 196 | 197 | $manager = $this->manager(); 198 | $smsDriver = $manager->driver('luoSiMao'); 199 | $smsDriver->setContentByVerifyCode(); 200 | $result = $smsDriver->singlesSend($this->mobile, false); 201 | 202 | var_dump($result); 203 | $this->assertTrue(true); 204 | } 205 | 206 | /** 207 | * 【赛邮云通信xsend发送方式】返回拼接后的待发送数据 208 | * 官方模板格式:您的验证码是@var(verifyCode),有效期为@var(minute)分钟,请尽快验证 209 | * 如果使用系统默认验证码,请使用verifyCode标签 210 | */ 211 | public function testGetSubMailData() 212 | { 213 | var_dump('【赛邮云通信】'); 214 | 215 | $templateVar = ['verifyCode' => random_int(100000, 999999), 'minute' => 60]; 216 | 217 | $manager = $this->manager(); 218 | $smsDriver = $manager->driver('subMail'); 219 | $smsDriver->setSignName('辣妈测试'); 220 | $smsDriver->setTemplateVar($templateVar, true); 221 | $result = $smsDriver->singlesSend($this->mobile, false); 222 | 223 | var_dump($result); 224 | $this->assertTrue(true); 225 | } 226 | 227 | /** 228 | * 【云片网】返回拼接后的待发送数据 229 | * 官方模板格式:您的验证码是#code#,有效期为#time#分钟,请尽快验证 230 | * 程序中替换变量名:您的验证码是{verifyCode},有效期为{time}分钟,请尽快验证 231 | */ 232 | public function testGetYunPianData() 233 | { 234 | var_dump('【云片网】'); 235 | 236 | $manager = $this->manager(); 237 | $smsDriver = $manager->driver('yunPian'); 238 | $smsDriver->setContentByVerifyCode(20); 239 | $result = $smsDriver->singlesSend($this->mobile, false); 240 | 241 | var_dump($result); 242 | $this->assertTrue(true); 243 | } 244 | 245 | /** 246 | * 【云通讯】返回拼接后的待发送数据 247 | * 官方测试模板ID:1 248 | * 官方测试模板:【云通讯】您使用的是云通讯短信模板,您的验证码是{1},请于{2}分钟内正确输入。 249 | * 如果使用系统默认验证码,请使用verifyCode标签 250 | */ 251 | public function testGetYunTongXunData() 252 | { 253 | var_dump('【云通讯】'); 254 | 255 | $templateVar = ['1' => 'verifyCode', '2' => 15]; 256 | 257 | $manager = $this->manager(); 258 | $smsDriver = $manager->driver('yunTongXun'); 259 | $smsDriver->setTemplateId(1); 260 | $smsDriver->setTemplateVar($templateVar); 261 | $result = $smsDriver->singlesSend($this->mobile, false); 262 | 263 | var_dump($result); 264 | $this->assertTrue(true); 265 | } 266 | 267 | /** 268 | * 【腾讯云短信 cloud.tencent.com】 269 | * 官方模板格式:验证码{1},请于{2}分钟内填写。如非本人操作,请忽略。 270 | * 如果使用系统内验证码,请使用verifyCode标签 271 | * 默认国家码为86,其它国家和地区的编码必须填写 272 | * 国内短信格式:$mobile = '13******' 或 $mobile = ['86', '13*********'] 273 | * 其它国家和地区短信格式:$mobile = ['82', '016********'] 274 | */ 275 | public function testGetQQYunData() 276 | { 277 | var_dump('【QQYun】'); 278 | 279 | $templateVar = ['1' => 'verifyCode', '2' => 15]; 280 | 281 | $manager = $this->manager(); 282 | $smsDriver = $manager->driver('qqYun'); 283 | $smsDriver->setSignName('雷神的号'); 284 | $smsDriver->setTemplateVar($templateVar); 285 | $result = $smsDriver->singlesSend($this->mobile, false); 286 | 287 | var_dump($result); 288 | $this->assertTrue(true); 289 | } 290 | 291 | /******************************* 实际发送 分割线 **********************************************/ 292 | 293 | /** 294 | * 【ALiYun】 295 | * 官方模板格式:${verifyCode}是您请求的验证码 296 | * 如果使用系统内验证码,请使用verifyCode标签 297 | */ 298 | // public function testALiYunAgent() 299 | // { 300 | // var_dump('【ALiYun】'); 301 | // 302 | // $templateVar = ['verifyCode' => 'verifyCode']; 303 | // 304 | // $manager = $this->manager(); 305 | // $smsDriver = $manager->driver('aLiYun'); 306 | // $smsDriver->setSignName('雷神'); 307 | // $smsDriver->setTemplateVar($templateVar, true); 308 | // $result = $smsDriver->singlesSend($this->mobile); 309 | // 310 | // var_dump($result); 311 | // $this->assertTrue(true); 312 | // } 313 | 314 | /** 315 | * 【luoSiMao】 316 | * 不支持营销短信、全变量短信模板 317 | * 官方模板格式:###是您请求的验证码【{signName}】,代码中签名在后面,发送到手机上自动变为在前面。 318 | * 程序中替换变量名:{verifyCode}是您请求的验证码 319 | * 请用标准变量名代替###,以方便程序拼接数据,变量顺序和官方模板###位置顺序保持一致 320 | */ 321 | // public function testLuoSiMaoAgent() 322 | // { 323 | // var_dump('【luoSiMao】'); 324 | // 325 | // $manager = $this->manager(); 326 | // $smsDriver = $manager->driver('luoSiMao'); 327 | // $smsDriver->setContentByVerifyCode(); 328 | // $result = $smsDriver->singlesSend($this->mobile); 329 | // 330 | // var_dump($result); 331 | // $this->assertTrue(true); 332 | // } 333 | 334 | // public function testLuoSiMaoAgentByCustomContent() 335 | // { 336 | // var_dump('【luoSiMao Custom Content】'); 337 | // 338 | // $content = '{name},您的帐号异地登录,如要不是你本人操作,请及时修改密码'; //设置短信内容 339 | // $templateVar = ['name' => 'discovery']; 340 | // 341 | // $manager = $this->manager(); 342 | // $smsDriver = $manager->driver('luoSiMao'); 343 | // $smsDriver->setContent($content); 344 | // $smsDriver->setContentByCustomVar($templateVar); 345 | // $result = $smsDriver->singlesSend($this->mobile); 346 | // 347 | // var_dump($result); 348 | // $this->assertTrue(true); 349 | // } 350 | 351 | /** 352 | * 【赛邮云通信xsend发送方式】 353 | * 官方模板格式:您的验证码是@var(verifyCode),有效期为@var(minute)分钟,请尽快验证 354 | * 如果使用系统默认验证码,请使用verifyCode标签 355 | */ 356 | // public function testSubMailAgent() 357 | // { 358 | // var_dump('【赛邮云通信】'); 359 | // 360 | // $templateVar = ['verifyCode' => random_int(100000, 999999), 'minute' => 60]; 361 | // 362 | // $manager = $this->manager(); 363 | // $smsDriver = $manager->driver('subMail'); 364 | // $smsDriver->setSignName('辣妈测试'); 365 | // $smsDriver->setTemplateVar($templateVar, true); 366 | // $result = $smsDriver->singlesSend($this->mobile); 367 | // 368 | // var_dump($templateVar['verifyCode']); 369 | // var_dump($result); 370 | // $this->assertTrue(true); 371 | // } 372 | 373 | /** 374 | * 【云片网】 375 | * 官方模板格式:您的验证码是#code#,有效期为#time#分钟,请尽快验证 376 | * 程序中替换变量名:您的验证码是{verifyCode},有效期为{time}分钟,请尽快验证 377 | */ 378 | // public function testYunPianAgent() 379 | // { 380 | // var_dump('【云片网】'); 381 | // 382 | // $manager = $this->manager(); 383 | // $smsDriver = $manager->driver('yunPian'); 384 | // $smsDriver->setContentByVerifyCode(20); 385 | // $result = $smsDriver->singlesSend($this->mobile); 386 | // 387 | // var_dump($result); 388 | // $this->assertTrue(true); 389 | // } 390 | 391 | /** 392 | * 【云通讯】 393 | * 官方测试模板ID:1 394 | * 官方测试模板:【云通讯】您使用的是云通讯短信模板,您的验证码是{1},请于{2}分钟内正确输入。 395 | * 如果使用系统默认验证码,请使用verifyCode标签 396 | */ 397 | // public function testYunTongXunAgent() 398 | // { 399 | // var_dump('【云通讯】'); 400 | // 401 | // $templateVar=['1'=>'verifyCode','2'=>15]; 402 | // 403 | // $manager = $this->manager(); 404 | // $smsDriver = $manager->driver('yunTongXun'); 405 | // $smsDriver->setTemplateId(1); 406 | // $smsDriver->setTemplateVar($templateVar); 407 | // $result = $smsDriver->singlesSend($this->mobile); 408 | // 409 | // var_dump($result); 410 | // $this->assertTrue(true); 411 | // } 412 | 413 | /** 414 | * 【腾讯云短信 cloud.tencent.com】 415 | * 官方模板格式:验证码{1},请于{2}分钟内填写。如非本人操作,请忽略。 416 | * 如果使用系统内验证码,请使用verifyCode标签 417 | * 默认国家码为86,其它国家和地区的编码必须填写 418 | * 国内短信格式:$mobile = '13******' 或 $mobile = ['86', '13*********'] 419 | * 其它国家和地区短信格式:$mobile = ['82', '016********'] 420 | */ 421 | // public function testQQYunAgent() 422 | // { 423 | // var_dump('【QQYun】'); 424 | // 425 | // $templateVar = ['1' => 'verifyCode', '2' => 15]; 426 | // 427 | // $manager = $this->manager(); 428 | // $smsDriver = $manager->driver('qqYun'); 429 | // $smsDriver->setSignName('雷神的号'); 430 | // $smsDriver->setTemplateVar($templateVar); 431 | // $result = $smsDriver->singlesSend($this->mobile); 432 | // 433 | // var_dump($result); 434 | // $this->assertTrue(true); 435 | // } 436 | } 437 | --------------------------------------------------------------------------------