Download the PHP package abcsun/laravel-sms without Composer
On this page you can find all versions of the php package abcsun/laravel-sms. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download abcsun/laravel-sms
More information about abcsun/laravel-sms
Files in abcsun/laravel-sms
Informations about the package laravel-sms
laravel-sms for laravel 5
使用场景
- 发送短信验证码。
- 发送信息通知短信(如:订单通知,发货通知,上课通知...)。
- 特殊情况下用户收不到短信? laravel-sms提倡通过备用代理器机制使用两个及两个以上服务商。
该包特性
- 数据库记录/管理短信数据及其发送情况。
- 兼容模板短信和内容短信。
- 支持短信队列。
- 备用代理器(服务商)机制。即:如果用一个服务商发送短信失败,将会自动尝试通过预先设置的备用服务商发送。
- 集成验证码短信发送/校验模块,分分钟搞定验证码短信发送以及手机号/验证码校验, 从此告别重复写验证码短信发送与校验的历史。
- 支持语音验证码,使用方法见特性5。
- 集成如下第三方短信服务商,你也可自定义代理器。
服务商 | 模板短信 | 内容短信 | 语音验证码 | 最低消费 | 最低消费单价 |
---|---|---|---|---|---|
Luosimao | no | yes | yes | ¥850(1万条) | ¥0.085/条 |
云片网络 | no | yes | yes | ¥55(1千条) | ¥0.055/条 |
容联·云通讯 | yes | no | yes | 充值¥500 | ¥0.055/条 |
SUBMAIL | yes | no | no | ¥100(1千条) | ¥0.100/条 |
云之讯 | yes | no | yes | ¥0.050/条 |
安装
在项目根目录下运行如下composer命令:
快速上手
1.注册服务提供器
在config/app.php文件中providers数组里加入:
在config/app.php文件中的aliases数组里加入
2.migration生成 & 参数配置
-
请先运行如下命令生成配置文件和migration文件
-
在数据库中生成sms表
-
设置默认代理器(服务商)
请在config/laravel-sms.php中设置默认代理服务商,默认为'Luosimao'。
-
配置代理服务商的相关参数
在config/laravel-sms.php中,找到你想要使用的代理器,并填写好配置信息。
如果你使用的是Luosimao,请在数组'Luosimao'中按照提示填写配置信息
更多的服务商配置就不详说了,请到配置文件中查看并按提示修改相应代理服务商的配置。
3.Enjoy it! 使用Sms模型发送短信
在控制器中发送触发短信,如:
4.常用的语法糖
-
发送给谁
- 设置模板ID
如果你只使用了默认代理器,即没有开启备用代理器机制。你只需要设置默认代理器的模板ID:
如果你要开启备用代理器机制,那么需要为只支持模板短信默认/备用代理器设置相应模板ID,这样才能保证这些代理器正常使用。可以这样设置:
-
设置模板短信的模板数据
-
设置内容短信的内容
有些服务商(如YunPian,Luosimao)只支持内容短信(即直接发送短信内容),不支持模板,那么就需要设置短信内容。
-
临时开启/关闭短信队列
- 发送短信
短信队列
在config/laravel-sms中修改配置
如果你开启了队列,需要运行如下命名监听队列
备用代理器机制
如果用一个服务商发送短信失败(如:欠费、频繁发送、发送次数上限、内容重复...),将会自动尝试通过备用服务商发送。 在config/laravel-sms.php中配置备用代理器
其中agents中如果有多个值,如:A,B,C。 那么当默认代理器发送失败时,会自动启用A代理器,若A代理器发送失败,则会自动启用B,依次类推直到最后一个备用代理器。
验证码短信模块
可以直接访问example.com/sms/info查看该模块是否可用,并可在该页面里观察验证码短信发送数据,方便你进行调试。
1.[浏览器端]请求发送带验证码短信
该包已经封装好浏览器端的jquery/zepto插件,只需要为发送按钮添加扩展方法即可实现发送短信。
注意: 如果你使用Luosimao语音验证码,请在配置文件中'Luosimao'中设置'voiceApikey'。
2.[服务器端]配置短信内容/模板
配置文件: config/laravel-sms.php
- 填写你的验证码短信内容或模板标示符
如果你使用的是内容短信(如云片网络,Luosimao),则使用或修改'verifySmsContent'的值:
如果你使用模板短信(如云通讯,SubMail),需要到相应代理器中填写模板标示符:
- 修改或自定义发送前检测规则
3.[服务器端]合法性验证
用户填写验证码并提交表单到服务器时,在你的控制器中需要验证手机号和验证码是否正确,你只需要加上如下代码即可:
PS:
mobile_changed
验证用户手机号是否合法。verify_code
验证验证码是否合法(验证码是否正确,是否超时无效)。-
verify_rule:{$mobileRule}
检测是否为非法请求,第一值为手机号检测规则,必须和你在浏览器端js插件中填写的mobileRule的值一致。请在语言包中做好翻译。
自助二次开发
1.自定义Model
继承model类(Toplan\Sms\Sms)
修改model类后需要在配置文件中,修改key为'smsModel'的值:
自定义代理器
欢迎贡献更多的代理器,这样就能支持更多第三方服务商的发送接口。请注意命名规范,Foo为代理器(服务商)名称。
配置项加入到src/config/laravel-sms.php中:
在agents目录下添加代理器类(注意类名为FooAgent),并继承Agent抽象类。如果使用到其他api,可以将api文件放入src/lib文件夹中。
至此, 新加代理器成功!
License
MIT