README.md 6.7 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

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

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

O
overtrue 已提交
25 26 27 28 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/)


O
overtrue 已提交
37
## 环境需求
O
overtrue 已提交
38

O
overtrue 已提交
39
- PHP >= 5.6
O
overtrue 已提交
40

O
overtrue 已提交
41
## 安装
O
overtrue 已提交
42 43 44 45 46

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

O
overtrue 已提交
47
## 使用
O
overtrue 已提交
48 49 50 51

```php
use Overtrue\EasySms\EasySms;

O
overtrue 已提交
52
$config = [
O
overtrue 已提交
53
    // HTTP 请求的超时时间(秒)
O
overtrue 已提交
54
    'timeout' => 5.0,
O
overtrue 已提交
55 56
    
    // 默认发送配置
O
overtrue 已提交
57
    'default' => [
O
overtrue 已提交
58
        // 网关调用策略,默认:顺序调用
O
overtrue 已提交
59 60 61
        'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class
        
        // 默认可用的发送网关
O
overtrue 已提交
62 63 64 65
        'gateways' => [
            'yunpian', 'alidayu',
        ],
    ],
O
overtrue 已提交
66
    // 可用的网关配置
O
overtrue 已提交
67
    'gateways' => [
O
overtrue 已提交
68
        'errorlog' => [
O
overtrue 已提交
69 70
            'file' => '/tmp/easy-sms.log',
        ],
O
overtrue 已提交
71
        'yunpian' => [
O
overtrue 已提交
72 73
            'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
        ],
O
overtrue 已提交
74
        'alidayu' => [
O
overtrue 已提交
75
            //...
O
overtrue 已提交
76 77 78
        ],
    ],
];
O
overtrue 已提交
79

O
overtrue 已提交
80
$easySms = new EasySms($config);
O
overtrue 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

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

## 短信内容

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

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

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

## 发送网关

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

```php
H
HanSon 已提交
106
$easySms->send(13188888888, [
O
overtrue 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    'content'  => '您的验证码为: 6379', 
    'template' => 'SMS_001', 
    'data' => [ 
        'code' => 6379
    ],
 ], ['yunpian', 'juhe']); // 这里的网关配置将会覆盖全局默认值
```

## 返回值

由于使用多网关发送,所以返回值为一个数组,结构如下:
```php
[
    'yunpian' => [
        'status' => 'success',
        'result' => [...] // 平台返回值
    ],
    'juhe' => [
        'status' => 'erred',
        'exception' => \Overtrue\EasySms\Exceptions\GatewayErrorException 对象
    ],
    //...
]
O
overtrue 已提交
130 131
```

O
overtrue 已提交
132
## 定义短信
安正超 已提交
133

O
overtrue 已提交
134 135 136 137 138 139
你可以根本发送场景的不同,定义不同的短信类,从而实现一处定义多处调用,你可以继承 `Overtrue\EasySms\Message` 来定义短信模型:

```php
<?php

use Overtrue\EasySms\Message;
O
overtrue 已提交
140
use Overtrue\EasySms\Contracts\GatewayInterface;
O
overtrue 已提交
141
use Overtrue\EasySms\Strategies\OrderStrategy;
O
overtrue 已提交
142 143 144 145

class OrderPaidMessage extends Messeage
{
    protected $order;
O
overtrue 已提交
146 147
    protected $strategy = OrderStrategy::class;           // 定义本短信的网关使用策略,覆盖全局配置中的 `default.strategy`
    protected $gateways = ['alidayu', 'yunpian', 'juhe']; // 定义本短信的适用平台,覆盖全局配置中的 `default.gateways`
O
overtrue 已提交
148 149 150 151 152 153 154

    public function __construct($order)
    {
        $this->order = $order;
    }
        
    // 定义直接使用内容发送平台的内容
O
overtrue 已提交
155
    public function getContent(GatewayInterface $gateway = null)
O
overtrue 已提交
156 157 158 159 160
    {
        return sprintf('您的订单:%s, 已经完成付款', $this->order->no);    
    }
    
    // 定义使用模板发送方式平台所需要的模板 ID
O
overtrue 已提交
161
    public function getTemplate(GatewayInterface $gateway = null)
O
overtrue 已提交
162 163 164 165 166
    {
        return 'SMS_003'; 
    }
        
    // 模板参数
O
overtrue 已提交
167
    public function getData(GatewayInterface $gateway = null)
O
overtrue 已提交
168 169 170 171 172 173 174
    {
        return [
            'order_no' => $this->order->no    
        ];    
    }
}
```
安正超 已提交
175

O
overtrue 已提交
176 177 178 179 180 181 182 183 184 185 186
> 更多自定义方式请参考:[`Overtrue\EasySms\Message`](Overtrue\EasySms\Message;)

发送自定义短信:

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

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

O
overtrue 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
## 各平台配置说明



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

```php
    'alidayu' => [
        'app_key' => '',
        'sign_name' => '',
    ],
```

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

```php
    'yunpian' => [
        'api_key' => '',
    ],
```

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

```php
    'submail' => [
        'app_id' => '',
        'app_key' => '',
        'project' => '',
    ],
```

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

O
overtrue 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
```php
    'luosimao' => [
        'api_key' => '',
    ],
```

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

```php
    'yuntongxun' => [
        'app_id' => '',
        'account_sid' => '',
        'account_token' => '',
        'is_sub_account' => false,
    ],
```

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

```php
    'huyi' => [
        'api_id' => '',
    ],
```

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

```php
    'juhe' => [
        'app_key' => '',
    ],
```

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

```php
    'sendcloud' => [
        'sms_user' => '',
        'sms_key' => '',
    ],
```
安正超 已提交
261

O
overtrue 已提交
262
## License
O
overtrue 已提交
263 264

MIT