PHPackages                             hyperf-ext/sms - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. hyperf-ext/sms

ActiveLibrary

hyperf-ext/sms
==============

The Hyperf SMS package.

v2.2.0(4y ago)254.4k↓100%5[5 issues](https://github.com/hyperf-ext/sms/issues)[1 PRs](https://github.com/hyperf-ext/sms/pulls)MITPHPPHP &gt;=7.3

Since Oct 31Pushed 3y ago1 watchersCompare

[ Source](https://github.com/hyperf-ext/sms)[ Packagist](https://packagist.org/packages/hyperf-ext/sms)[ RSS](/packages/hyperf-ext-sms/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (16)Versions (8)Used By (0)

Hyperf 短信发送组件
=============

[](#hyperf-短信发送组件)

简介
--

[](#简介)

组件实现了一套干净、简洁的 API ，并为诸多第三方短信发送服务提供了相应的驱动，让你可以快速、简单地从您的应用发送短信。驱动基本移植自 [overtrue/easy-sms](https://github.com/overtrue/easy-sms) ，同时借鉴了其网关策略并进行了扩展。

安装
--

[](#安装)

```
composer require hyperf-ext/sms
```

配置
--

[](#配置)

### 发布配置

[](#发布配置)

```
php bin/hyperf.php vendor:publish hyperf-ext/sms
```

### 发送器配置

[](#发送器配置)

配置文件中的 `senders` 节点配置的每个发送器配置都有自己的「驱动」和配置选项，这将允许你的应用程序使用不同的短信服务来发送特定的消息。例如，你的应用程序可能使用阿里云发送验证码类消息，而使用腾讯云发送通知类消息。再或者为同类消息配置多个发送器来提高服务可用性。

#### [阿里云](https://www.aliyun.com/product/sms)

[](#阿里云)

短信消息类可用配置内容方法：`template`，`with`，`signature`

```
[
    'driver' => \HyperfExt\Sms\Drivers\AliyunDriver::class,
    'config' => [
        'access_key_id' => '',
        'access_key_secret' => '',
        'sign_name' => '',
    ],
]
```

#### [百度智能云](https://cloud.baidu.com/product/sms.html)

[](#百度智能云)

短信消息类可用配置内容方法：`template`，`with`

```
[
    'driver' => \HyperfExt\Sms\Drivers\BaiduCloudDriver::class,
    'config' => [
        'ak' => '',
        'sk' => '',
        'invoke_id' => '',
        'domain' => '',
    ],
]
```

#### [华为云](https://www.huaweicloud.com/product/msgsms.html)

[](#华为云)

短信消息类可用配置内容方法：`template`，`with`，`from`

```
[
    'driver' => \HyperfExt\Sms\Drivers\HuaweiCloudDriver::class,
    'config' => [
        'endpoint' => '', // 从管理控制台获取到的 App 接入地址
        'app_key' => '',
        'app_secret' => '',
        'from' => [
            'default' => '', // 默认签名通道号
            // 'another' => '', // 其他签名通道号
        ],
    ],
]
```

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

[](#聚合数据)

短信消息类可用配置内容方法：`template`，`with`

```
[
    'driver' => \HyperfExt\Sms\Drivers\JuheDataDriver::class,
    'config' => [
       'app_key' => '',
    ],
]
```

#### [螺丝帽](https://luosimao.com/service/sms)

[](#螺丝帽)

短信消息类可用配置内容方法：`content`

```
[
    'driver' => \HyperfExt\Sms\Drivers\LuosimaoDriver::class,
    'config' => [
       'app_key' => '',
    ],
]
```

#### [七牛云](https://www.qiniu.com/products/sms)

[](#七牛云)

短信消息类可用配置内容方法：`template`，`with`

```
[
    'driver' => \HyperfExt\Sms\Drivers\QiniuDriver::class,
    'config' => [
        'secret_key' => '',
        'access_key' => '',
    ],
]
```

#### [融云](https://www.rongcloud.cn/product/sms)

[](#融云)

短信消息类可用配置内容方法：`template`，`with`

```
[
    'driver' => \HyperfExt\Sms\Drivers\RongCloudDriver::class,
    'config' => [
        'app_key' => '',
        'app_secret' => '',
    ],
]
```

#### [容联云通讯](https://www.yuntongxun.com/api/sms.html)

[](#容联云通讯)

短信消息类可用配置内容方法：`template`，`with`

```
[
    'driver' => \HyperfExt\Sms\Drivers\RonglianDriver::class,
    'config' => [
        'app_id' => '',
        'account_sid' => '',
        'account_token' => '',
        'is_sub_account' => false,
    ],
]
```

#### [SendCloud](https://sendcloud.sohu.com/sms.html)

[](#sendcloud)

短信消息类可用配置内容方法：`template`，`with`

```
[
    'driver' => \HyperfExt\Sms\Drivers\SendCloudDriver::class,
    'config' => [
        'sms_user' => '',
        'sms_key' => '',
        'timestamp' => false, // 是否启用时间戳
    ],
]
```

#### [短信宝](http://www.smsbao.com/)

[](#短信宝)

短信消息类可用配置内容方法：`template`，`with`

```
[
    'driver' => \HyperfExt\Sms\Drivers\SmsBaoDriver::class,
    'config' => [
        'user' => '',
        'password' => '',
    ],
]
```

#### [腾讯云](https://cloud.tencent.com/product/sms)

[](#腾讯云)

短信消息类可用配置内容方法：`template`，`with`，`signature`

```
[
    'driver' => \HyperfExt\Sms\Drivers\TencentCloudDriver::class,
    'config' => [
        'sdk_app_id' => '',
        'secret_id' => '',
        'secret_key' => '',
        'sign' => null, // 短信签名
        'from' => [ // SenderId，中国大陆地区无需配置
            'default' => '', // 默认 SenderId
            // 'another' => '', // 其他 SenderId
        ],
    ],
]
```

#### [Twillo](https://www.twilio.com/sms)

[](#twillo)

短信消息类可用配置内容方法：`content`

```
[
    'driver' => \HyperfExt\Sms\Drivers\TwilioDriver::class,
    'config' => [
        'account_sid' => '',
        'token' => '',
        'from' => [
            'default' => '',
            // 'another' => '',
        ],
    ],
]
```

#### [UCloud](https://www.ucloud.cn/site/product/usms.html)

[](#ucloud)

短信消息类可用配置内容方法：`template`、`with`、`signature`

```
[
    'driver' => \HyperfExt\Sms\Drivers\UCloudDriver::class,
    'config' => [
        'private_key' => '',
        'public_key' => '',
        'sig_content' => '', // 短信签名
        'project_id' => '', // 项目ID,子账号才需要该参数
    ],
]
```

#### [云片](https://www.yunpian.com/product/domestic-sms)

[](#云片)

短信消息类可用配置内容方法：`content`、`signature`

```
[
    'driver' => \HyperfExt\Sms\Drivers\YunpianDriver::class,
    'config' => [
        'api_key' => '',
        'signature' => '', // 短信签名，内容中无签名时使用
    ],
]
```

#### [网易云信](https://yunxin.163.com/sms)

[](#网易云信)

短信消息类可用配置内容方法：`template`，`with`

```
[
    'driver' => \HyperfExt\Sms\Drivers\YunxinDriver::class,
    'config' => [
       'app_key' => '',
       'app_secret' => '',
       'code_length' => 4, // 随机验证码长度，范围 4～10，默认为 4
       'need_up' => false, // 是否需要支持短信上行
    ],
]
```

生成短信消息类
-------

[](#生成短信消息类)

应用发送的每种消息都被表示为短信消息类。这些类存储于 `app/Sms` 目录中。如果您的应用中没有该目录，别慌，当您使用 `gen:sms` 命令生成您的首个短信消息类时，应用将会自动创建它：

```
php bin/hyperf.php gen:sms VerificationCode
```

编写短信消息类
-------

[](#编写短信消息类)

所有的短信消息类的配置都在 `build` 方法中完成。您可以通过调用诸如 `content`、`template`、`signature` 和 `from` 这样的各种各样的方法来配置消息的内容及其发送。

### 配置发送器

[](#配置发送器)

在发送消息前，您需要指定哪些发送器可被用于发送消息。有两种方式来指定可用发送器。

#### 通过配置文件

[](#通过配置文件)

您可以通过配置文件的 `default.senders` 节点来指定默认的被用于发送消息的发送器，值类型可以是 `array` 或 `string`。

```
[
    'default' => [
        'senders' => ['aliyun', 'twillo'],
    ],
]
```

#### 通过 Smsable 类属性

[](#通过-smsable-类属性)

您也可以通过短信消息类的 `senders` 公开属性来指定可用发送器，如果您未在短信消息类中指定，那么在发送时会自动使用配置文件中指定的发送器。

```
use HyperfExt\Sms\Smsable;

class VerificationCode extends Smsable
{
    public $senders = ['aliyun', 'twillo'];
}
```

#### 指定地区限定的发送器

[](#指定地区限定的发送器)

由于短信服务提供商所服务的地区是有限的，亦或是因各地区价格因素的考量，您可能会为此使用多个提供商来让您的业务覆盖尽可能多的地区，此时您可为指定地区的号码配置指定的发送器。

```
[
    'aliyun' => ['cn', 'hk', 'mo', 'tw'], // 仅当手机号是大陆和港澳台地区时使用阿里云和腾讯云
    'tencent_cloud' => ['cn', 'hk', 'mo', 'tw'],
    'twillo', // 大陆和港澳台地区以外使用 Twillo
]
```

> 地区代码使用 ISO 3166-1 两位地区代码，不区分大小写。
>
> 发送器的筛选逻辑在内建的发送器策略中实现，您可以通过*自定义发送器策略*来根据自身需求实现自己的处理逻辑。

### 配置发送器策略

[](#配置发送器策略)

发送器策略是在使用多个发送器的情况下，用来确定发信程序以何种顺序来选择发送器的排序和筛选程序。当一个发送器发送失败后会根据顺序选择下一个发送器，直到发送成功。

组件内建 `OrderStrategy` 和 `RandomStrategy` 两个默认的策略。`OrderStrategy` 策略依照您指定发送器的顺序来选择发送器。`RandomStrategy` 策略则将发送器列表打乱来随机排序。

就像配置发送器一样，发送器策略有着相同的两种配置方式。配置文件的 `default.strategy` 节点，或短信消息类的 `strategy` 公开属性。

#### 自定义发送器策略

[](#自定义发送器策略)

您也可以自定义发送器策略，只需实现 `StrategyInterface` 接口。该接口只有一个 `apply` 方法，接受两个参数。第一个参数接受您配置的可用发送器列表，第二个参数接受要收信的手机号码 `MobileNumber` 类。

### 配置内容

[](#配置内容)

您可以在短信消息类的 `build` 方法中使用 `content` 方法指定短信内容，使用 `template` 方法来指定在短信模板，使用 `with` 方法来指定短信参数，使用 `signature` 方法来指定短信签名。

您需要根据您选择的短信服务的不同来选择使用哪些方法。

#### 通过 `content` 方法

[](#通过-content-方法)

```
public function build(SenderInterface $sender): void
{
    return $this
        ->content('您的验证码是 123456')
        ->signature('【HyperfExt】');
}
```

#### 通过 `template` 方法

[](#通过-template-方法)

```
public function build(SenderInterface $sender): void
{
    return $this
        ->template('SMS_001')
        ->signature('【HyperfExt】')
        ->with('code', '123456');
}
```

### 配置发件人 ID

[](#配置发件人-id)

某些短信业务提供商可能会要求传递发件人 ID（一般称为 `sender id` 或 `from`），您可以在对应发送器下的 `config.from` 节点内配置，并通过短信消息类的 `from` 方法使用。以 Twillo 为例：

```
// 在配置文件中指定
'twillo' => [
    'driver' => \HyperfExt\Sms\Drivers\TwilioDriver::class,
    'config' => [
        'account_sid' => '',
        'token' => '',
        'from' => [
            'default' => '123', // 当未在短信消息类中指定时使用 `default` 的值
            'another1' => '456',
            'another2' => '789',
        ],
    ],
],

// 在短信消息类中使用
public function build(SenderInterface $sender): void
{
    return $this
        ->from('another2');
}
```

发送短信
----

[](#发送短信)

若要发送短信，使用 `Sms` 辅助类的 `to` 方法。`to` 方法接受手机号码和实现了 `HasMobileNumber` 接口的实例。一旦指定了收信人，就可以将短信消息类实例传递给 `send` 方法：

```
