Git Product home page Git Product logo

easy-sms's Introduction

Easy SMS

📲 一款满足你的多种发送需求的短信发送组件

Build Status Latest Stable Version Latest Unstable Version Scrutinizer Code Quality Code Coverage Total Downloads License

特点

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

平台支持

环境需求

  • PHP >= 5.6

安装

$ composer require "overtrue/easy-sms"

使用

use Overtrue\EasySms\EasySms;

$config = [
    // HTTP 请求的超时时间(秒)
    'timeout' => 5.0,
    
    // 默认发送配置
    'default' => [
        // 网关调用策略,默认:顺序调用
        'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
        
        // 默认可用的发送网关
        'gateways' => [
            'yunpian', 'alidayu',
        ],
    ],
    // 可用的网关配置
    'gateways' => [
        'errorlog' => [
            'file' => '/tmp/easy-sms.log',
        ],
        'yunpian' => [
            'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
        ],
        'alidayu' => [
            //...
        ],
    ],
];

$easySms = new EasySms($config);

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

短信内容

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

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

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

发送网关

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

$easySms->send(13188888888, [
    'content'  => '您的验证码为: 6379', 
    'template' => 'SMS_001', 
    'data' => [ 
        'code' => 6379
    ],
 ], ['yunpian', 'juhe']); // 这里的网关配置将会覆盖全局默认值

返回值

由于使用多网关发送,所以返回值为一个数组,结构如下:

[
    'yunpian' => [
        'status' => 'success',
        'result' => [...] // 平台返回值
    ],
    'juhe' => [
        'status' => 'erred',
        'exception' => \Overtrue\EasySms\Exceptions\GatewayErrorException 对象
    ],
    //...
]

定义短信

你可以根本发送场景的不同,定义不同的短信类,从而实现一处定义多处调用,你可以继承 Overtrue\EasySms\Message 来定义短信模型:

<?php

use Overtrue\EasySms\Message;
use Overtrue\EasySms\Contracts\GatewayInterface;
use Overtrue\EasySms\Strategies\OrderStrategy;

class OrderPaidMessage extends Messeage
{
    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

发送自定义短信:

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

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

各平台配置说明

阿里大于

    'alidayu' => [
        'app_key' => '',
        'app_secret' => '',
        'sign_name' => '',
    ],

云片

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

Submail

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

螺丝帽

    'luosimao' => [
        'api_key' => '',
    ],

容联云通讯

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

互亿无线

    'huyi' => [
        'api_id' => '',
        'api_key' => '',
    ],

聚合数据

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

SendCloud

    'sendcloud' => [
        'sms_user' => '',
        'sms_key' => '',
    ],

百度云

    'baidu' => [
        'ak' => '',
        'sk' => '',
        'invoke_id' => '',
        'domain' => '',
    ],

License

MIT

easy-sms's People

Contributors

hanson avatar iwzh avatar jcc avatar leo108 avatar novalevel avatar overtrue avatar scrutinizer-auto-fixer avatar xule avatar zhenggg avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.