README.md 11.1 KB
Newer Older
O
overtrue 已提交
1
<h1 align="center">Easy SMS</h1>
O
overtrue 已提交
2

O
overtrue 已提交
3
<p align="center">:calling: 一款满足你的多种发送需求的短信发送组件</p>
O
overtrue 已提交
4 5 6 7 8 9 10 11 12 13

<p align="center">
<a href="https://travis-ci.org/overtrue/easy-sms"><img src="https://travis-ci.org/overtrue/easy-sms.svg?branch=master" alt="Build Status"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/v/unstable.svg" alt="Latest Unstable Version"></a>
<a href="https://scrutinizer-ci.com/g/overtrue/easy-sms/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/easy-sms/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a>
<a href="https://scrutinizer-ci.com/g/overtrue/easy-sms/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/easy-sms/badges/coverage.png?b=master" alt="Code Coverage"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/downloads" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/license" alt="License"></a>
</p>
O
overtrue 已提交
14

P
Paul King 已提交
15

O
overtrue 已提交
16 17 18 19 20 21 22 23 24
## 特点

1. 支持目前市面多家服务商
1. 一套写法兼容所有平台
1. 简单配置即可灵活增减服务商
1. 内置多种服务商轮询策略、支持自定义轮询策略
1. 统一的返回值格式,便于日志与监控
1. 自动轮询选择可用的服务商
1. 更多等你去发现与改进...
O
overtrue 已提交
25

O
overtrue 已提交
26 27
## 平台支持

28
- [阿里云](https://www.aliyun.com/)
O
overtrue 已提交
29 30 31 32 33 34 35 36
- [云片](https://www.yunpian.com)
- [Submail](https://www.mysubmail.com)
- [螺丝帽](https://luosimao.com/)
- [阿里大于](https://www.alidayu.com/)
- [容联云通讯](http://www.yuntongxun.com)
- [互亿无线](http://www.ihuyi.com)
- [聚合数据](https://www.juhe.cn)
- [SendCloud](http://www.sendcloud.net/)
I
iwzh 已提交
37
- [百度云](https://cloud.baidu.com/)
38
- [华信短信平台](http://www.ipyy.com/)
39
- [253云通讯(创蓝)](https://www.253.com/)
40
- [融云](http://www.rongcloud.cn)
高大成 已提交
41
- [天毅无线](http://www.85hu.com/)
J
Jiajian Chan 已提交
42
- [腾讯云 SMS](https://cloud.tencent.com/product/sms)
O
overtrue 已提交
43 44


O
overtrue 已提交
45
## 环境需求
O
overtrue 已提交
46

O
overtrue 已提交
47
- PHP >= 5.6
O
overtrue 已提交
48

O
overtrue 已提交
49
## 安装
O
overtrue 已提交
50 51 52 53 54

```shell
$ composer require "overtrue/easy-sms"
```

O
overtrue 已提交
55
## 使用
O
overtrue 已提交
56 57 58 59

```php
use Overtrue\EasySms\EasySms;

O
overtrue 已提交
60
$config = [
O
overtrue 已提交
61
    // HTTP 请求的超时时间(秒)
O
overtrue 已提交
62
    'timeout' => 5.0,
63

O
overtrue 已提交
64
    // 默认发送配置
O
overtrue 已提交
65
    'default' => [
O
overtrue 已提交
66
        // 网关调用策略,默认:顺序调用
O
overtrue 已提交
67
        'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
68

O
overtrue 已提交
69
        // 默认可用的发送网关
O
overtrue 已提交
70
        'gateways' => [
71
            'yunpian', 'aliyun', 'alidayu',
O
overtrue 已提交
72 73
        ],
    ],
O
overtrue 已提交
74
    // 可用的网关配置
O
overtrue 已提交
75
    'gateways' => [
O
overtrue 已提交
76
        'errorlog' => [
O
overtrue 已提交
77 78
            'file' => '/tmp/easy-sms.log',
        ],
O
overtrue 已提交
79
        'yunpian' => [
O
overtrue 已提交
80 81
            'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
        ],
82 83 84 85 86
        'aliyun' => [
            'access_key_id' => '',
            'access_key_secret' => '',
            'sign_name' => '',
        ],
O
overtrue 已提交
87
        'alidayu' => [
O
overtrue 已提交
88
            //...
O
overtrue 已提交
89 90 91
        ],
    ],
];
O
overtrue 已提交
92

O
overtrue 已提交
93
$easySms = new EasySms($config);
O
overtrue 已提交
94

95
$easySms->send(13188888888, [
96 97 98
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
99 100 101
        'code' => 6379
    ],
]);
O
overtrue 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
```

## 短信内容

由于使用多网关发送,所以一条短信要支持多平台发送,每家的发送方式不一样,但是我们抽象定义了以下公用属性:

- `content` 文字内容,使用在像云片类似的以文字内容发送的平台
- `template` 模板 ID,使用在以模板ID来发送短信的平台
- `data`  模板变量,使用在以模板ID来发送短信的平台

所以,在使用过程中你可以根据所要使用的平台定义发送的内容。

## 发送网关

默认使用 `default` 中的设置来发送,如果某一条短信你想要覆盖默认的设置。在 `send` 方法中使用第三个参数即可:

```php
H
HanSon 已提交
119
$easySms->send(13188888888, [
120 121 122
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
O
overtrue 已提交
123 124 125 126 127 128 129 130 131 132 133
        'code' => 6379
    ],
 ], ['yunpian', 'juhe']); // 这里的网关配置将会覆盖全局默认值
```

## 返回值

由于使用多网关发送,所以返回值为一个数组,结构如下:
```php
[
    'yunpian' => [
O
overtrue 已提交
134
        'gateway' => 'yunpian',
O
overtrue 已提交
135 136 137 138
        'status' => 'success',
        'result' => [...] // 平台返回值
    ],
    'juhe' => [
O
overtrue 已提交
139
        'gateway' => 'juhe',
O
overtrue 已提交
140
        'status' => 'failure',
O
overtrue 已提交
141 142 143 144
        'exception' => \Overtrue\EasySms\Exceptions\GatewayErrorException 对象
    ],
    //...
]
O
overtrue 已提交
145 146
```

O
overtrue 已提交
147 148
如果所选网关列表均发送失败时,将会抛出 `Overtrue\EasySms\Exceptions\NoGatewayAvailableException` 异常,你可以使用 `$e->results` 获取发送结果。

O
overtrue 已提交
149 150 151 152 153 154 155 156 157
你也可以使用 `$e` 提供的更多便捷方法:

```php
$e->getResults();               // 返回所有 API 的结果,结构同上
$e->getExceptions();            // 返回所有调用异常列表
$e->getException($gateway);     // 返回指定网关名称的异常对象
$e->getLastException();         // 获取最后一个失败的异常对象 
```

O
overtrue 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
## 自定义网关

本拓展已经支持用户自定义网关,你可以很方便的配置即可当成与其它拓展一样的使用:

```php
$config = [
    ...
    'default' => [
        'gateways' => [
            'mygateway', // 配置你的网站到可用的网关列表
        ],
    ],
    'gateways' => [
        'mygateway' => [...], // 你网关所需要的参数,如果没有可以不配置
    ],
];

$easySms = new EasySms($config);

// 注册
$easySms->extend('mygateway', function($gatewayConfig){
    // $gatewayConfig 来自配置文件里的 `gateways.mygateway`
    return new MyGateway($gatewayConfig);
});

183
$easySms->send(13188888888, [
184 185 186
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
187 188 189
        'code' => 6379
    ],
]);
O
overtrue 已提交
190 191
```

O
overtrue 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
## 国际短信

国际短信与国内短信的区别是号码前面需要加国际码,但是由于各平台对国际号码的写法不一致,所以在发送国际短信的时候有一点区别:

```php
use Overtrue\EasySms\PhoneNumber;

// 发送到国际码为 31 的国际号码
$number = new PhoneNumber(13188888888, 31);

$easySms->send($number, [
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
        'code' => 6379
    ],
]);
```

O
overtrue 已提交
211
## 定义短信
安正超 已提交
212

桥边红药 已提交
213
你可以根据发送场景的不同,定义不同的短信类,从而实现一处定义多处调用,你可以继承 `Overtrue\EasySms\Message` 来定义短信模型:
O
overtrue 已提交
214 215 216 217 218

```php
<?php

use Overtrue\EasySms\Message;
O
overtrue 已提交
219
use Overtrue\EasySms\Contracts\GatewayInterface;
O
overtrue 已提交
220
use Overtrue\EasySms\Strategies\OrderStrategy;
O
overtrue 已提交
221

L
littlehz 已提交
222
class OrderPaidMessage extends Message
O
overtrue 已提交
223 224
{
    protected $order;
O
overtrue 已提交
225 226
    protected $strategy = OrderStrategy::class;           // 定义本短信的网关使用策略,覆盖全局配置中的 `default.strategy`
    protected $gateways = ['alidayu', 'yunpian', 'juhe']; // 定义本短信的适用平台,覆盖全局配置中的 `default.gateways`
O
overtrue 已提交
227 228 229 230 231

    public function __construct($order)
    {
        $this->order = $order;
    }
232

O
overtrue 已提交
233
    // 定义直接使用内容发送平台的内容
O
overtrue 已提交
234
    public function getContent(GatewayInterface $gateway = null)
O
overtrue 已提交
235 236 237
    {
        return sprintf('您的订单:%s, 已经完成付款', $this->order->no);    
    }
238

O
overtrue 已提交
239
    // 定义使用模板发送方式平台所需要的模板 ID
O
overtrue 已提交
240
    public function getTemplate(GatewayInterface $gateway = null)
O
overtrue 已提交
241
    {
242
        return 'SMS_003';
O
overtrue 已提交
243
    }
244

O
overtrue 已提交
245
    // 模板参数
O
overtrue 已提交
246
    public function getData(GatewayInterface $gateway = null)
O
overtrue 已提交
247 248 249 250 251 252 253
    {
        return [
            'order_no' => $this->order->no    
        ];    
    }
}
```
安正超 已提交
254

O
overtrue 已提交
255 256 257 258 259 260 261 262 263 264 265
> 更多自定义方式请参考:[`Overtrue\EasySms\Message`](Overtrue\EasySms\Message;)

发送自定义短信:

```php
$order = ...;
$message = new OrderPaidMessage($order);

$easySms->send(13188888888, $message);
```

O
overtrue 已提交
266 267
## 各平台配置说明

268
### [阿里云](https://www.aliyun.com/)
O
overtrue 已提交
269

O
overtrue 已提交
270 271
短信内容使用 `template` + `data`

272 273 274 275 276 277 278
```php
    'aliyun' => [
        'access_key_id' => '',
        'access_key_secret' => '',
        'sign_name' => '',
    ],
```
O
overtrue 已提交
279 280 281

### [阿里大于](https://www.alidayu.com/)

O
overtrue 已提交
282 283
短信内容使用 `template` + `data`

O
overtrue 已提交
284 285 286
```php
    'alidayu' => [
        'app_key' => '',
287
        'app_secret' => '',
O
overtrue 已提交
288 289 290 291 292 293
        'sign_name' => '',
    ],
```

### [云片](https://www.yunpian.com)

O
overtrue 已提交
294 295
短信内容使用 `content`

O
overtrue 已提交
296 297 298 299 300 301 302 303
```php
    'yunpian' => [
        'api_key' => '',
    ],
```

### [Submail](https://www.mysubmail.com)

O
overtrue 已提交
304 305
短信内容使用 `data`

O
overtrue 已提交
306 307 308 309 310 311 312 313 314
```php
    'submail' => [
        'app_id' => '',
        'app_key' => '',
        'project' => '',
    ],
```

### [螺丝帽](https://luosimao.com/)
O
overtrue 已提交
315

O
overtrue 已提交
316 317
短信内容使用 `content`

O
overtrue 已提交
318 319 320 321 322 323 324 325
```php
    'luosimao' => [
        'api_key' => '',
    ],
```

### [容联云通讯](http://www.yuntongxun.com)

O
overtrue 已提交
326 327
短信内容使用 `template` + `data`

O
overtrue 已提交
328 329 330 331 332 333 334 335 336 337 338
```php
    'yuntongxun' => [
        'app_id' => '',
        'account_sid' => '',
        'account_token' => '',
        'is_sub_account' => false,
    ],
```

### [互亿无线](http://www.ihuyi.com)

O
overtrue 已提交
339 340
短信内容使用 `content`

O
overtrue 已提交
341 342 343
```php
    'huyi' => [
        'api_id' => '',
344
        'api_key' => '',
O
overtrue 已提交
345 346 347 348 349
    ],
```

### [聚合数据](https://www.juhe.cn)

O
overtrue 已提交
350 351
短信内容使用 `template` + `data`

O
overtrue 已提交
352 353 354 355 356 357 358 359
```php
    'juhe' => [
        'app_key' => '',
    ],
```

### [SendCloud](http://www.sendcloud.net/)

O
overtrue 已提交
360 361
短信内容使用 `template` + `data`

O
overtrue 已提交
362 363 364 365
```php
    'sendcloud' => [
        'sms_user' => '',
        'sms_key' => '',
O
overtrue 已提交
366
        'timestamp' => false, // 是否启用时间戳
O
overtrue 已提交
367 368
    ],
```
I
iwzh 已提交
369 370
### [百度云](https://cloud.baidu.com/)

O
overtrue 已提交
371 372
短信内容使用 `template` + `data`

I
iwzh 已提交
373 374 375 376 377
```php
    'baidu' => [
        'ak' => '',
        'sk' => '',
        'invoke_id' => '',
378
        'domain' => '',
I
iwzh 已提交
379 380
    ],
```
安正超 已提交
381

382 383
### [华信短信平台](http://www.ipyy.com/)

O
overtrue 已提交
384 385
短信内容使用 `content`

386 387 388 389 390 391 392 393 394 395
```php
    'huaxin' => [
        'user_id'  => '',
        'password' => '',
        'account'  => '',
        'ip'       => '',
        'ext_no'   => '',
    ],
```

396 397 398 399 400 401 402 403 404 405 406
### [253云通讯(创蓝)](https://www.253.com/)

短信内容使用 `content`

```php
    'chuanglan' => [
        'username'  => '',
        'password' => '',
    ],
```

407 408 409 410 411 412 413 414 415 416 417
### [融云](http://www.rongcloud.cn)

短信分为两大类,验证类和通知类短信。 发送验证类短信使用 `template` + `data`

```php
    'rongcloud' => [
        'app_key' => '',
        'app_secret' => '',
    ]
```

高大成 已提交
418 419 420 421 422 423 424 425 426 427 428 429
### [天毅无线](http://www.85hu.com/)

短信内容使用 `content`

```php
    'tianyiwuxian' => [
        'username' => '', //用户名
        'password' => '', //密码
        'gwid' => '', //网关ID
    ]
```

T
tradzero 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442
### [twilio](https://www.twilio.com)

短信使用 `content`  
发送对象需要 使用`+`添加区号

```php
    'twilio' => [
        'account_sid' => '', // sid
        'from' => '', // 发送的号码 可以在控制台购买
        'token' => '', // apitoken
    ],
```

J
Jiajian Chan 已提交
443 444 445 446 447 448 449 450 451 452 453
### [腾讯云 SMS](https://cloud.tencent.com/product/sms)

短信内容使用 `content`

```php
    'qcloud' => [
        'sdk_app_id' => '', // SDK APP ID
        'app_key' => '', // APP KEY
    ],
```

O
overtrue 已提交
454
## License
O
overtrue 已提交
455 456

MIT