初始上传
This commit is contained in:
20
vendor/overtrue/easy-sms/.editorconfig
vendored
Executable file
20
vendor/overtrue/easy-sms/.editorconfig
vendored
Executable file
@@ -0,0 +1,20 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = false
|
||||
|
||||
[*.{vue,js,scss}]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
768
vendor/overtrue/easy-sms/README.md
vendored
Executable file
768
vendor/overtrue/easy-sms/README.md
vendored
Executable file
@@ -0,0 +1,768 @@
|
||||
<h1 align="center">Easy SMS</h1>
|
||||
|
||||
<p align="center">:calling: 一款满足你的多种发送需求的短信发送组件</p>
|
||||
|
||||
<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>
|
||||
|
||||
|
||||
## 特点
|
||||
|
||||
1. 支持目前市面多家服务商
|
||||
1. 一套写法兼容所有平台
|
||||
1. 简单配置即可灵活增减服务商
|
||||
1. 内置多种服务商轮询策略、支持自定义轮询策略
|
||||
1. 统一的返回值格式,便于日志与监控
|
||||
1. 自动轮询选择可用的服务商
|
||||
1. 更多等你去发现与改进...
|
||||
|
||||
## 平台支持
|
||||
|
||||
- [阿里云](https://www.aliyun.com/)
|
||||
- [云片](https://www.yunpian.com)
|
||||
- [Submail](https://www.mysubmail.com)
|
||||
- [螺丝帽](https://luosimao.com/)
|
||||
- [容联云通讯](http://www.yuntongxun.com)
|
||||
- [互亿无线](http://www.ihuyi.com)
|
||||
- [聚合数据](https://www.juhe.cn)
|
||||
- [SendCloud](http://www.sendcloud.net/)
|
||||
- [百度云](https://cloud.baidu.com/)
|
||||
- [华信短信平台](http://www.ipyy.com/)
|
||||
- [253云通讯(创蓝)](https://www.253.com/)
|
||||
- [融云](http://www.rongcloud.cn)
|
||||
- [天毅无线](http://www.85hu.com/)
|
||||
- [腾讯云 SMS](https://cloud.tencent.com/product/sms)
|
||||
- [阿凡达数据](http://www.avatardata.cn/)
|
||||
- [华为云](https://www.huaweicloud.com/product/msgsms.html)
|
||||
- [网易云信](https://yunxin.163.com/sms)
|
||||
- [云之讯](https://www.ucpaas.com/index.html)
|
||||
- [凯信通](http://www.kingtto.cn/)
|
||||
- [七牛云](https://www.qiniu.com/)
|
||||
- [UE35.net](http://uesms.ue35.cn/)
|
||||
- [Ucloud](https://www.ucloud.cn)
|
||||
- [短信宝](http://www.smsbao.com/)
|
||||
- [Tiniyo](https://tiniyo.com/)
|
||||
- [摩杜云](https://www.moduyun.com/)
|
||||
|
||||
## 环境需求
|
||||
|
||||
- PHP >= 5.6
|
||||
|
||||
## 安装
|
||||
|
||||
```shell
|
||||
$ composer require "overtrue/easy-sms"
|
||||
```
|
||||
|
||||
**For Laravel notification**
|
||||
|
||||
如果你喜欢使用 [Laravel Notification](https://laravel.com/docs/5.8/notifications), 可以考虑直接使用朋友封装的拓展包:
|
||||
|
||||
https://github.com/yl/easysms-notification-channel
|
||||
|
||||
## 使用
|
||||
|
||||
```php
|
||||
use Overtrue\EasySms\EasySms;
|
||||
|
||||
$config = [
|
||||
// HTTP 请求的超时时间(秒)
|
||||
'timeout' => 5.0,
|
||||
|
||||
// 默认发送配置
|
||||
'default' => [
|
||||
// 网关调用策略,默认:顺序调用
|
||||
'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
|
||||
|
||||
// 默认可用的发送网关
|
||||
'gateways' => [
|
||||
'yunpian', 'aliyun',
|
||||
],
|
||||
],
|
||||
// 可用的网关配置
|
||||
'gateways' => [
|
||||
'errorlog' => [
|
||||
'file' => '/tmp/easy-sms.log',
|
||||
],
|
||||
'yunpian' => [
|
||||
'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
|
||||
],
|
||||
'aliyun' => [
|
||||
'access_key_id' => '',
|
||||
'access_key_secret' => '',
|
||||
'sign_name' => '',
|
||||
],
|
||||
//...
|
||||
],
|
||||
];
|
||||
|
||||
$easySms = new EasySms($config);
|
||||
|
||||
$easySms->send(13188888888, [
|
||||
'content' => '您的验证码为: 6379',
|
||||
'template' => 'SMS_001',
|
||||
'data' => [
|
||||
'code' => 6379
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
## 短信内容
|
||||
|
||||
由于使用多网关发送,所以一条短信要支持多平台发送,每家的发送方式不一样,但是我们抽象定义了以下公用属性:
|
||||
|
||||
- `content` 文字内容,使用在像云片类似的以文字内容发送的平台
|
||||
- `template` 模板 ID,使用在以模板ID来发送短信的平台
|
||||
- `data` 模板变量,使用在以模板ID来发送短信的平台
|
||||
|
||||
所以,在使用过程中你可以根据所要使用的平台定义发送的内容。
|
||||
|
||||
```php
|
||||
$easySms->send(13188888888, [
|
||||
'content' => '您的验证码为: 6379',
|
||||
'template' => 'SMS_001',
|
||||
'data' => [
|
||||
'code' => 6379
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
你也可以使用闭包来返回对应的值:
|
||||
|
||||
```php
|
||||
$easySms->send(13188888888, [
|
||||
'content' => function($gateway){
|
||||
return '您的验证码为: 6379';
|
||||
},
|
||||
'template' => function($gateway){
|
||||
return 'SMS_001';
|
||||
},
|
||||
'data' => function($gateway){
|
||||
return [
|
||||
'code' => 6379
|
||||
];
|
||||
},
|
||||
]);
|
||||
```
|
||||
|
||||
你可以根据 `$gateway` 参数类型来判断返回值,例如:
|
||||
|
||||
```php
|
||||
$easySms->send(13188888888, [
|
||||
'content' => function($gateway){
|
||||
if ($gateway->getName() == 'yunpian') {
|
||||
return '云片专用验证码:1235';
|
||||
}
|
||||
return '您的验证码为: 6379';
|
||||
},
|
||||
'template' => function($gateway){
|
||||
if ($gateway->getName() == 'aliyun') {
|
||||
return 'TP2818';
|
||||
}
|
||||
return 'SMS_001';
|
||||
},
|
||||
'data' => function($gateway){
|
||||
return [
|
||||
'code' => 6379
|
||||
];
|
||||
},
|
||||
]);
|
||||
```
|
||||
|
||||
## 发送网关
|
||||
|
||||
默认使用 `default` 中的设置来发送,如果某一条短信你想要覆盖默认的设置。在 `send` 方法中使用第三个参数即可:
|
||||
|
||||
```php
|
||||
$easySms->send(13188888888, [
|
||||
'content' => '您的验证码为: 6379',
|
||||
'template' => 'SMS_001',
|
||||
'data' => [
|
||||
'code' => 6379
|
||||
],
|
||||
], ['yunpian', 'juhe']); // 这里的网关配置将会覆盖全局默认值
|
||||
```
|
||||
|
||||
## 返回值
|
||||
|
||||
由于使用多网关发送,所以返回值为一个数组,结构如下:
|
||||
```php
|
||||
[
|
||||
'yunpian' => [
|
||||
'gateway' => 'yunpian',
|
||||
'status' => 'success',
|
||||
'result' => [...] // 平台返回值
|
||||
],
|
||||
'juhe' => [
|
||||
'gateway' => 'juhe',
|
||||
'status' => 'failure',
|
||||
'exception' => \Overtrue\EasySms\Exceptions\GatewayErrorException 对象
|
||||
],
|
||||
//...
|
||||
]
|
||||
```
|
||||
|
||||
如果所选网关列表均发送失败时,将会抛出 `Overtrue\EasySms\Exceptions\NoGatewayAvailableException` 异常,你可以使用 `$e->results` 获取发送结果。
|
||||
|
||||
你也可以使用 `$e` 提供的更多便捷方法:
|
||||
|
||||
```php
|
||||
$e->getResults(); // 返回所有 API 的结果,结构同上
|
||||
$e->getExceptions(); // 返回所有调用异常列表
|
||||
$e->getException($gateway); // 返回指定网关名称的异常对象
|
||||
$e->getLastException(); // 获取最后一个失败的异常对象
|
||||
```
|
||||
|
||||
## 自定义网关
|
||||
|
||||
本拓展已经支持用户自定义网关,你可以很方便的配置即可当成与其它拓展一样的使用:
|
||||
|
||||
```php
|
||||
$config = [
|
||||
...
|
||||
'default' => [
|
||||
'gateways' => [
|
||||
'mygateway', // 配置你的网站到可用的网关列表
|
||||
],
|
||||
],
|
||||
'gateways' => [
|
||||
'mygateway' => [...], // 你网关所需要的参数,如果没有可以不配置
|
||||
],
|
||||
];
|
||||
|
||||
$easySms = new EasySms($config);
|
||||
|
||||
// 注册
|
||||
$easySms->extend('mygateway', function($gatewayConfig){
|
||||
// $gatewayConfig 来自配置文件里的 `gateways.mygateway`
|
||||
return new MyGateway($gatewayConfig);
|
||||
});
|
||||
|
||||
$easySms->send(13188888888, [
|
||||
'content' => '您的验证码为: 6379',
|
||||
'template' => 'SMS_001',
|
||||
'data' => [
|
||||
'code' => 6379
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
## 国际短信
|
||||
|
||||
国际短信与国内短信的区别是号码前面需要加国际码,但是由于各平台对国际号码的写法不一致,所以在发送国际短信的时候有一点区别:
|
||||
|
||||
```php
|
||||
use Overtrue\EasySms\PhoneNumber;
|
||||
|
||||
// 发送到国际码为 31 的国际号码
|
||||
$number = new PhoneNumber(13188888888, 31);
|
||||
|
||||
$easySms->send($number, [
|
||||
'content' => '您的验证码为: 6379',
|
||||
'template' => 'SMS_001',
|
||||
'data' => [
|
||||
'code' => 6379
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
## 定义短信
|
||||
|
||||
你可以根据发送场景的不同,定义不同的短信类,从而实现一处定义多处调用,你可以继承 `Overtrue\EasySms\Message` 来定义短信模型:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Overtrue\EasySms\Message;
|
||||
use Overtrue\EasySms\Contracts\GatewayInterface;
|
||||
use Overtrue\EasySms\Strategies\OrderStrategy;
|
||||
|
||||
class OrderPaidMessage extends Message
|
||||
{
|
||||
protected $order;
|
||||
protected $strategy = OrderStrategy::class; // 定义本短信的网关使用策略,覆盖全局配置中的 `default.strategy`
|
||||
protected $gateways = ['alidayu', 'yunpian', 'juhe']; // 定义本短信的适用平台,覆盖全局配置中的 `default.gateways`
|
||||
|
||||
public function __construct($order)
|
||||
{
|
||||
$this->order = $order;
|
||||
}
|
||||
|
||||
// 定义直接使用内容发送平台的内容
|
||||
public function getContent(GatewayInterface $gateway = null)
|
||||
{
|
||||
return sprintf('您的订单:%s, 已经完成付款', $this->order->no);
|
||||
}
|
||||
|
||||
// 定义使用模板发送方式平台所需要的模板 ID
|
||||
public function getTemplate(GatewayInterface $gateway = null)
|
||||
{
|
||||
return 'SMS_003';
|
||||
}
|
||||
|
||||
// 模板参数
|
||||
public function getData(GatewayInterface $gateway = null)
|
||||
{
|
||||
return [
|
||||
'order_no' => $this->order->no
|
||||
];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> 更多自定义方式请参考:[`Overtrue\EasySms\Message`](Overtrue\EasySms\Message;)
|
||||
|
||||
发送自定义短信:
|
||||
|
||||
```php
|
||||
$order = ...;
|
||||
$message = new OrderPaidMessage($order);
|
||||
|
||||
$easySms->send(13188888888, $message);
|
||||
```
|
||||
|
||||
## 各平台配置说明
|
||||
|
||||
### [阿里云](https://www.aliyun.com/)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'aliyun' => [
|
||||
'access_key_id' => '',
|
||||
'access_key_secret' => '',
|
||||
'sign_name' => '',
|
||||
],
|
||||
```
|
||||
|
||||
### [阿里云Rest](https://www.aliyun.com/)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'aliyunrest' => [
|
||||
'app_key' => '',
|
||||
'app_secret_key' => '',
|
||||
'sign_name' => '',
|
||||
],
|
||||
```
|
||||
|
||||
### [云片](https://www.yunpian.com)
|
||||
|
||||
短信内容使用 `content`
|
||||
|
||||
```php
|
||||
'yunpian' => [
|
||||
'api_key' => '',
|
||||
'signature' => '【默认签名】', // 内容中无签名时使用
|
||||
],
|
||||
```
|
||||
|
||||
### [Submail](https://www.mysubmail.com)
|
||||
|
||||
短信内容使用 `data`
|
||||
|
||||
```php
|
||||
'submail' => [
|
||||
'app_id' => '',
|
||||
'app_key' => '',
|
||||
'project' => '', // 默认 project,可在发送时 data 中指定
|
||||
],
|
||||
```
|
||||
|
||||
### [螺丝帽](https://luosimao.com/)
|
||||
|
||||
短信内容使用 `content`
|
||||
|
||||
```php
|
||||
'luosimao' => [
|
||||
'api_key' => '',
|
||||
],
|
||||
```
|
||||
|
||||
### [容联云通讯](http://www.yuntongxun.com)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'yuntongxun' => [
|
||||
'app_id' => '',
|
||||
'account_sid' => '',
|
||||
'account_token' => '',
|
||||
'is_sub_account' => false,
|
||||
],
|
||||
```
|
||||
|
||||
### [互亿无线](http://www.ihuyi.com)
|
||||
|
||||
短信内容使用 `content`
|
||||
|
||||
```php
|
||||
'huyi' => [
|
||||
'api_id' => '',
|
||||
'api_key' => '',
|
||||
'signature' => '',
|
||||
],
|
||||
```
|
||||
|
||||
### [聚合数据](https://www.juhe.cn)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'juhe' => [
|
||||
'app_key' => '',
|
||||
],
|
||||
```
|
||||
|
||||
### [SendCloud](http://www.sendcloud.net/)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'sendcloud' => [
|
||||
'sms_user' => '',
|
||||
'sms_key' => '',
|
||||
'timestamp' => false, // 是否启用时间戳
|
||||
],
|
||||
```
|
||||
### [百度云](https://cloud.baidu.com/)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'baidu' => [
|
||||
'ak' => '',
|
||||
'sk' => '',
|
||||
'invoke_id' => '',
|
||||
'domain' => '',
|
||||
],
|
||||
```
|
||||
|
||||
### [华信短信平台](http://www.ipyy.com/)
|
||||
|
||||
短信内容使用 `content`
|
||||
|
||||
```php
|
||||
'huaxin' => [
|
||||
'user_id' => '',
|
||||
'password' => '',
|
||||
'account' => '',
|
||||
'ip' => '',
|
||||
'ext_no' => '',
|
||||
],
|
||||
```
|
||||
|
||||
### [253云通讯(创蓝)](https://www.253.com/)
|
||||
|
||||
短信内容使用 `content`
|
||||
|
||||
```php
|
||||
'chuanglan' => [
|
||||
'account' => '',
|
||||
'password' => '',
|
||||
|
||||
// 国际短信时必填
|
||||
'intel_account' => '',
|
||||
'intel_password' => '',
|
||||
|
||||
// \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_VALIDATE_CODE => 验证码通道(默认)
|
||||
// \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_PROMOTION_CODE => 会员营销通道
|
||||
'channel' => \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_VALIDATE_CODE,
|
||||
|
||||
// 会员营销通道 特定参数。创蓝规定:api提交营销短信的时候,需要自己加短信的签名及退订信息
|
||||
'sign' => '【通讯云】',
|
||||
'unsubscribe' => '回TD退订',
|
||||
],
|
||||
```
|
||||
|
||||
### [融云](http://www.rongcloud.cn)
|
||||
|
||||
短信分为两大类,验证类和通知类短信。 发送验证类短信使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'rongcloud' => [
|
||||
'app_key' => '',
|
||||
'app_secret' => '',
|
||||
]
|
||||
```
|
||||
|
||||
### [天毅无线](http://www.85hu.com/)
|
||||
|
||||
短信内容使用 `content`
|
||||
|
||||
```php
|
||||
'tianyiwuxian' => [
|
||||
'username' => '', //用户名
|
||||
'password' => '', //密码
|
||||
'gwid' => '', //网关ID
|
||||
]
|
||||
```
|
||||
|
||||
### [twilio](https://www.twilio.com)
|
||||
|
||||
短信使用 `content`
|
||||
发送对象需要 使用`+`添加区号
|
||||
|
||||
```php
|
||||
'twilio' => [
|
||||
'account_sid' => '', // sid
|
||||
'from' => '', // 发送的号码 可以在控制台购买
|
||||
'token' => '', // apitoken
|
||||
],
|
||||
```
|
||||
|
||||
### [tiniyo](https://www.tiniyo.com)
|
||||
|
||||
短信使用 `content`
|
||||
发送对象需要 使用`+`添加区号
|
||||
|
||||
```php
|
||||
'tiniyo' => [
|
||||
'account_sid' => '', // auth_id from https://tiniyo.com
|
||||
'from' => '', // 发送的号码 可以在控制台购买
|
||||
'token' => '', // auth_secret from https://tiniyo.com
|
||||
],
|
||||
```
|
||||
|
||||
|
||||
### [腾讯云 SMS](https://cloud.tencent.com/product/sms)
|
||||
|
||||
短信内容使用 `content`
|
||||
|
||||
```php
|
||||
'qcloud' => [
|
||||
'sdk_app_id' => '', // SDK APP ID
|
||||
'app_key' => '', // APP KEY
|
||||
'sign_name' => '', // 短信签名,如果使用默认签名,该字段可缺省(对应官方文档中的sign)
|
||||
],
|
||||
```
|
||||
|
||||
发送示例:
|
||||
|
||||
```php
|
||||
$easySms->send(18888888888, [
|
||||
'template' => 101234, // 模板ID
|
||||
'content' => "您的动态验证码为:{1},请于5分钟内完成验证,如非本人操作,请忽略本短信!", // 模板内容
|
||||
'data' => [
|
||||
$code, // 模板变量
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
### [阿凡达数据](http://www.avatardata.cn/)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'avatardata' => [
|
||||
'app_key' => '', // APP KEY
|
||||
],
|
||||
```
|
||||
|
||||
### [华为云 SMS](https://www.huaweicloud.com/product/msgsms.html)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'huawei' => [
|
||||
'endpoint' => '', // APP接入地址
|
||||
'app_key' => '', // APP KEY
|
||||
'app_secret' => '', // APP SECRET
|
||||
'from' => [
|
||||
'default' => '1069012345', // 默认使用签名通道号
|
||||
'custom' => 'csms12345', // 其他签名通道号 可以在 data 中定义 from 来指定
|
||||
'abc' => 'csms67890', // 其他签名通道号
|
||||
...
|
||||
],
|
||||
'callback' => '' // 短信状态回调地址
|
||||
],
|
||||
```
|
||||
|
||||
使用默认签名通道 `default`
|
||||
|
||||
```php
|
||||
$easySms->send(13188888888, [
|
||||
'template' => 'SMS_001',
|
||||
'data' => [
|
||||
6379
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
使用指定签名通道
|
||||
|
||||
```php
|
||||
$easySms->send(13188888888, [
|
||||
'template' => 'SMS_001',
|
||||
'data' => [
|
||||
6379,
|
||||
'from' => 'custom' // 对应 config 中的 from 数组中 custom
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
### [网易云信](https://yunxin.163.com/sms)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'yunxin' => [
|
||||
'app_key' => '',
|
||||
'app_secret' => '',
|
||||
'code_length' => 4, // 随机验证码长度,范围 4~10,默认为 4
|
||||
'need_up' => false, // 是否需要支持短信上行
|
||||
],
|
||||
```
|
||||
|
||||
```php
|
||||
$easySms->send(18888888888, [
|
||||
'template' => 'SMS_001', // 不填则使用默认模板
|
||||
'data' => [
|
||||
'code' => 8946, // 如果设置了该参数,则 code_length 参数无效
|
||||
'action' => 'sendCode', // 默认为 `sendCode`,校验短信验证码使用 `verifyCode`
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
### [云之讯](https://www.ucpaas.com/index.html)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'yunzhixun' => [
|
||||
'sid' => '',
|
||||
'token' => '',
|
||||
'app_id' => '',
|
||||
],
|
||||
```
|
||||
|
||||
```php
|
||||
$easySms->send(18888888888, [
|
||||
'template' => 'SMS_001',
|
||||
'data' => [
|
||||
'params' => '8946,3', // 模板参数,多个参数使用 `,` 分割,模板无参数时可为空
|
||||
'uid' => 'hexianghui', // 用户 ID,随状态报告返回,可为空
|
||||
'mobiles' => '18888888888,188888888889', // 批量发送短信,手机号使用 `,` 分割,不使用批量发送请不要设置该参数
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
### [凯信通](http://www.kingtto.cn/)
|
||||
|
||||
短信内容使用 `content`
|
||||
|
||||
```php
|
||||
'kingtto' => [
|
||||
'userid' => '',
|
||||
'account' => '',
|
||||
'password' => '',
|
||||
],
|
||||
```
|
||||
|
||||
```php
|
||||
$easySms->send(18888888888, [
|
||||
'content' => '您的验证码为: 6379',
|
||||
]);
|
||||
```
|
||||
|
||||
### [七牛云](https://www.qiniu.com/)
|
||||
|
||||
短信内容使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'qiniu' => [
|
||||
'secret_key' => '',
|
||||
'access_key' => '',
|
||||
],
|
||||
```
|
||||
|
||||
```php
|
||||
$easySms->send(18888888888, [
|
||||
'template' => '1231234123412341234',
|
||||
'data' => [
|
||||
'code' => 1234,
|
||||
],
|
||||
]);
|
||||
```
|
||||
### [Ucloud](https://www.ucloud.cn/)
|
||||
短信使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'ucloud' => [
|
||||
'private_key' => '', //私钥
|
||||
'public_key' => '', //公钥
|
||||
’sig_content‘ => '', // 短信签名,
|
||||
'project_id' => '', //项目ID,子账号才需要该参数
|
||||
],
|
||||
```
|
||||
|
||||
```php
|
||||
$easySms->send(18888888888, [
|
||||
'template' => 'UTAXXXXX', //短信模板
|
||||
'data' => [
|
||||
'code' => 1234, //模板参数,模板没有参数不用则填写,有多个参数请用数组,[1111,1111]
|
||||
'mobiles' =>'', //同时发送多个手机短信,请用数组[xxx,xxx]
|
||||
],
|
||||
]);
|
||||
|
||||
```
|
||||
|
||||
|
||||
### [短信宝](http://www.smsbao.com/)
|
||||
短信使用 `template`
|
||||
|
||||
```php
|
||||
'smsbao' => [
|
||||
'user' => '', //账号
|
||||
'password' => '' //密码
|
||||
],
|
||||
```
|
||||
|
||||
```php
|
||||
$easySms->send(18888888888, [
|
||||
'template' => '您的验证码为: 6379', //短信模板
|
||||
]);
|
||||
|
||||
```
|
||||
|
||||
### [摩杜云](https://www.moduyun.com/)
|
||||
短信使用 `template` + `data`
|
||||
|
||||
```php
|
||||
'moduyun' => [
|
||||
'accesskey' => '', //必填 ACCESS KEY
|
||||
'secretkey' => '', //必填 SECRET KEY
|
||||
'signId' => '', //选填 短信签名,如果使用默认签名,该字段可缺省
|
||||
'type' => 0, //选填 0:普通短信;1:营销短信
|
||||
],
|
||||
```
|
||||
|
||||
```php
|
||||
$easySms->send(18888888888, [
|
||||
'template' => '5a95****b953', //短信模板
|
||||
'data' => [
|
||||
1234, //模板参数,对应模板的{1}
|
||||
30 //模板参数,对应模板的{2}
|
||||
//...
|
||||
],
|
||||
]);
|
||||
|
||||
```
|
||||
|
||||
## PHP 扩展包开发
|
||||
|
||||
> 想知道如何从零开始构建 PHP 扩展包?
|
||||
>
|
||||
> 请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
29
vendor/overtrue/easy-sms/composer.json
vendored
Executable file
29
vendor/overtrue/easy-sms/composer.json
vendored
Executable file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "overtrue/easy-sms",
|
||||
"description": "The easiest way to send short message.",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^6.2 || ^7.0",
|
||||
"php": ">=5.6",
|
||||
"ext-json": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^5.7 || ^7.5",
|
||||
"mockery/mockery": "1.3.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Overtrue\\EasySms\\": "src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Overtrue\\EasySms\\Tests\\": "tests"
|
||||
}
|
||||
},
|
||||
"license": "MIT",
|
||||
"authors": [{
|
||||
"name": "overtrue",
|
||||
"email": "i@overtrue.me"
|
||||
}]
|
||||
}
|
||||
38
vendor/overtrue/easy-sms/src/Contracts/GatewayInterface.php
vendored
Executable file
38
vendor/overtrue/easy-sms/src/Contracts/GatewayInterface.php
vendored
Executable file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Contracts;
|
||||
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
|
||||
/**
|
||||
* Class GatewayInterface.
|
||||
*/
|
||||
interface GatewayInterface
|
||||
{
|
||||
/**
|
||||
* Get gateway name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Send a short message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config);
|
||||
}
|
||||
63
vendor/overtrue/easy-sms/src/Contracts/MessageInterface.php
vendored
Executable file
63
vendor/overtrue/easy-sms/src/Contracts/MessageInterface.php
vendored
Executable file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Contracts;
|
||||
|
||||
/**
|
||||
* Interface MessageInterface.
|
||||
*/
|
||||
interface MessageInterface
|
||||
{
|
||||
const TEXT_MESSAGE = 'text';
|
||||
|
||||
const VOICE_MESSAGE = 'voice';
|
||||
|
||||
/**
|
||||
* Return the message type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageType();
|
||||
|
||||
/**
|
||||
* Return message content.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContent(GatewayInterface $gateway = null);
|
||||
|
||||
/**
|
||||
* Return the template id of message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTemplate(GatewayInterface $gateway = null);
|
||||
|
||||
/**
|
||||
* Return the template data of message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData(GatewayInterface $gateway = null);
|
||||
|
||||
/**
|
||||
* Return message supported gateways.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGateways();
|
||||
}
|
||||
53
vendor/overtrue/easy-sms/src/Contracts/PhoneNumberInterface.php
vendored
Executable file
53
vendor/overtrue/easy-sms/src/Contracts/PhoneNumberInterface.php
vendored
Executable file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Contracts;
|
||||
|
||||
/**
|
||||
* Interface PhoneNumberInterface.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
interface PhoneNumberInterface extends \JsonSerializable
|
||||
{
|
||||
/**
|
||||
* 86.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getIDDCode();
|
||||
|
||||
/**
|
||||
* 18888888888.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getNumber();
|
||||
|
||||
/**
|
||||
* +8618888888888.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUniversalNumber();
|
||||
|
||||
/**
|
||||
* 008618888888888.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getZeroPrefixedNumber();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString();
|
||||
}
|
||||
27
vendor/overtrue/easy-sms/src/Contracts/StrategyInterface.php
vendored
Executable file
27
vendor/overtrue/easy-sms/src/Contracts/StrategyInterface.php
vendored
Executable file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Contracts;
|
||||
|
||||
/**
|
||||
* Interface StrategyInterface.
|
||||
*/
|
||||
interface StrategyInterface
|
||||
{
|
||||
/**
|
||||
* Apply the strategy and return result.
|
||||
*
|
||||
* @param array $gateways
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function apply(array $gateways);
|
||||
}
|
||||
327
vendor/overtrue/easy-sms/src/EasySms.php
vendored
Executable file
327
vendor/overtrue/easy-sms/src/EasySms.php
vendored
Executable file
@@ -0,0 +1,327 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms;
|
||||
|
||||
use Closure;
|
||||
use Overtrue\EasySms\Contracts\GatewayInterface;
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Contracts\StrategyInterface;
|
||||
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
|
||||
use Overtrue\EasySms\Gateways\Gateway;
|
||||
use Overtrue\EasySms\Strategies\OrderStrategy;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class EasySms.
|
||||
*/
|
||||
class EasySms
|
||||
{
|
||||
/**
|
||||
* @var \Overtrue\EasySms\Support\Config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $defaultGateway;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $customCreators = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $gateways = [];
|
||||
|
||||
/**
|
||||
* @var \Overtrue\EasySms\Messenger
|
||||
*/
|
||||
protected $messenger;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $strategies = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = new Config($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* @param string|array $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface|array $message
|
||||
* @param array $gateways
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
|
||||
* @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
|
||||
*/
|
||||
public function send($to, $message, array $gateways = [])
|
||||
{
|
||||
$to = $this->formatPhoneNumber($to);
|
||||
$message = $this->formatMessage($message);
|
||||
$gateways = empty($gateways) ? $message->getGateways() : $gateways;
|
||||
|
||||
if (empty($gateways)) {
|
||||
$gateways = $this->config->get('default.gateways', []);
|
||||
}
|
||||
|
||||
return $this->getMessenger()->send($to, $message, $this->formatGateways($gateways));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a gateway.
|
||||
*
|
||||
* @param string|null $name
|
||||
*
|
||||
* @return \Overtrue\EasySms\Contracts\GatewayInterface
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
public function gateway($name)
|
||||
{
|
||||
if (!isset($this->gateways[$name])) {
|
||||
$this->gateways[$name] = $this->createGateway($name);
|
||||
}
|
||||
|
||||
return $this->gateways[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a strategy instance.
|
||||
*
|
||||
* @param string|null $strategy
|
||||
*
|
||||
* @return \Overtrue\EasySms\Contracts\StrategyInterface
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
public function strategy($strategy = null)
|
||||
{
|
||||
if (\is_null($strategy)) {
|
||||
$strategy = $this->config->get('default.strategy', OrderStrategy::class);
|
||||
}
|
||||
|
||||
if (!\class_exists($strategy)) {
|
||||
$strategy = __NAMESPACE__.'\Strategies\\'.\ucfirst($strategy);
|
||||
}
|
||||
|
||||
if (!\class_exists($strategy)) {
|
||||
throw new InvalidArgumentException("Unsupported strategy \"{$strategy}\"");
|
||||
}
|
||||
|
||||
if (empty($this->strategies[$strategy]) || !($this->strategies[$strategy] instanceof StrategyInterface)) {
|
||||
$this->strategies[$strategy] = new $strategy($this);
|
||||
}
|
||||
|
||||
return $this->strategies[$strategy];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom driver creator Closure.
|
||||
*
|
||||
* @param string $name
|
||||
* @param \Closure $callback
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function extend($name, Closure $callback)
|
||||
{
|
||||
$this->customCreators[$name] = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Overtrue\EasySms\Support\Config
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Overtrue\EasySms\Messenger
|
||||
*/
|
||||
public function getMessenger()
|
||||
{
|
||||
return $this->messenger ?: $this->messenger = new Messenger($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new driver instance.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return GatewayInterface
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
protected function createGateway($name)
|
||||
{
|
||||
$config = $this->config->get("gateways.{$name}", []);
|
||||
|
||||
if (!isset($config['timeout'])) {
|
||||
$config['timeout'] = $this->config->get('timeout', Gateway::DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
$config['options'] = $this->config->get('options', []);
|
||||
|
||||
if (isset($this->customCreators[$name])) {
|
||||
$gateway = $this->callCustomCreator($name, $config);
|
||||
} else {
|
||||
$className = $this->formatGatewayClassName($name);
|
||||
$gateway = $this->makeGateway($className, $config);
|
||||
}
|
||||
|
||||
if (!($gateway instanceof GatewayInterface)) {
|
||||
throw new InvalidArgumentException(\sprintf('Gateway "%s" must implement interface %s.', $name, GatewayInterface::class));
|
||||
}
|
||||
|
||||
return $gateway;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make gateway instance.
|
||||
*
|
||||
* @param string $gateway
|
||||
* @param array $config
|
||||
*
|
||||
* @return \Overtrue\EasySms\Contracts\GatewayInterface
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
protected function makeGateway($gateway, $config)
|
||||
{
|
||||
if (!\class_exists($gateway) || !\in_array(GatewayInterface::class, \class_implements($gateway))) {
|
||||
throw new InvalidArgumentException(\sprintf('Class "%s" is a invalid easy-sms gateway.', $gateway));
|
||||
}
|
||||
|
||||
return new $gateway($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format gateway name.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function formatGatewayClassName($name)
|
||||
{
|
||||
if (\class_exists($name) && \in_array(GatewayInterface::class, \class_implements($name))) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
$name = \ucfirst(\str_replace(['-', '_', ''], '', $name));
|
||||
|
||||
return __NAMESPACE__."\\Gateways\\{$name}Gateway";
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a custom gateway creator.
|
||||
*
|
||||
* @param string $gateway
|
||||
* @param array $config
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function callCustomCreator($gateway, $config)
|
||||
{
|
||||
return \call_user_func($this->customCreators[$gateway], $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|\Overtrue\EasySms\Contracts\PhoneNumberInterface $number
|
||||
*
|
||||
* @return \Overtrue\EasySms\Contracts\PhoneNumberInterface|string
|
||||
*/
|
||||
protected function formatPhoneNumber($number)
|
||||
{
|
||||
if ($number instanceof PhoneNumberInterface) {
|
||||
return $number;
|
||||
}
|
||||
|
||||
return new PhoneNumber(\trim($number));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string|\Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
*
|
||||
* @return \Overtrue\EasySms\Contracts\MessageInterface
|
||||
*/
|
||||
protected function formatMessage($message)
|
||||
{
|
||||
if (!($message instanceof MessageInterface)) {
|
||||
if (!\is_array($message)) {
|
||||
$message = [
|
||||
'content' => $message,
|
||||
'template' => $message,
|
||||
];
|
||||
}
|
||||
|
||||
$message = new Message($message);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $gateways
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
protected function formatGateways(array $gateways)
|
||||
{
|
||||
$formatted = [];
|
||||
|
||||
foreach ($gateways as $gateway => $setting) {
|
||||
if (\is_int($gateway) && \is_string($setting)) {
|
||||
$gateway = $setting;
|
||||
$setting = [];
|
||||
}
|
||||
|
||||
$formatted[$gateway] = $setting;
|
||||
$globalSettings = $this->config->get("gateways.{$gateway}", []);
|
||||
|
||||
if (\is_string($gateway) && !empty($globalSettings) && \is_array($setting)) {
|
||||
$formatted[$gateway] = new Config(\array_merge($globalSettings, $setting));
|
||||
}
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($this->strategy()->apply($formatted) as $name) {
|
||||
$result[$name] = $formatted[$name];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
21
vendor/overtrue/easy-sms/src/Exceptions/Exception.php
vendored
Executable file
21
vendor/overtrue/easy-sms/src/Exceptions/Exception.php
vendored
Executable file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Exceptions;
|
||||
|
||||
/**
|
||||
* Class Exception.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
class Exception extends \Exception
|
||||
{
|
||||
}
|
||||
37
vendor/overtrue/easy-sms/src/Exceptions/GatewayErrorException.php
vendored
Executable file
37
vendor/overtrue/easy-sms/src/Exceptions/GatewayErrorException.php
vendored
Executable file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Exceptions;
|
||||
|
||||
/**
|
||||
* Class GatewayErrorException.
|
||||
*/
|
||||
class GatewayErrorException extends Exception
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $raw = [];
|
||||
|
||||
/**
|
||||
* GatewayErrorException constructor.
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param array $raw
|
||||
*/
|
||||
public function __construct($message, $code, array $raw = [])
|
||||
{
|
||||
parent::__construct($message, intval($code));
|
||||
|
||||
$this->raw = $raw;
|
||||
}
|
||||
}
|
||||
19
vendor/overtrue/easy-sms/src/Exceptions/InvalidArgumentException.php
vendored
Executable file
19
vendor/overtrue/easy-sms/src/Exceptions/InvalidArgumentException.php
vendored
Executable file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Exceptions;
|
||||
|
||||
/**
|
||||
* Class InvalidArgumentException.
|
||||
*/
|
||||
class InvalidArgumentException extends Exception
|
||||
{
|
||||
}
|
||||
81
vendor/overtrue/easy-sms/src/Exceptions/NoGatewayAvailableException.php
vendored
Executable file
81
vendor/overtrue/easy-sms/src/Exceptions/NoGatewayAvailableException.php
vendored
Executable file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Exceptions;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class NoGatewayAvailableException.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
class NoGatewayAvailableException extends Exception
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $results = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $exceptions = [];
|
||||
|
||||
/**
|
||||
* NoGatewayAvailableException constructor.
|
||||
*
|
||||
* @param array $results
|
||||
* @param int $code
|
||||
* @param \Throwable|null $previous
|
||||
*/
|
||||
public function __construct(array $results = [], $code = 0, Throwable $previous = null)
|
||||
{
|
||||
$this->results = $results;
|
||||
$this->exceptions = \array_column($results, 'exception', 'gateway');
|
||||
|
||||
parent::__construct('All the gateways have failed. You can get error details by `$exception->getExceptions()`', $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getResults()
|
||||
{
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $gateway
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getException($gateway)
|
||||
{
|
||||
return isset($this->exceptions[$gateway]) ? $this->exceptions[$gateway] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getExceptions()
|
||||
{
|
||||
return $this->exceptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLastException()
|
||||
{
|
||||
return end($this->exceptions);
|
||||
}
|
||||
}
|
||||
104
vendor/overtrue/easy-sms/src/Gateways/AliyunGateway.php
vendored
Executable file
104
vendor/overtrue/easy-sms/src/Gateways/AliyunGateway.php
vendored
Executable file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class AliyunGateway.
|
||||
*
|
||||
* @author carson <docxcn@gmail.com>
|
||||
*
|
||||
* @see https://help.aliyun.com/document_detail/55451.html
|
||||
*/
|
||||
class AliyunGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'http://dysmsapi.aliyuncs.com';
|
||||
|
||||
const ENDPOINT_METHOD = 'SendSms';
|
||||
|
||||
const ENDPOINT_VERSION = '2017-05-25';
|
||||
|
||||
const ENDPOINT_FORMAT = 'JSON';
|
||||
|
||||
const ENDPOINT_REGION_ID = 'cn-hangzhou';
|
||||
|
||||
const ENDPOINT_SIGNATURE_METHOD = 'HMAC-SHA1';
|
||||
|
||||
const ENDPOINT_SIGNATURE_VERSION = '1.0';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getData($this);
|
||||
|
||||
$signName = !empty($data['sign_name']) ? $data['sign_name'] : $config->get('sign_name');
|
||||
|
||||
unset($data['sign_name']);
|
||||
|
||||
$params = [
|
||||
'RegionId' => self::ENDPOINT_REGION_ID,
|
||||
'AccessKeyId' => $config->get('access_key_id'),
|
||||
'Format' => self::ENDPOINT_FORMAT,
|
||||
'SignatureMethod' => self::ENDPOINT_SIGNATURE_METHOD,
|
||||
'SignatureVersion' => self::ENDPOINT_SIGNATURE_VERSION,
|
||||
'SignatureNonce' => uniqid(),
|
||||
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
|
||||
'Action' => self::ENDPOINT_METHOD,
|
||||
'Version' => self::ENDPOINT_VERSION,
|
||||
'PhoneNumbers' => !\is_null($to->getIDDCode()) ? strval($to->getZeroPrefixedNumber()) : $to->getNumber(),
|
||||
'SignName' => $signName,
|
||||
'TemplateCode' => $message->getTemplate($this),
|
||||
'TemplateParam' => json_encode($data, JSON_FORCE_OBJECT),
|
||||
];
|
||||
|
||||
$params['Signature'] = $this->generateSign($params);
|
||||
|
||||
$result = $this->get(self::ENDPOINT_URL, $params);
|
||||
|
||||
if ('OK' != $result['Code']) {
|
||||
throw new GatewayErrorException($result['Message'], $result['Code'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Sign.
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSign($params)
|
||||
{
|
||||
ksort($params);
|
||||
$accessKeySecret = $this->config->get('access_key_secret');
|
||||
$stringToSign = 'GET&%2F&'.urlencode(http_build_query($params, null, '&', PHP_QUERY_RFC3986));
|
||||
|
||||
return base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret.'&', true));
|
||||
}
|
||||
}
|
||||
107
vendor/overtrue/easy-sms/src/Gateways/AliyunrestGateway.php
vendored
Executable file
107
vendor/overtrue/easy-sms/src/Gateways/AliyunrestGateway.php
vendored
Executable file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class AliyunrestGateway.
|
||||
*/
|
||||
class AliyunrestGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'http://gw.api.taobao.com/router/rest';
|
||||
|
||||
const ENDPOINT_VERSION = '2.0';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
const ENDPOINT_METHOD = 'alibaba.aliqin.fc.sms.num.send';
|
||||
|
||||
const ENDPOINT_SIGNATURE_METHOD = 'md5';
|
||||
|
||||
const ENDPOINT_PARTNER_ID = 'EasySms';
|
||||
|
||||
/**
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array|void
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$urlParams = [
|
||||
'app_key' => $config->get('app_key'),
|
||||
'v' => self::ENDPOINT_VERSION,
|
||||
'format' => self::ENDPOINT_FORMAT,
|
||||
'sign_method' => self::ENDPOINT_SIGNATURE_METHOD,
|
||||
'method' => self::ENDPOINT_METHOD,
|
||||
'timestamp' => date('Y-m-d H:i:s'),
|
||||
'partner_id' => self::ENDPOINT_PARTNER_ID,
|
||||
];
|
||||
|
||||
$params = [
|
||||
'extend' => '',
|
||||
'sms_type' => 'normal',
|
||||
'sms_free_sign_name' => $config->get('sign_name'),
|
||||
'sms_param' => json_encode($message->getData($this)),
|
||||
'rec_num' => !\is_null($to->getIDDCode()) ? strval($to->getZeroPrefixedNumber()) : $to->getNumber(),
|
||||
'sms_template_code' => $message->getTemplate($this),
|
||||
];
|
||||
$urlParams['sign'] = $this->generateSign(array_merge($params, $urlParams));
|
||||
|
||||
$result = $this->post($this->getEndpointUrl($urlParams), $params);
|
||||
|
||||
if (isset($result['error_response']) && 0 != $result['error_response']['code']) {
|
||||
throw new GatewayErrorException($result['error_response']['msg'], $result['error_response']['code'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getEndpointUrl($params)
|
||||
{
|
||||
return self::ENDPOINT_URL.'?'.http_build_query($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSign($params)
|
||||
{
|
||||
ksort($params);
|
||||
|
||||
$stringToBeSigned = $this->config->get('app_secret_key');
|
||||
foreach ($params as $k => $v) {
|
||||
if (!is_array($v) && '@' != substr($v, 0, 1)) {
|
||||
$stringToBeSigned .= "$k$v";
|
||||
}
|
||||
}
|
||||
unset($k, $v);
|
||||
$stringToBeSigned .= $this->config->get('app_secret_key');
|
||||
|
||||
return strtoupper(md5($stringToBeSigned));
|
||||
}
|
||||
}
|
||||
60
vendor/overtrue/easy-sms/src/Gateways/AvatardataGateway.php
vendored
Executable file
60
vendor/overtrue/easy-sms/src/Gateways/AvatardataGateway.php
vendored
Executable file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class AvatardataGateway.
|
||||
*
|
||||
* @see http://www.avatardata.cn/Docs/Api/fd475e40-7809-4be7-936c-5926dd41b0fe
|
||||
*/
|
||||
class AvatardataGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'http://v1.avatardata.cn/Sms/Send';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws GatewayErrorException;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$params = [
|
||||
'mobile' => $to->getNumber(),
|
||||
'templateId' => $message->getTemplate($this),
|
||||
'param' => implode(',', $message->getData($this)),
|
||||
'dtype' => self::ENDPOINT_FORMAT,
|
||||
'key' => $config->get('app_key'),
|
||||
];
|
||||
|
||||
$result = $this->get(self::ENDPOINT_URL, $params);
|
||||
|
||||
if ($result['error_code']) {
|
||||
throw new GatewayErrorException($result['reason'], $result['error_code'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
174
vendor/overtrue/easy-sms/src/Gateways/BaiduGateway.php
vendored
Executable file
174
vendor/overtrue/easy-sms/src/Gateways/BaiduGateway.php
vendored
Executable file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class BaiduGateway.
|
||||
*
|
||||
* @see https://cloud.baidu.com/doc/SMS/index.html
|
||||
*/
|
||||
class BaiduGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_HOST = 'smsv3.bj.baidubce.com';
|
||||
|
||||
const ENDPOINT_URI = '/api/v3/sendSms';
|
||||
|
||||
const BCE_AUTH_VERSION = 'bce-auth-v1';
|
||||
|
||||
const DEFAULT_EXPIRATION_IN_SECONDS = 1800; //签名有效期默认1800秒
|
||||
|
||||
const SUCCESS_CODE = 1000;
|
||||
|
||||
/**
|
||||
* Send message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$params = [
|
||||
'signatureId' => $config->get('invoke_id'),
|
||||
'mobile' => $to->getNumber(),
|
||||
'template' => $message->getTemplate($this),
|
||||
'contentVar' => $message->getData($this),
|
||||
];
|
||||
if (!empty($params['contentVar']['custom'])) {
|
||||
//用户自定义参数,格式为字符串,状态回调时会回传该值
|
||||
$params['custom'] = $params['contentVar']['custom'];
|
||||
unset($params['contentVar']['custom']);
|
||||
}
|
||||
if (!empty($params['contentVar']['userExtId'])) {
|
||||
//通道自定义扩展码,上行回调时会回传该值,其格式为纯数字串。默认为不开通,请求时无需设置该参数。如需开通请联系客服申请
|
||||
$params['userExtId'] = $params['contentVar']['userExtId'];
|
||||
unset($params['contentVar']['userExtId']);
|
||||
}
|
||||
|
||||
$datetime = gmdate('Y-m-d\TH:i:s\Z');
|
||||
|
||||
$headers = [
|
||||
'host' => self::ENDPOINT_HOST,
|
||||
'content-type' => 'application/json',
|
||||
'x-bce-date' => $datetime,
|
||||
];
|
||||
//获得需要签名的数据
|
||||
$signHeaders = $this->getHeadersToSign($headers, ['host', 'x-bce-date']);
|
||||
|
||||
$headers['Authorization'] = $this->generateSign($signHeaders, $datetime, $config);
|
||||
|
||||
$result = $this->request('post', self::buildEndpoint($config), ['headers' => $headers, 'json' => $params]);
|
||||
|
||||
if (self::SUCCESS_CODE != $result['code']) {
|
||||
throw new GatewayErrorException($result['message'], $result['code'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint(Config $config)
|
||||
{
|
||||
return 'http://'.$config->get('domain', self::ENDPOINT_HOST).self::ENDPOINT_URI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Authorization header.
|
||||
*
|
||||
* @param array $signHeaders
|
||||
* @param int $datetime
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSign(array $signHeaders, $datetime, Config $config)
|
||||
{
|
||||
// 生成 authString
|
||||
$authString = self::BCE_AUTH_VERSION.'/'.$config->get('ak').'/'
|
||||
.$datetime.'/'.self::DEFAULT_EXPIRATION_IN_SECONDS;
|
||||
|
||||
// 使用 sk 和 authString 生成 signKey
|
||||
$signingKey = hash_hmac('sha256', $authString, $config->get('sk'));
|
||||
// 生成标准化 URI
|
||||
// 根据 RFC 3986,除了:1.大小写英文字符 2.阿拉伯数字 3.点'.'、波浪线'~'、减号'-'以及下划线'_' 以外都要编码
|
||||
$canonicalURI = str_replace('%2F', '/', rawurlencode(self::ENDPOINT_URI));
|
||||
|
||||
// 生成标准化 QueryString
|
||||
$canonicalQueryString = ''; // 此 api 不需要此项。返回空字符串
|
||||
|
||||
// 整理 headersToSign,以 ';' 号连接
|
||||
$signedHeaders = empty($signHeaders) ? '' : strtolower(trim(implode(';', array_keys($signHeaders))));
|
||||
|
||||
// 生成标准化 header
|
||||
$canonicalHeader = $this->getCanonicalHeaders($signHeaders);
|
||||
|
||||
// 组成标准请求串
|
||||
$canonicalRequest = "POST\n{$canonicalURI}\n{$canonicalQueryString}\n{$canonicalHeader}";
|
||||
|
||||
// 使用 signKey 和标准请求串完成签名
|
||||
$signature = hash_hmac('sha256', $canonicalRequest, $signingKey);
|
||||
|
||||
// 组成最终签名串
|
||||
return "{$authString}/{$signedHeaders}/{$signature}";
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成标准化 http 请求头串.
|
||||
*
|
||||
* @param array $headers
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getCanonicalHeaders(array $headers)
|
||||
{
|
||||
$headerStrings = [];
|
||||
foreach ($headers as $name => $value) {
|
||||
//trim后再encode,之后使用':'号连接起来
|
||||
$headerStrings[] = rawurlencode(strtolower(trim($name))).':'.rawurlencode(trim($value));
|
||||
}
|
||||
|
||||
sort($headerStrings);
|
||||
|
||||
return implode("\n", $headerStrings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 指定的 keys 过滤应该参与签名的 header.
|
||||
*
|
||||
* @param array $headers
|
||||
* @param array $keys
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getHeadersToSign(array $headers, array $keys)
|
||||
{
|
||||
return array_intersect_key($headers, array_flip($keys));
|
||||
}
|
||||
}
|
||||
156
vendor/overtrue/easy-sms/src/Gateways/ChuanglanGateway.php
vendored
Executable file
156
vendor/overtrue/easy-sms/src/Gateways/ChuanglanGateway.php
vendored
Executable file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class ChuanglanGateway.
|
||||
*
|
||||
* @see https://zz.253.com/v5.html#/api_doc
|
||||
*/
|
||||
class ChuanglanGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
/**
|
||||
* URL模板
|
||||
*/
|
||||
const ENDPOINT_URL_TEMPLATE = 'https://%s.253.com/msg/send/json';
|
||||
|
||||
/**
|
||||
* 国际短信
|
||||
*/
|
||||
const INT_URL = 'http://intapi.253.com/send/json';
|
||||
|
||||
/**
|
||||
* 验证码渠道code.
|
||||
*/
|
||||
const CHANNEL_VALIDATE_CODE = 'smsbj1';
|
||||
|
||||
/**
|
||||
* 会员营销渠道code.
|
||||
*/
|
||||
const CHANNEL_PROMOTION_CODE = 'smssh1';
|
||||
|
||||
/**
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws GatewayErrorException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$IDDCode = !empty($to->getIDDCode()) ? $to->getIDDCode() : 86;
|
||||
|
||||
$params = [
|
||||
'account' => $config->get('account'),
|
||||
'password' => $config->get('password'),
|
||||
'phone' => $to->getNumber(),
|
||||
'msg' => $this->wrapChannelContent($message->getContent($this), $config, $IDDCode),
|
||||
];
|
||||
|
||||
if (86 != $IDDCode) {
|
||||
$params['mobile'] = $to->getIDDCode().$to->getNumber();
|
||||
$params['account'] = $config->get('intel_account') ?: $config->get('account');
|
||||
$params['password'] = $config->get('intel_password') ?: $config->get('password');
|
||||
}
|
||||
|
||||
$result = $this->postJson($this->buildEndpoint($config, $IDDCode), $params);
|
||||
|
||||
if (!isset($result['code']) || '0' != $result['code']) {
|
||||
throw new GatewayErrorException(json_encode($result, JSON_UNESCAPED_UNICODE), isset($result['code']) ? $result['code'] : 0, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @param int $IDDCode
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function buildEndpoint(Config $config, $IDDCode = 86)
|
||||
{
|
||||
$channel = $this->getChannel($config, $IDDCode);
|
||||
|
||||
if (self::INT_URL === $channel) {
|
||||
return $channel;
|
||||
}
|
||||
|
||||
return sprintf(self::ENDPOINT_URL_TEMPLATE, $channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @param int $IDDCode
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function getChannel(Config $config, $IDDCode)
|
||||
{
|
||||
if (86 != $IDDCode) {
|
||||
return self::INT_URL;
|
||||
}
|
||||
$channel = $config->get('channel', self::CHANNEL_VALIDATE_CODE);
|
||||
|
||||
if (!in_array($channel, [self::CHANNEL_VALIDATE_CODE, self::CHANNEL_PROMOTION_CODE])) {
|
||||
throw new InvalidArgumentException('Invalid channel for ChuanglanGateway.');
|
||||
}
|
||||
|
||||
return $channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param Config $config
|
||||
* @param int $IDDCode
|
||||
*
|
||||
* @return string|string
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function wrapChannelContent($content, Config $config, $IDDCode)
|
||||
{
|
||||
$channel = $this->getChannel($config, $IDDCode);
|
||||
|
||||
if (self::CHANNEL_PROMOTION_CODE == $channel) {
|
||||
$sign = (string) $config->get('sign', '');
|
||||
if (empty($sign)) {
|
||||
throw new InvalidArgumentException('Invalid sign for ChuanglanGateway when using promotion channel');
|
||||
}
|
||||
|
||||
$unsubscribe = (string) $config->get('unsubscribe', '');
|
||||
if (empty($unsubscribe)) {
|
||||
throw new InvalidArgumentException('Invalid unsubscribe for ChuanglanGateway when using promotion channel');
|
||||
}
|
||||
|
||||
$content = $sign.$content.$unsubscribe;
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
50
vendor/overtrue/easy-sms/src/Gateways/ErrorlogGateway.php
vendored
Executable file
50
vendor/overtrue/easy-sms/src/Gateways/ErrorlogGateway.php
vendored
Executable file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
|
||||
/**
|
||||
* Class ErrorlogGateway.
|
||||
*/
|
||||
class ErrorlogGateway extends Gateway
|
||||
{
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
if (is_array($to)) {
|
||||
$to = implode(',', $to);
|
||||
}
|
||||
|
||||
$message = sprintf(
|
||||
"[%s] to: %s | message: \"%s\" | template: \"%s\" | data: %s\n",
|
||||
date('Y-m-d H:i:s'),
|
||||
$to,
|
||||
$message->getContent($this),
|
||||
$message->getTemplate($this),
|
||||
json_encode($message->getData($this))
|
||||
);
|
||||
|
||||
$file = $this->config->get('file', ini_get('error_log'));
|
||||
$status = error_log($message, 3, $file);
|
||||
|
||||
return compact('status', 'file');
|
||||
}
|
||||
}
|
||||
120
vendor/overtrue/easy-sms/src/Gateways/Gateway.php
vendored
Executable file
120
vendor/overtrue/easy-sms/src/Gateways/Gateway.php
vendored
Executable file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\GatewayInterface;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
|
||||
/**
|
||||
* Class Gateway.
|
||||
*/
|
||||
abstract class Gateway implements GatewayInterface
|
||||
{
|
||||
const DEFAULT_TIMEOUT = 5.0;
|
||||
|
||||
/**
|
||||
* @var \Overtrue\EasySms\Support\Config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
protected $timeout;
|
||||
|
||||
/**
|
||||
* Gateway constructor.
|
||||
*
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = new Config($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return timeout.
|
||||
*
|
||||
* @return int|mixed
|
||||
*/
|
||||
public function getTimeout()
|
||||
{
|
||||
return $this->timeout ?: $this->config->get('timeout', self::DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set timeout.
|
||||
*
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTimeout($timeout)
|
||||
{
|
||||
$this->timeout = floatval($timeout);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Overtrue\EasySms\Support\Config
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setConfig(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $options
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setGuzzleOptions($options)
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getGuzzleOptions()
|
||||
{
|
||||
return $this->options ?: $this->config->get('options', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return \strtolower(str_replace([__NAMESPACE__.'\\', 'Gateway'], '', \get_class($this)));
|
||||
}
|
||||
}
|
||||
148
vendor/overtrue/easy-sms/src/Gateways/HuaweiGateway.php
vendored
Executable file
148
vendor/overtrue/easy-sms/src/Gateways/HuaweiGateway.php
vendored
Executable file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
class HuaweiGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_HOST = 'https://api.rtc.huaweicloud.com:10443';
|
||||
|
||||
const ENDPOINT_URI = '/sms/batchSendSms/v1';
|
||||
|
||||
const SUCCESS_CODE = '000000';
|
||||
|
||||
/**
|
||||
* 发送信息.
|
||||
*
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws GatewayErrorException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$appKey = $config->get('app_key');
|
||||
$appSecret = $config->get('app_secret');
|
||||
$channels = $config->get('from');
|
||||
$statusCallback = $config->get('callback', '');
|
||||
|
||||
$endpoint = $this->getEndpoint($config);
|
||||
$headers = $this->getHeaders($appKey, $appSecret);
|
||||
|
||||
$templateId = $message->getTemplate($this);
|
||||
$messageData = $message->getData($this);
|
||||
|
||||
// 短信签名通道号码
|
||||
$from = 'default';
|
||||
if (isset($messageData['from'])) {
|
||||
$from = $messageData['from'];
|
||||
unset($messageData['from']);
|
||||
}
|
||||
$channel = isset($channels[$from]) ? $channels[$from] : '';
|
||||
|
||||
if (empty($channel)) {
|
||||
throw new InvalidArgumentException("From Channel [{$from}] Not Exist");
|
||||
}
|
||||
|
||||
$params = [
|
||||
'from' => $channel,
|
||||
'to' => $to->getUniversalNumber(),
|
||||
'templateId' => $templateId,
|
||||
'templateParas' => json_encode($messageData),
|
||||
'statusCallback' => $statusCallback,
|
||||
];
|
||||
|
||||
try {
|
||||
$result = $this->request('post', $endpoint, [
|
||||
'headers' => $headers,
|
||||
'form_params' => $params,
|
||||
//为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
|
||||
'verify' => false,
|
||||
]);
|
||||
} catch (RequestException $e) {
|
||||
$result = $this->unwrapResponse($e->getResponse());
|
||||
}
|
||||
|
||||
if (self::SUCCESS_CODE != $result['code']) {
|
||||
throw new GatewayErrorException($result['description'], ltrim($result['code'], 'E'), $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造 Endpoint.
|
||||
*
|
||||
* @param Config $config
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getEndpoint(Config $config)
|
||||
{
|
||||
$endpoint = rtrim($config->get('endpoint', self::ENDPOINT_HOST), '/');
|
||||
|
||||
return $endpoint.self::ENDPOINT_URI;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求 Headers 参数.
|
||||
*
|
||||
* @param string $appKey
|
||||
* @param string $appSecret
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getHeaders($appKey, $appSecret)
|
||||
{
|
||||
return [
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'Authorization' => 'WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
|
||||
'X-WSSE' => $this->buildWsseHeader($appKey, $appSecret),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造X-WSSE参数值
|
||||
*
|
||||
* @param string $appKey
|
||||
* @param string $appSecret
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildWsseHeader($appKey, $appSecret)
|
||||
{
|
||||
$now = date('Y-m-d\TH:i:s\Z');
|
||||
$nonce = uniqid();
|
||||
$passwordDigest = base64_encode(hash('sha256', ($nonce.$now.$appSecret)));
|
||||
|
||||
return sprintf(
|
||||
'UsernameToken Username="%s",PasswordDigest="%s",Nonce="%s",Created="%s"',
|
||||
$appKey,
|
||||
$passwordDigest,
|
||||
$nonce,
|
||||
$now
|
||||
);
|
||||
}
|
||||
}
|
||||
73
vendor/overtrue/easy-sms/src/Gateways/HuaxinGateway.php
vendored
Executable file
73
vendor/overtrue/easy-sms/src/Gateways/HuaxinGateway.php
vendored
Executable file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class HuaxinGateway.
|
||||
*
|
||||
* @see http://www.ipyy.com/help/
|
||||
*/
|
||||
class HuaxinGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'http://%s/smsJson.aspx';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$endpoint = $this->buildEndpoint($config->get('ip'));
|
||||
|
||||
$result = $this->post($endpoint, [
|
||||
'userid' => $config->get('user_id'),
|
||||
'account' => $config->get('account'),
|
||||
'password' => $config->get('password'),
|
||||
'mobile' => $to->getNumber(),
|
||||
'content' => $message->getContent($this),
|
||||
'sendTime' => '',
|
||||
'action' => 'send',
|
||||
'extno' => $config->get('ext_no'),
|
||||
]);
|
||||
|
||||
if ('Success' !== $result['returnstatus']) {
|
||||
throw new GatewayErrorException($result['message'], 400, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param string $ip
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($ip)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $ip);
|
||||
}
|
||||
}
|
||||
77
vendor/overtrue/easy-sms/src/Gateways/HuyiGateway.php
vendored
Executable file
77
vendor/overtrue/easy-sms/src/Gateways/HuyiGateway.php
vendored
Executable file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class HuyiGateway.
|
||||
*
|
||||
* @see http://www.ihuyi.com/api/sms.html
|
||||
*/
|
||||
class HuyiGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'http://106.ihuyi.com/webservice/sms.php?method=Submit';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
const SUCCESS_CODE = 2;
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$params = [
|
||||
'account' => $config->get('api_id'),
|
||||
'mobile' => $to->getIDDCode() ? \sprintf('%s %s', $to->getIDDCode(), $to->getNumber()) : $to->getNumber(),
|
||||
'content' => $message->getContent($this),
|
||||
'time' => time(),
|
||||
'format' => self::ENDPOINT_FORMAT,
|
||||
'sign' => $config->get('signature'),
|
||||
];
|
||||
|
||||
$params['password'] = $this->generateSign($params);
|
||||
|
||||
$result = $this->post(self::ENDPOINT_URL, $params);
|
||||
|
||||
if (self::SUCCESS_CODE != $result['code']) {
|
||||
throw new GatewayErrorException($result['msg'], $result['code'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Sign.
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSign($params)
|
||||
{
|
||||
return md5($params['account'].$this->config->get('api_key').$params['mobile'].$params['content'].$params['time']);
|
||||
}
|
||||
}
|
||||
76
vendor/overtrue/easy-sms/src/Gateways/JuheGateway.php
vendored
Executable file
76
vendor/overtrue/easy-sms/src/Gateways/JuheGateway.php
vendored
Executable file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class JuheGateway.
|
||||
*
|
||||
* @see https://www.juhe.cn/docs/api/id/54
|
||||
*/
|
||||
class JuheGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'http://v.juhe.cn/sms/send';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$params = [
|
||||
'mobile' => $to->getNumber(),
|
||||
'tpl_id' => $message->getTemplate($this),
|
||||
'tpl_value' => $this->formatTemplateVars($message->getData($this)),
|
||||
'dtype' => self::ENDPOINT_FORMAT,
|
||||
'key' => $config->get('app_key'),
|
||||
];
|
||||
|
||||
$result = $this->get(self::ENDPOINT_URL, $params);
|
||||
|
||||
if ($result['error_code']) {
|
||||
throw new GatewayErrorException($result['reason'], $result['error_code'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $vars
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function formatTemplateVars(array $vars)
|
||||
{
|
||||
$formatted = [];
|
||||
|
||||
foreach ($vars as $key => $value) {
|
||||
$formatted[sprintf('#%s#', trim($key, '#'))] = $value;
|
||||
}
|
||||
|
||||
return http_build_query($formatted);
|
||||
}
|
||||
}
|
||||
61
vendor/overtrue/easy-sms/src/Gateways/KingttoGateway.php
vendored
Executable file
61
vendor/overtrue/easy-sms/src/Gateways/KingttoGateway.php
vendored
Executable file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class KingttoGateWay.
|
||||
*
|
||||
* @see http://www.kingtto.cn/
|
||||
*/
|
||||
class KingttoGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'http://101.201.41.194:9999/sms.aspx';
|
||||
|
||||
const ENDPOINT_METHOD = 'send';
|
||||
|
||||
/**
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface|array|string
|
||||
*
|
||||
* @throws GatewayErrorException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$params = [
|
||||
'action' => self::ENDPOINT_METHOD,
|
||||
'userid' => $config->get('userid'),
|
||||
'account' => $config->get('account'),
|
||||
'password' => $config->get('password'),
|
||||
'mobile' => $to->getNumber(),
|
||||
'content' => $message->getContent(),
|
||||
];
|
||||
|
||||
$result = $this->post(self::ENDPOINT_URL, $params);
|
||||
|
||||
if ('Success' != $result['returnstatus']) {
|
||||
throw new GatewayErrorException($result['message'], $result['remainpoint'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
74
vendor/overtrue/easy-sms/src/Gateways/LuosimaoGateway.php
vendored
Executable file
74
vendor/overtrue/easy-sms/src/Gateways/LuosimaoGateway.php
vendored
Executable file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class LuosimaoGateway.
|
||||
*
|
||||
* @see https://luosimao.com/docs/api/
|
||||
*/
|
||||
class LuosimaoGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'https://%s.luosimao.com/%s/%s.%s';
|
||||
|
||||
const ENDPOINT_VERSION = 'v1';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$endpoint = $this->buildEndpoint('sms-api', 'send');
|
||||
|
||||
$result = $this->post($endpoint, [
|
||||
'mobile' => $to->getNumber(),
|
||||
'message' => $message->getContent($this),
|
||||
], [
|
||||
'Authorization' => 'Basic '.base64_encode('api:key-'.$config->get('api_key')),
|
||||
]);
|
||||
|
||||
if ($result['error']) {
|
||||
throw new GatewayErrorException($result['msg'], $result['error'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($type, $function)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $type, self::ENDPOINT_VERSION, $function, self::ENDPOINT_FORMAT);
|
||||
}
|
||||
}
|
||||
106
vendor/overtrue/easy-sms/src/Gateways/ModuyunGateway.php
vendored
Executable file
106
vendor/overtrue/easy-sms/src/Gateways/ModuyunGateway.php
vendored
Executable file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class ModuyunGateway.
|
||||
*
|
||||
* @see https://www.moduyun.com/doc/index.html#10002
|
||||
*/
|
||||
class ModuyunGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'https://live.moduyun.com/sms/v2/sendsinglesms';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$urlParams = [
|
||||
'accesskey' => $config->get('accesskey'),
|
||||
'random' => rand(100000, 999999),
|
||||
];
|
||||
|
||||
$params = [
|
||||
'tel' => [
|
||||
'mobile' => $to->getNumber(),
|
||||
'nationcode' => $to->getIDDCode() ?: '86',
|
||||
],
|
||||
'signId' => $config->get('signId', ''),
|
||||
'templateId' => $message->getTemplate($this),
|
||||
'time' => time(),
|
||||
'type' => $config->get('type', 0),
|
||||
'params' => array_values($message->getData($this)),
|
||||
'ext' => '',
|
||||
'extend' => '',
|
||||
];
|
||||
$params['sig'] = $this->generateSign($params, $urlParams['random']);
|
||||
|
||||
$result = $this->request('post', $this->getEndpointUrl($urlParams), [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json'
|
||||
],
|
||||
'json' => $params,
|
||||
]);
|
||||
|
||||
$result = is_string($result) ? json_decode($result, true) : $result;
|
||||
if (0 != $result['result']) {
|
||||
throw new GatewayErrorException($result['errmsg'], $result['result'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getEndpointUrl($params)
|
||||
{
|
||||
return self::ENDPOINT_URL . '?' . http_build_query($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Sign.
|
||||
*
|
||||
* @param array $params
|
||||
* @param string $random
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSign($params, $random)
|
||||
{
|
||||
return hash('sha256', sprintf(
|
||||
'secretkey=%s&random=%d&time=%d&mobile=%s',
|
||||
$this->config->get('secretkey'),
|
||||
$random,
|
||||
$params['time'],
|
||||
$params['tel']['mobile']
|
||||
));
|
||||
}
|
||||
}
|
||||
115
vendor/overtrue/easy-sms/src/Gateways/QcloudGateway.php
vendored
Executable file
115
vendor/overtrue/easy-sms/src/Gateways/QcloudGateway.php
vendored
Executable file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class QcloudGateway.
|
||||
*
|
||||
* @see https://cloud.tencent.com/document/product/382/13297
|
||||
*/
|
||||
class QcloudGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'https://yun.tim.qq.com/v5/';
|
||||
|
||||
const ENDPOINT_METHOD = 'tlssmssvr/sendsms';
|
||||
|
||||
const ENDPOINT_VERSION = 'v5';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getData($this);
|
||||
|
||||
$signName = !empty($data['sign_name']) ? $data['sign_name'] : $config->get('sign_name', '');
|
||||
|
||||
unset($data['sign_name']);
|
||||
|
||||
$msg = $message->getContent($this);
|
||||
if (!empty($msg) && '【' != mb_substr($msg, 0, 1) && !empty($signName)) {
|
||||
$msg = '【'.$signName.'】'.$msg;
|
||||
}
|
||||
|
||||
$type = !empty($data['type']) ? $data['type'] : 0;
|
||||
$params = [
|
||||
'tel' => [
|
||||
'nationcode' => $to->getIDDCode() ?: 86,
|
||||
'mobile' => $to->getNumber(),
|
||||
],
|
||||
'type' => $type,
|
||||
'msg' => $msg,
|
||||
'time' => time(),
|
||||
'extend' => '',
|
||||
'ext' => '',
|
||||
];
|
||||
if (!is_null($message->getTemplate($this)) && is_array($data)) {
|
||||
unset($params['msg']);
|
||||
$params['params'] = array_values($data);
|
||||
$params['tpl_id'] = $message->getTemplate($this);
|
||||
$params['sign'] = $signName;
|
||||
}
|
||||
$random = substr(uniqid(), -10);
|
||||
|
||||
$params['sig'] = $this->generateSign($params, $random);
|
||||
|
||||
$url = self::ENDPOINT_URL.self::ENDPOINT_METHOD.'?sdkappid='.$config->get('sdk_app_id').'&random='.$random;
|
||||
|
||||
$result = $this->request('post', $url, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
'json' => $params,
|
||||
]);
|
||||
|
||||
if (0 != $result['result']) {
|
||||
throw new GatewayErrorException($result['errmsg'], $result['result'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Sign.
|
||||
*
|
||||
* @param array $params
|
||||
* @param string $random
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSign($params, $random)
|
||||
{
|
||||
ksort($params);
|
||||
|
||||
return hash('sha256', sprintf(
|
||||
'appkey=%s&random=%s&time=%s&mobile=%s',
|
||||
$this->config->get('app_key'),
|
||||
$random,
|
||||
$params['time'],
|
||||
$params['tel']['mobile']
|
||||
), false);
|
||||
}
|
||||
}
|
||||
148
vendor/overtrue/easy-sms/src/Gateways/QiniuGateway.php
vendored
Executable file
148
vendor/overtrue/easy-sms/src/Gateways/QiniuGateway.php
vendored
Executable file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class QiniuGateway.
|
||||
*
|
||||
* @see https://developer.qiniu.com/sms/api/5897/sms-api-send-message
|
||||
*/
|
||||
class QiniuGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'https://%s.qiniuapi.com/%s/%s';
|
||||
|
||||
const ENDPOINT_VERSION = 'v1';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$endpoint = $this->buildEndpoint('sms', 'message/single');
|
||||
|
||||
$data = $message->getData($this);
|
||||
|
||||
$params = [
|
||||
'template_id' => $message->getTemplate($this),
|
||||
'mobile' => $to->getNumber(),
|
||||
];
|
||||
|
||||
if (!empty($data)) {
|
||||
$params['parameters'] = $data;
|
||||
}
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'application/json',
|
||||
];
|
||||
|
||||
$headers['Authorization'] = $this->generateSign($endpoint, 'POST', json_encode($params), $headers['Content-Type'], $config);
|
||||
|
||||
$result = $this->postJson($endpoint, $params, $headers);
|
||||
|
||||
if (isset($result['error'])) {
|
||||
throw new GatewayErrorException($result['message'], $result['error'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($type, $function)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $type, self::ENDPOINT_VERSION, $function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param string $body
|
||||
* @param string $contentType
|
||||
* @param Config $config
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSign($url, $method, $body, $contentType, Config $config)
|
||||
{
|
||||
$urlItems = parse_url($url);
|
||||
$host = $urlItems['host'];
|
||||
if (isset($urlItems['port'])) {
|
||||
$port = $urlItems['port'];
|
||||
} else {
|
||||
$port = '';
|
||||
}
|
||||
$path = $urlItems['path'];
|
||||
if (isset($urlItems['query'])) {
|
||||
$query = $urlItems['query'];
|
||||
} else {
|
||||
$query = '';
|
||||
}
|
||||
//write request uri
|
||||
$toSignStr = $method.' '.$path;
|
||||
if (!empty($query)) {
|
||||
$toSignStr .= '?'.$query;
|
||||
}
|
||||
//write host and port
|
||||
$toSignStr .= "\nHost: ".$host;
|
||||
if (!empty($port)) {
|
||||
$toSignStr .= ':'.$port;
|
||||
}
|
||||
//write content type
|
||||
if (!empty($contentType)) {
|
||||
$toSignStr .= "\nContent-Type: ".$contentType;
|
||||
}
|
||||
$toSignStr .= "\n\n";
|
||||
//write body
|
||||
if (!empty($body)) {
|
||||
$toSignStr .= $body;
|
||||
}
|
||||
|
||||
$hmac = hash_hmac('sha1', $toSignStr, $config->get('secret_key'), true);
|
||||
|
||||
return 'Qiniu '.$config->get('access_key').':'.$this->base64UrlSafeEncode($hmac);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function base64UrlSafeEncode($data)
|
||||
{
|
||||
$find = array('+', '/');
|
||||
$replace = array('-', '_');
|
||||
|
||||
return str_replace($find, $replace, base64_encode($data));
|
||||
}
|
||||
}
|
||||
134
vendor/overtrue/easy-sms/src/Gateways/RongcloudGateway.php
vendored
Executable file
134
vendor/overtrue/easy-sms/src/Gateways/RongcloudGateway.php
vendored
Executable file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class RongcloudGateway.
|
||||
*
|
||||
* @author Darren Gao <realgaodacheng@gmail.com>
|
||||
*
|
||||
* @see http://www.rongcloud.cn/docs/sms_service.html#send_sms_code
|
||||
*/
|
||||
class RongcloudGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'http://api.sms.ronghub.com/%s.%s';
|
||||
|
||||
const ENDPOINT_ACTION = 'sendCode';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
const ENDPOINT_REGION = '86'; // 中国区,目前只支持此国别
|
||||
|
||||
const SUCCESS_CODE = 200;
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getData();
|
||||
$action = array_key_exists('action', $data) ? $data['action'] : self::ENDPOINT_ACTION;
|
||||
$endpoint = $this->buildEndpoint($action);
|
||||
|
||||
$headers = [
|
||||
'Nonce' => uniqid(),
|
||||
'App-Key' => $config->get('app_key'),
|
||||
'Timestamp' => time(),
|
||||
];
|
||||
$headers['Signature'] = $this->generateSign($headers, $config);
|
||||
|
||||
switch ($action) {
|
||||
case 'sendCode':
|
||||
$params = [
|
||||
'mobile' => $to->getNumber(),
|
||||
'region' => self::ENDPOINT_REGION,
|
||||
'templateId' => $message->getTemplate($this),
|
||||
];
|
||||
|
||||
break;
|
||||
case 'verifyCode':
|
||||
if (!array_key_exists('code', $data)
|
||||
or !array_key_exists('sessionId', $data)) {
|
||||
throw new GatewayErrorException('"code" or "sessionId" is not set', 0);
|
||||
}
|
||||
$params = [
|
||||
'code' => $data['code'],
|
||||
'sessionId' => $data['sessionId'],
|
||||
];
|
||||
|
||||
break;
|
||||
case 'sendNotify':
|
||||
$params = [
|
||||
'mobile' => $to->getNumber(),
|
||||
'region' => self::ENDPOINT_REGION,
|
||||
'templateId' => $message->getTemplate($this),
|
||||
];
|
||||
$params = array_merge($params, $data);
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new GatewayErrorException(sprintf('action: %s not supported', $action));
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->post($endpoint, $params, $headers);
|
||||
|
||||
if (self::SUCCESS_CODE !== $result['code']) {
|
||||
throw new GatewayErrorException($result['errorMessage'], $result['code'], $result);
|
||||
}
|
||||
} catch (ClientException $e) {
|
||||
throw new GatewayErrorException($e->getMessage(), $e->getCode());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Sign.
|
||||
*
|
||||
* @param array $params
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSign($params, Config $config)
|
||||
{
|
||||
return sha1(sprintf('%s%s%s', $config->get('app_secret'), $params['Nonce'], $params['Timestamp']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param string $action
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($action)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $action, self::ENDPOINT_FORMAT);
|
||||
}
|
||||
}
|
||||
95
vendor/overtrue/easy-sms/src/Gateways/SendcloudGateway.php
vendored
Executable file
95
vendor/overtrue/easy-sms/src/Gateways/SendcloudGateway.php
vendored
Executable file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class SendcloudGateway.
|
||||
*
|
||||
* @see http://sendcloud.sohu.com/doc/sms/
|
||||
*/
|
||||
class SendcloudGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'http://www.sendcloud.net/smsapi/%s';
|
||||
|
||||
/**
|
||||
* Send a short message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$params = [
|
||||
'smsUser' => $config->get('sms_user'),
|
||||
'templateId' => $message->getTemplate($this),
|
||||
'msgType' => $to->getIDDCode() ? 2 : 0,
|
||||
'phone' => $to->getZeroPrefixedNumber(),
|
||||
'vars' => $this->formatTemplateVars($message->getData($this)),
|
||||
];
|
||||
|
||||
if ($config->get('timestamp', false)) {
|
||||
$params['timestamp'] = time() * 1000;
|
||||
}
|
||||
|
||||
$params['signature'] = $this->sign($params, $config->get('sms_key'));
|
||||
|
||||
$result = $this->post(sprintf(self::ENDPOINT_TEMPLATE, 'send'), $params);
|
||||
|
||||
if (!$result['result']) {
|
||||
throw new GatewayErrorException($result['message'], $result['statusCode'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $vars
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function formatTemplateVars(array $vars)
|
||||
{
|
||||
$formatted = [];
|
||||
|
||||
foreach ($vars as $key => $value) {
|
||||
$formatted[sprintf('%%%s%%', trim($key, '%'))] = $value;
|
||||
}
|
||||
|
||||
return json_encode($formatted, JSON_FORCE_OBJECT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @param string $key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function sign($params, $key)
|
||||
{
|
||||
ksort($params);
|
||||
|
||||
return md5(sprintf('%s&%s&%s', $key, urldecode(http_build_query($params)), $key));
|
||||
}
|
||||
}
|
||||
78
vendor/overtrue/easy-sms/src/Gateways/SmsbaoGateway.php
vendored
Executable file
78
vendor/overtrue/easy-sms/src/Gateways/SmsbaoGateway.php
vendored
Executable file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class SmsbaoGateway
|
||||
* @author iwindy <203962638@qq.com>
|
||||
* @see http://www.smsbao.com/openapi/
|
||||
*/
|
||||
class SmsbaoGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'http://api.smsbao.com/%s';
|
||||
|
||||
const SUCCESS_CODE = '0';
|
||||
|
||||
protected $errorStatuses = [
|
||||
'0' => '短信发送成功',
|
||||
'-1' => '参数不全',
|
||||
'-2' => '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!',
|
||||
'30' => '密码错误',
|
||||
'40' => '账号不存在',
|
||||
'41' => '余额不足',
|
||||
'42' => '帐户已过期',
|
||||
'43' => 'IP地址限制',
|
||||
'50' => '内容含有敏感词'
|
||||
];
|
||||
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getContent($this);
|
||||
|
||||
if (is_null($to->getIDDCode())) {
|
||||
$number = $to->getNumber();
|
||||
$action = 'sms';
|
||||
} else {
|
||||
$number = $to->getUniversalNumber();
|
||||
$action = 'wsms';
|
||||
}
|
||||
|
||||
$params = [
|
||||
'u' => $config->get('user'),
|
||||
'p' => md5($config->get('password')),
|
||||
'm' => $number,
|
||||
'c' => $data
|
||||
];
|
||||
|
||||
$result = $this->get($this->buildEndpoint($action), $params);
|
||||
|
||||
if ($result !== self::SUCCESS_CODE) {
|
||||
throw new GatewayErrorException($this->errorStatuses[$result], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function buildEndpoint($type)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_URL, $type);
|
||||
}
|
||||
}
|
||||
88
vendor/overtrue/easy-sms/src/Gateways/SubmailGateway.php
vendored
Executable file
88
vendor/overtrue/easy-sms/src/Gateways/SubmailGateway.php
vendored
Executable file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class SubmailGateway.
|
||||
*
|
||||
* @see https://www.mysubmail.com/chs/documents/developer/index
|
||||
*/
|
||||
class SubmailGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'https://api.mysubmail.com/%s.%s';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$endpoint = $this->buildEndpoint($this->inChineseMainland($to) ? 'message/xsend' : 'internationalsms/xsend');
|
||||
|
||||
$data = $message->getData($this);
|
||||
|
||||
$result = $this->post($endpoint, [
|
||||
'appid' => $config->get('app_id'),
|
||||
'signature' => $config->get('app_key'),
|
||||
'project' => !empty($data['project']) ? $data['project'] : $config->get('project'),
|
||||
'to' => $to->getUniversalNumber(),
|
||||
'vars' => json_encode($data, JSON_FORCE_OBJECT),
|
||||
]);
|
||||
|
||||
if ('success' != $result['status']) {
|
||||
throw new GatewayErrorException($result['msg'], $result['code'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param string $function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($function)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $function, self::ENDPOINT_FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the phone number belongs to chinese mainland.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function inChineseMainland($to)
|
||||
{
|
||||
$code = $to->getIDDCode();
|
||||
|
||||
return empty($code) || 86 === $code;
|
||||
}
|
||||
}
|
||||
84
vendor/overtrue/easy-sms/src/Gateways/TianyiwuxianGateway.php
vendored
Executable file
84
vendor/overtrue/easy-sms/src/Gateways/TianyiwuxianGateway.php
vendored
Executable file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class TianyiwuxianGateway.
|
||||
*
|
||||
* @author Darren Gao <realgaodacheng@gmail.com>
|
||||
*/
|
||||
class TianyiwuxianGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'http://jk.106api.cn/sms%s.aspx';
|
||||
|
||||
const ENDPOINT_ENCODE = 'UTF8';
|
||||
|
||||
const ENDPOINT_TYPE = 'send';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
const SUCCESS_STATUS = 'success';
|
||||
|
||||
const SUCCESS_CODE = '0';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$endpoint = $this->buildEndpoint();
|
||||
|
||||
$params = [
|
||||
'gwid' => $config->get('gwid'),
|
||||
'type' => self::ENDPOINT_TYPE,
|
||||
'rece' => self::ENDPOINT_FORMAT,
|
||||
'mobile' => $to->getNumber(),
|
||||
'message' => $message->getContent($this),
|
||||
'username' => $config->get('username'),
|
||||
'password' => strtoupper(md5($config->get('password'))),
|
||||
];
|
||||
|
||||
$result = $this->post($endpoint, $params);
|
||||
|
||||
$result = json_decode($result, true);
|
||||
|
||||
if (self::SUCCESS_STATUS !== $result['returnstatus'] || self::SUCCESS_CODE !== $result['code']) {
|
||||
throw new GatewayErrorException($result['remark'], $result['code']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint()
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, self::ENDPOINT_ENCODE);
|
||||
}
|
||||
}
|
||||
86
vendor/overtrue/easy-sms/src/Gateways/TiniyoGateway.php
vendored
Executable file
86
vendor/overtrue/easy-sms/src/Gateways/TiniyoGateway.php
vendored
Executable file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class Tiniyo Gateway.
|
||||
*
|
||||
* @see https://tiniyo.com/sms.html
|
||||
*/
|
||||
class TiniyoGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'https://api.tiniyo.com/v1/Account/%s/Message';
|
||||
|
||||
const SUCCESS_CODE = '000000';
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'tiniyo';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$accountSid = $config->get('account_sid');
|
||||
$endpoint = $this->buildEndPoint($accountSid);
|
||||
|
||||
$params = [
|
||||
'dst' => $to->getUniversalNumber(),
|
||||
'src' => $config->get('from'),
|
||||
'text' => $message->getContent($this),
|
||||
];
|
||||
|
||||
$result = $this->request('post', $endpoint, [
|
||||
'json' => $params,
|
||||
'headers' => [
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json;charset=utf-8',
|
||||
'Authorization' => base64_encode($config->get('account_sid').':'.$config->get('token')),
|
||||
],
|
||||
]);
|
||||
|
||||
if (self::SUCCESS_CODE != $result['statusCode']) {
|
||||
throw new GatewayErrorException($result['statusCode'], $result['statusCode'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* build endpoint url.
|
||||
*
|
||||
* @param string $accountSid
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndPoint($accountSid)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_URL, $accountSid);
|
||||
}
|
||||
}
|
||||
91
vendor/overtrue/easy-sms/src/Gateways/TwilioGateway.php
vendored
Executable file
91
vendor/overtrue/easy-sms/src/Gateways/TwilioGateway.php
vendored
Executable file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class TwilioGateway.
|
||||
*
|
||||
* @see https://www.twilio.com/docs/api/messaging/send-messages
|
||||
*/
|
||||
class TwilioGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json';
|
||||
|
||||
protected $errorStatuses = [
|
||||
'failed',
|
||||
'undelivered',
|
||||
];
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'twilio';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$accountSid = $config->get('account_sid');
|
||||
$endpoint = $this->buildEndPoint($accountSid);
|
||||
|
||||
$params = [
|
||||
'To' => $to->getUniversalNumber(),
|
||||
'From' => $config->get('from'),
|
||||
'Body' => $message->getContent($this),
|
||||
];
|
||||
|
||||
try {
|
||||
$result = $this->request('post', $endpoint, [
|
||||
'auth' => [
|
||||
$accountSid,
|
||||
$config->get('token'),
|
||||
],
|
||||
'form_params' => $params,
|
||||
]);
|
||||
if (in_array($result['status'], $this->errorStatuses) || !is_null($result['error_code'])) {
|
||||
throw new GatewayErrorException($result['message'], $result['error_code'], $result);
|
||||
}
|
||||
} catch (ClientException $e) {
|
||||
throw new GatewayErrorException($e->getMessage(), $e->getCode());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* build endpoint url.
|
||||
*
|
||||
* @param string $accountSid
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndPoint($accountSid)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_URL, $accountSid);
|
||||
}
|
||||
}
|
||||
130
vendor/overtrue/easy-sms/src/Gateways/UcloudGateway.php
vendored
Executable file
130
vendor/overtrue/easy-sms/src/Gateways/UcloudGateway.php
vendored
Executable file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class UcloudGateway.
|
||||
*/
|
||||
class UcloudGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_URL = 'https://api.ucloud.cn';
|
||||
|
||||
const ENDPOINT_Action = 'SendUSMSMessage';
|
||||
|
||||
const SUCCESS_CODE = 0;
|
||||
|
||||
/**
|
||||
* Send Message.
|
||||
*
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws GatewayErrorException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$params = $this->buildParams($to, $message, $config);
|
||||
|
||||
$result = $this->get(self::ENDPOINT_URL, $params);
|
||||
|
||||
if (self::SUCCESS_CODE != $result['RetCode']) {
|
||||
throw new GatewayErrorException($result['Message'], $result['RetCode'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Params.
|
||||
*
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildParams(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getData($this);
|
||||
$params = [
|
||||
'Action' => self::ENDPOINT_Action,
|
||||
'SigContent' => $config->get('sig_content'),
|
||||
'TemplateId' => $message->getTemplate($this),
|
||||
'PublicKey' => $config->get('public_key'),
|
||||
];
|
||||
$code = isset($data['code']) ? $data['code'] : '';
|
||||
if (is_array($code) && !empty($code)) {
|
||||
foreach ($code as $key => $value) {
|
||||
$params['TemplateParams.'.$key] = $value;
|
||||
}
|
||||
} else {
|
||||
if (!empty($code) || !is_null($code)) {
|
||||
$params['TemplateParams.0'] = $code;
|
||||
}
|
||||
}
|
||||
|
||||
$mobiles = isset($data['mobiles']) ? $data['mobiles'] : '';
|
||||
if (!empty($mobiles) && !is_null($mobiles)) {
|
||||
if (is_array($mobiles)) {
|
||||
foreach ($mobiles as $key => $value) {
|
||||
$params['PhoneNumbers.'.$key] = $value;
|
||||
}
|
||||
} else {
|
||||
$params['PhoneNumbers.0'] = $mobiles;
|
||||
}
|
||||
} else {
|
||||
$params['PhoneNumbers.0'] = $to->getNumber();
|
||||
}
|
||||
|
||||
if (!is_null($config->get('project_id')) && !empty($config->get('project_id'))) {
|
||||
$params['ProjectId'] = $config->get('project_id');
|
||||
}
|
||||
|
||||
$signature = $this->getSignature($params, $config->get('private_key'));
|
||||
$params['Signature'] = $signature;
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Sign.
|
||||
*
|
||||
* @param array $params
|
||||
* @param string $privateKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getSignature($params, $privateKey)
|
||||
{
|
||||
ksort($params);
|
||||
|
||||
$paramsData = '';
|
||||
foreach ($params as $key => $value) {
|
||||
$paramsData .= $key;
|
||||
$paramsData .= $value;
|
||||
}
|
||||
$paramsData .= $privateKey;
|
||||
|
||||
return sha1($paramsData);
|
||||
}
|
||||
}
|
||||
77
vendor/overtrue/easy-sms/src/Gateways/Ue35Gateway.php
vendored
Executable file
77
vendor/overtrue/easy-sms/src/Gateways/Ue35Gateway.php
vendored
Executable file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class Ue35Gateway.
|
||||
*
|
||||
* @see https://shimo.im/docs/380b42d8cba24521
|
||||
*/
|
||||
class Ue35Gateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_HOST = 'sms.ue35.cn';
|
||||
|
||||
const ENDPOINT_URI = '/sms/interface/sendmess.htm';
|
||||
|
||||
const SUCCESS_CODE = 1;
|
||||
|
||||
/**
|
||||
* Send message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$params = [
|
||||
'username' => $config->get('username'),
|
||||
'userpwd' => $config->get('userpwd'),
|
||||
'mobiles' => $to->getNumber(),
|
||||
'content' => $message->getContent($this),
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'host' => static::ENDPOINT_HOST,
|
||||
'content-type' => 'application/json',
|
||||
'user-agent' => 'PHP EasySms Client',
|
||||
];
|
||||
|
||||
$result = $this->request('get', self::getEndpointUri().'?'.http_build_query($params), ['headers' => $headers]);
|
||||
if (is_string($result)) {
|
||||
$result = json_decode(json_encode(simplexml_load_string($result)), true);
|
||||
}
|
||||
|
||||
if (self::SUCCESS_CODE != $result['errorcode']) {
|
||||
throw new GatewayErrorException($result['message'], $result['errorcode'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getEndpointUri()
|
||||
{
|
||||
return 'http://'.static::ENDPOINT_HOST.static::ENDPOINT_URI;
|
||||
}
|
||||
}
|
||||
99
vendor/overtrue/easy-sms/src/Gateways/YunpianGateway.php
vendored
Executable file
99
vendor/overtrue/easy-sms/src/Gateways/YunpianGateway.php
vendored
Executable file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class YunpianGateway.
|
||||
*
|
||||
* @see https://www.yunpian.com/doc/zh_CN/intl/single_send.html
|
||||
*/
|
||||
class YunpianGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'https://%s.yunpian.com/%s/%s/%s.%s';
|
||||
|
||||
const ENDPOINT_VERSION = 'v2';
|
||||
|
||||
const ENDPOINT_FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$template = $message->getTemplate($this);
|
||||
$function = 'single_send';
|
||||
$option = [
|
||||
'form_params' => [
|
||||
'apikey' => $config->get('api_key'),
|
||||
'mobile' => $to->getUniversalNumber()
|
||||
],
|
||||
'exceptions' => false,
|
||||
];
|
||||
|
||||
if(!is_null($template)){
|
||||
$function = 'tpl_single_send';
|
||||
$data = [];
|
||||
|
||||
foreach ($message->getData($this) ?? [] as $key => $value) {
|
||||
$data[] = urlencode('#'.$key.'#') . '=' . urlencode($value);
|
||||
}
|
||||
|
||||
$option['form_params'] = array_merge($option['form_params'],[
|
||||
'tpl_id' => $template,
|
||||
'tpl_value' => implode('&', $data)
|
||||
]);
|
||||
}else{
|
||||
$content = $message->getContent($this);
|
||||
$signature = $config->get('signature', '');
|
||||
$option['form_params'] = array_merge($option['form_params'],[
|
||||
'text' => 0 === \stripos($content, '【') ? $content : $signature.$content
|
||||
]);
|
||||
}
|
||||
|
||||
$endpoint = $this->buildEndpoint('sms', 'sms', $function);
|
||||
$result = $this->request('post', $endpoint, $option);
|
||||
|
||||
if ($result['code']) {
|
||||
throw new GatewayErrorException($result['msg'], $result['code'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $resource
|
||||
* @param string $function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($type, $resource, $function)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $type, self::ENDPOINT_VERSION, $resource, $function, self::ENDPOINT_FORMAT);
|
||||
}
|
||||
}
|
||||
99
vendor/overtrue/easy-sms/src/Gateways/YuntongxunGateway.php
vendored
Executable file
99
vendor/overtrue/easy-sms/src/Gateways/YuntongxunGateway.php
vendored
Executable file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class YuntongxunGateway.
|
||||
*
|
||||
* @see http://www.yuntongxun.com/doc/rest/sms/3_2_2_2.html
|
||||
*/
|
||||
class YuntongxunGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'https://%s:%s/%s/%s/%s/%s/%s?sig=%s';
|
||||
|
||||
const SERVER_IP = 'app.cloopen.com';
|
||||
|
||||
const DEBUG_SERVER_IP = 'sandboxapp.cloopen.com';
|
||||
|
||||
const DEBUG_TEMPLATE_ID = 1;
|
||||
|
||||
const SERVER_PORT = '8883';
|
||||
|
||||
const SDK_VERSION = '2013-12-26';
|
||||
|
||||
const SUCCESS_CODE = '000000';
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$datetime = date('YmdHis');
|
||||
|
||||
$endpoint = $this->buildEndpoint('SMS', 'TemplateSMS', $datetime, $config);
|
||||
|
||||
$result = $this->request('post', $endpoint, [
|
||||
'json' => [
|
||||
'to' => $to,
|
||||
'templateId' => (int) ($this->config->get('debug') ? self::DEBUG_TEMPLATE_ID : $message->getTemplate($this)),
|
||||
'appId' => $config->get('app_id'),
|
||||
'datas' => $message->getData($this),
|
||||
],
|
||||
'headers' => [
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json;charset=utf-8',
|
||||
'Authorization' => base64_encode($config->get('account_sid').':'.$datetime),
|
||||
],
|
||||
]);
|
||||
|
||||
if (self::SUCCESS_CODE != $result['statusCode']) {
|
||||
throw new GatewayErrorException($result['statusCode'], $result['statusCode'], $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build endpoint url.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $resource
|
||||
* @param string $datetime
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($type, $resource, $datetime, Config $config)
|
||||
{
|
||||
$serverIp = $this->config->get('debug') ? self::DEBUG_SERVER_IP : self::SERVER_IP;
|
||||
|
||||
$accountType = $this->config->get('is_sub_account') ? 'SubAccounts' : 'Accounts';
|
||||
|
||||
$sig = strtoupper(md5($config->get('account_sid').$config->get('account_token').$datetime));
|
||||
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $serverIp, self::SERVER_PORT, self::SDK_VERSION, $accountType, $config->get('account_sid'), $type, $resource, $sig);
|
||||
}
|
||||
}
|
||||
162
vendor/overtrue/easy-sms/src/Gateways/YunxinGateway.php
vendored
Executable file
162
vendor/overtrue/easy-sms/src/Gateways/YunxinGateway.php
vendored
Executable file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class YunxinGateway.
|
||||
*
|
||||
* @author her-cat <i@her-cat.com>
|
||||
*
|
||||
* @see https://dev.yunxin.163.com/docs/product/%E7%9F%AD%E4%BF%A1/%E7%9F%AD%E4%BF%A1%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97
|
||||
*/
|
||||
class YunxinGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'https://api.netease.im/%s/%s.action';
|
||||
|
||||
const ENDPOINT_ACTION = 'sendCode';
|
||||
|
||||
const SUCCESS_CODE = 200;
|
||||
|
||||
/**
|
||||
* Send a short message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws GatewayErrorException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getData($this);
|
||||
|
||||
$action = isset($data['action']) ? $data['action'] : self::ENDPOINT_ACTION;
|
||||
|
||||
$endpoint = $this->buildEndpoint('sms', $action);
|
||||
|
||||
switch ($action) {
|
||||
case 'sendCode':
|
||||
$params = $this->buildSendCodeParams($to, $message, $config);
|
||||
|
||||
break;
|
||||
case 'verifyCode':
|
||||
$params = $this->buildVerifyCodeParams($to, $message);
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new GatewayErrorException(sprintf('action: %s not supported', $action), 0);
|
||||
}
|
||||
|
||||
$headers = $this->buildHeaders($config);
|
||||
|
||||
try {
|
||||
$result = $this->post($endpoint, $params, $headers);
|
||||
|
||||
if (!isset($result['code']) || self::SUCCESS_CODE !== $result['code']) {
|
||||
$code = isset($result['code']) ? $result['code'] : 0;
|
||||
$error = isset($result['msg']) ? $result['msg'] : json_encode($result, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
throw new GatewayErrorException($error, $code);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new GatewayErrorException($e->getMessage(), $e->getCode());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $resource
|
||||
* @param $function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($resource, $function)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $resource, strtolower($function));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the request headers.
|
||||
*
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildHeaders(Config $config)
|
||||
{
|
||||
$headers = [
|
||||
'AppKey' => $config->get('app_key'),
|
||||
'Nonce' => md5(uniqid('easysms')),
|
||||
'CurTime' => (string) time(),
|
||||
'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
|
||||
];
|
||||
|
||||
$headers['CheckSum'] = sha1("{$config->get('app_secret')}{$headers['Nonce']}{$headers['CurTime']}");
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function buildSendCodeParams(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getData($this);
|
||||
$template = $message->getTemplate($this);
|
||||
|
||||
return [
|
||||
'mobile' => $to->getUniversalNumber(),
|
||||
'authCode' => array_key_exists('code', $data) ? $data['code'] : '',
|
||||
'deviceId' => array_key_exists('device_id', $data) ? $data['device_id'] : '',
|
||||
'templateid' => is_string($template) ? $template : '',
|
||||
'codeLen' => $config->get('code_length', 4),
|
||||
'needUp' => $config->get('need_up', false),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws GatewayErrorException
|
||||
*/
|
||||
public function buildVerifyCodeParams(PhoneNumberInterface $to, MessageInterface $message)
|
||||
{
|
||||
$data = $message->getData($this);
|
||||
|
||||
if (!array_key_exists('code', $data)) {
|
||||
throw new GatewayErrorException('"code" cannot be empty', 0);
|
||||
}
|
||||
|
||||
return [
|
||||
'mobile' => $to->getUniversalNumber(),
|
||||
'code' => $data['code'],
|
||||
];
|
||||
}
|
||||
}
|
||||
121
vendor/overtrue/easy-sms/src/Gateways/YunzhixunGateway.php
vendored
Executable file
121
vendor/overtrue/easy-sms/src/Gateways/YunzhixunGateway.php
vendored
Executable file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Gateways;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
||||
use Overtrue\EasySms\Support\Config;
|
||||
use Overtrue\EasySms\Traits\HasHttpRequest;
|
||||
|
||||
/**
|
||||
* Class YunzhixunGateway.
|
||||
*
|
||||
* @author her-cat <i@her-cat.com>
|
||||
*
|
||||
* @see http://docs.ucpaas.com/doku.php?id=%E7%9F%AD%E4%BF%A1:sendsms
|
||||
*/
|
||||
class YunzhixunGateway extends Gateway
|
||||
{
|
||||
use HasHttpRequest;
|
||||
|
||||
const SUCCESS_CODE = '000000';
|
||||
|
||||
const FUNCTION_SEND_SMS = 'sendsms';
|
||||
|
||||
const FUNCTION_BATCH_SEND_SMS = 'sendsms_batch';
|
||||
|
||||
const ENDPOINT_TEMPLATE = 'https://open.ucpaas.com/ol/%s/%s';
|
||||
|
||||
/**
|
||||
* Send a short message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param \Overtrue\EasySms\Support\Config $config
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws GatewayErrorException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getData($this);
|
||||
|
||||
$function = isset($data['mobiles']) ? self::FUNCTION_BATCH_SEND_SMS : self::FUNCTION_SEND_SMS;
|
||||
|
||||
$endpoint = $this->buildEndpoint('sms', $function);
|
||||
|
||||
$params = $this->buildParams($to, $message, $config);
|
||||
|
||||
return $this->execute($endpoint, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $resource
|
||||
* @param $function
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function buildEndpoint($resource, $function)
|
||||
{
|
||||
return sprintf(self::ENDPOINT_TEMPLATE, $resource, $function);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PhoneNumberInterface $to
|
||||
* @param MessageInterface $message
|
||||
* @param Config $config
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildParams(PhoneNumberInterface $to, MessageInterface $message, Config $config)
|
||||
{
|
||||
$data = $message->getData($this);
|
||||
|
||||
return [
|
||||
'sid' => $config->get('sid'),
|
||||
'token' => $config->get('token'),
|
||||
'appid' => $config->get('app_id'),
|
||||
'templateid' => $message->getTemplate($this),
|
||||
'uid' => isset($data['uid']) ? $data['uid'] : '',
|
||||
'param' => isset($data['params']) ? $data['params'] : '',
|
||||
'mobile' => isset($data['mobiles']) ? $data['mobiles'] : $to->getNumber(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $endpoint
|
||||
* @param $params
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws GatewayErrorException
|
||||
*/
|
||||
protected function execute($endpoint, $params)
|
||||
{
|
||||
try {
|
||||
$result = $this->postJson($endpoint, $params);
|
||||
|
||||
if (!isset($result['code']) || self::SUCCESS_CODE !== $result['code']) {
|
||||
$code = isset($result['code']) ? $result['code'] : 0;
|
||||
$error = isset($result['msg']) ? $result['msg'] : json_encode($result, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
throw new GatewayErrorException($error, $code);
|
||||
}
|
||||
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
throw new GatewayErrorException($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
187
vendor/overtrue/easy-sms/src/Message.php
vendored
Executable file
187
vendor/overtrue/easy-sms/src/Message.php
vendored
Executable file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms;
|
||||
|
||||
use Overtrue\EasySms\Contracts\GatewayInterface;
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
|
||||
/**
|
||||
* Class Message.
|
||||
*/
|
||||
class Message implements MessageInterface
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $gateways = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $content;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $template;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* Message constructor.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @param string $type
|
||||
*/
|
||||
public function __construct(array $attributes = [], $type = MessageInterface::TEXT_MESSAGE)
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
foreach ($attributes as $property => $value) {
|
||||
if (property_exists($this, $property)) {
|
||||
$this->$property = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the message type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return message content.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContent(GatewayInterface $gateway = null)
|
||||
{
|
||||
return is_callable($this->content) ? call_user_func($this->content, $gateway) : $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the template id of message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTemplate(GatewayInterface $gateway = null)
|
||||
{
|
||||
return is_callable($this->template) ? call_user_func($this->template, $gateway) : $this->template;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setType($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $content
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setContent($content)
|
||||
{
|
||||
$this->content = $content;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $template
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template)
|
||||
{
|
||||
$this->template = $template;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData(GatewayInterface $gateway = null)
|
||||
{
|
||||
return is_callable($this->data) ? call_user_func($this->data, $gateway) : $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|callable $data
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setData($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getGateways()
|
||||
{
|
||||
return $this->gateways;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $gateways
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setGateways(array $gateways)
|
||||
{
|
||||
$this->gateways = $gateways;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $property
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __get($property)
|
||||
{
|
||||
if (property_exists($this, $property)) {
|
||||
return $this->$property;
|
||||
}
|
||||
}
|
||||
}
|
||||
89
vendor/overtrue/easy-sms/src/Messenger.php
vendored
Executable file
89
vendor/overtrue/easy-sms/src/Messenger.php
vendored
Executable file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms;
|
||||
|
||||
use Overtrue\EasySms\Contracts\MessageInterface;
|
||||
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
|
||||
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
|
||||
|
||||
/**
|
||||
* Class Messenger.
|
||||
*/
|
||||
class Messenger
|
||||
{
|
||||
const STATUS_SUCCESS = 'success';
|
||||
|
||||
const STATUS_FAILURE = 'failure';
|
||||
|
||||
/**
|
||||
* @var \Overtrue\EasySms\EasySms
|
||||
*/
|
||||
protected $easySms;
|
||||
|
||||
/**
|
||||
* Messenger constructor.
|
||||
*
|
||||
* @param \Overtrue\EasySms\EasySms $easySms
|
||||
*/
|
||||
public function __construct(EasySms $easySms)
|
||||
{
|
||||
$this->easySms = $easySms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
|
||||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
|
||||
* @param array $gateways
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
|
||||
*/
|
||||
public function send(PhoneNumberInterface $to, MessageInterface $message, array $gateways = [])
|
||||
{
|
||||
$results = [];
|
||||
$isSuccessful = false;
|
||||
|
||||
foreach ($gateways as $gateway => $config) {
|
||||
try {
|
||||
$results[$gateway] = [
|
||||
'gateway' => $gateway,
|
||||
'status' => self::STATUS_SUCCESS,
|
||||
'result' => $this->easySms->gateway($gateway)->send($to, $message, $config),
|
||||
];
|
||||
$isSuccessful = true;
|
||||
|
||||
break;
|
||||
} catch (\Exception $e) {
|
||||
$results[$gateway] = [
|
||||
'gateway' => $gateway,
|
||||
'status' => self::STATUS_FAILURE,
|
||||
'exception' => $e,
|
||||
];
|
||||
} catch (\Throwable $e) {
|
||||
$results[$gateway] = [
|
||||
'gateway' => $gateway,
|
||||
'status' => self::STATUS_FAILURE,
|
||||
'exception' => $e,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$isSuccessful) {
|
||||
throw new NoGatewayAvailableException($results);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
115
vendor/overtrue/easy-sms/src/PhoneNumber.php
vendored
Executable file
115
vendor/overtrue/easy-sms/src/PhoneNumber.php
vendored
Executable file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms;
|
||||
|
||||
/**
|
||||
* Class PhoneNumberInterface.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
class PhoneNumber implements \Overtrue\EasySms\Contracts\PhoneNumberInterface
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $number;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $IDDCode;
|
||||
|
||||
/**
|
||||
* PhoneNumberInterface constructor.
|
||||
*
|
||||
* @param int $numberWithoutIDDCode
|
||||
* @param string $IDDCode
|
||||
*/
|
||||
public function __construct($numberWithoutIDDCode, $IDDCode = null)
|
||||
{
|
||||
$this->number = $numberWithoutIDDCode;
|
||||
$this->IDDCode = $IDDCode ? intval(ltrim($IDDCode, '+0')) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 86.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getIDDCode()
|
||||
{
|
||||
return $this->IDDCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 18888888888.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getNumber()
|
||||
{
|
||||
return $this->number;
|
||||
}
|
||||
|
||||
/**
|
||||
* +8618888888888.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUniversalNumber()
|
||||
{
|
||||
return $this->getPrefixedIDDCode('+').$this->number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 008618888888888.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getZeroPrefixedNumber()
|
||||
{
|
||||
return $this->getPrefixedIDDCode('00').$this->number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prefix
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPrefixedIDDCode($prefix)
|
||||
{
|
||||
return $this->IDDCode ? $prefix.$this->IDDCode : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getUniversalNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON.
|
||||
*
|
||||
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
|
||||
*
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
* which is a value of any type other than a resource
|
||||
*
|
||||
* @since 5.4.0
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->getUniversalNumber();
|
||||
}
|
||||
}
|
||||
32
vendor/overtrue/easy-sms/src/Strategies/OrderStrategy.php
vendored
Executable file
32
vendor/overtrue/easy-sms/src/Strategies/OrderStrategy.php
vendored
Executable file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Strategies;
|
||||
|
||||
use Overtrue\EasySms\Contracts\StrategyInterface;
|
||||
|
||||
/**
|
||||
* Class OrderStrategy.
|
||||
*/
|
||||
class OrderStrategy implements StrategyInterface
|
||||
{
|
||||
/**
|
||||
* Apply the strategy and return result.
|
||||
*
|
||||
* @param array $gateways
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function apply(array $gateways)
|
||||
{
|
||||
return array_keys($gateways);
|
||||
}
|
||||
}
|
||||
34
vendor/overtrue/easy-sms/src/Strategies/RandomStrategy.php
vendored
Executable file
34
vendor/overtrue/easy-sms/src/Strategies/RandomStrategy.php
vendored
Executable file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Strategies;
|
||||
|
||||
use Overtrue\EasySms\Contracts\StrategyInterface;
|
||||
|
||||
/**
|
||||
* Class RandomStrategy.
|
||||
*/
|
||||
class RandomStrategy implements StrategyInterface
|
||||
{
|
||||
/**
|
||||
* @param array $gateways
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function apply(array $gateways)
|
||||
{
|
||||
uasort($gateways, function () {
|
||||
return mt_rand() - mt_rand();
|
||||
});
|
||||
|
||||
return array_keys($gateways);
|
||||
}
|
||||
}
|
||||
143
vendor/overtrue/easy-sms/src/Support/Config.php
vendored
Executable file
143
vendor/overtrue/easy-sms/src/Support/Config.php
vendored
Executable file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Support;
|
||||
|
||||
use ArrayAccess;
|
||||
|
||||
/**
|
||||
* Class Config.
|
||||
*/
|
||||
class Config implements ArrayAccess
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Config constructor.
|
||||
*
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from an array using "dot" notation.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$config = $this->config;
|
||||
|
||||
if (isset($config[$key])) {
|
||||
return $config[$key];
|
||||
}
|
||||
|
||||
if (false === strpos($key, '.')) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
if (!is_array($config) || !array_key_exists($segment, $config)) {
|
||||
return $default;
|
||||
}
|
||||
$config = $config[$segment];
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a offset exists.
|
||||
*
|
||||
* @see http://php.net/manual/en/arrayaccess.offsetexists.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* An offset to check for.
|
||||
* </p>
|
||||
*
|
||||
* @return bool true on success or false on failure.
|
||||
* </p>
|
||||
* <p>
|
||||
* The return value will be casted to boolean if non-boolean was returned
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return array_key_exists($offset, $this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to retrieve.
|
||||
*
|
||||
* @see http://php.net/manual/en/arrayaccess.offsetget.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* The offset to retrieve.
|
||||
* </p>
|
||||
*
|
||||
* @return mixed Can return all value types
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->get($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to set.
|
||||
*
|
||||
* @see http://php.net/manual/en/arrayaccess.offsetset.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* The offset to assign the value to.
|
||||
* </p>
|
||||
* @param mixed $value <p>
|
||||
* The value to set.
|
||||
* </p>
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (isset($this->config[$offset])) {
|
||||
$this->config[$offset] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to unset.
|
||||
*
|
||||
* @see http://php.net/manual/en/arrayaccess.offsetunset.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* The offset to unset.
|
||||
* </p>
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
if (isset($this->config[$offset])) {
|
||||
unset($this->config[$offset]);
|
||||
}
|
||||
}
|
||||
}
|
||||
136
vendor/overtrue/easy-sms/src/Traits/HasHttpRequest.php
vendored
Executable file
136
vendor/overtrue/easy-sms/src/Traits/HasHttpRequest.php
vendored
Executable file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/easy-sms.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\EasySms\Traits;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Trait HasHttpRequest.
|
||||
*/
|
||||
trait HasHttpRequest
|
||||
{
|
||||
/**
|
||||
* Make a get request.
|
||||
*
|
||||
* @param string $endpoint
|
||||
* @param array $query
|
||||
* @param array $headers
|
||||
*
|
||||
* @return ResponseInterface|array|string
|
||||
*/
|
||||
protected function get($endpoint, $query = [], $headers = [])
|
||||
{
|
||||
return $this->request('get', $endpoint, [
|
||||
'headers' => $headers,
|
||||
'query' => $query,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a post request.
|
||||
*
|
||||
* @param string $endpoint
|
||||
* @param array $params
|
||||
* @param array $headers
|
||||
*
|
||||
* @return ResponseInterface|array|string
|
||||
*/
|
||||
protected function post($endpoint, $params = [], $headers = [])
|
||||
{
|
||||
return $this->request('post', $endpoint, [
|
||||
'headers' => $headers,
|
||||
'form_params' => $params,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a post request with json params.
|
||||
*
|
||||
* @param $endpoint
|
||||
* @param array $params
|
||||
* @param array $headers
|
||||
*
|
||||
* @return ResponseInterface|array|string
|
||||
*/
|
||||
protected function postJson($endpoint, $params = [], $headers = [])
|
||||
{
|
||||
return $this->request('post', $endpoint, [
|
||||
'headers' => $headers,
|
||||
'json' => $params,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a http request.
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $endpoint
|
||||
* @param array $options http://docs.guzzlephp.org/en/latest/request-options.html
|
||||
*
|
||||
* @return ResponseInterface|array|string
|
||||
*/
|
||||
protected function request($method, $endpoint, $options = [])
|
||||
{
|
||||
return $this->unwrapResponse($this->getHttpClient($this->getBaseOptions())->{$method}($endpoint, $options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return base Guzzle options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getBaseOptions()
|
||||
{
|
||||
$options = method_exists($this, 'getGuzzleOptions') ? $this->getGuzzleOptions() : [];
|
||||
|
||||
return \array_merge($options, [
|
||||
'base_uri' => method_exists($this, 'getBaseUri') ? $this->getBaseUri() : '',
|
||||
'timeout' => method_exists($this, 'getTimeout') ? $this->getTimeout() : 5.0,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return http client.
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return \GuzzleHttp\Client
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
protected function getHttpClient(array $options = [])
|
||||
{
|
||||
return new Client($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert response contents to json.
|
||||
*
|
||||
* @param \Psr\Http\Message\ResponseInterface $response
|
||||
*
|
||||
* @return ResponseInterface|array|string
|
||||
*/
|
||||
protected function unwrapResponse(ResponseInterface $response)
|
||||
{
|
||||
$contentType = $response->getHeaderLine('Content-Type');
|
||||
$contents = $response->getBody()->getContents();
|
||||
|
||||
if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
|
||||
return json_decode($contents, true);
|
||||
} elseif (false !== stripos($contentType, 'xml')) {
|
||||
return json_decode(json_encode(simplexml_load_string($contents)), true);
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user