anyspin/sms-sender

Laravel 的 SMS 发送器

v0.0.2 2020-12-10 10:54 UTC

This package is auto-updated.

Last update: 2024-09-10 18:56:05 UTC


README

SMS Laravel 包

基于驱动程序

当前实现的驱动程序

  • log - 用于调试目的
  • smsru - sms.ru
  • ... 即将推出 smsc.ru, smspilot.ru, 等

安装

从终端运行此命令

composer require anyspin/sms-sender

如果您想使用外观,在您的 app.php 配置文件中,如果想要使用外观,请将以下行添加到别名数组中

'Sms' => Anyspin\SmsSender\SmsFacade::class,

配置

您可以使用 artisan 命令将默认配置文件发布到 config/sms.php

php artisan vendor:publish --tag=config

用法

发送 SMS

<?php

use Anyspin\SmsSender\SmsFacade as Sms;
use Anyspin\SmsSender\SmsStatus;

$result = Sms::content('Your code: 123456', function($message) {
    $message->to('+79991234567');

    // or multiple recipients:

    $message->to([
        '+79991234567',
        '+79991234568',
        '+79991234569',
    ]);
});

if ($result[0]->status === SmsStatus::SENT)
{
    // message is sent successfully
}

// or the other way:

if ($result[0]->sent())
{
    // message is sent successfully
}

$result 是一个包含 SmsStatus 对象的数组(每个接收者一个),其中包含以下属性

  • id - SMS 外部 ID
  • to - 接收者的电话号码
  • cost - 此消息的成本(仅在检查时可用,见下文)
  • status - SMS 状态,以下之一
    • SmsStatus::NONE
    • SmsStatus::SENT
    • SmsStatus::DELIVERING
    • SmsStatus::DELIVERED
    • SmsStatus::FAILED

为了方便,SmsStatus 还有一些方法

  • sent() - 如果消息发送成功,则返回 true
  • delivering() - 如果消息正在传输,则返回 true
  • delivered() - 如果消息成功送达,则返回 true
  • succeed() - 如果消息已发送或正在传输或已送达,则返回 true
  • failed() - 如果消息发送失败,则返回 true

检查 SMS 状态

<?php

use Anyspin\SmsSender\SmsFacade as Sms;
use Anyspin\SmsSender\SmsStatus;

$result = Sms::check('123456');

if ($result[0]->status === SmsStatus::DELIVERED)
{
    // message is delivered successfully
}

// or the other way:

if ($result[0]->delivered())
{
    // message is delivered successfully
}

您也可以同时检查多个 ID

$result = Sms::check(['123456', '123457', '123458']);

检查外部服务的余额

<?php

use Anyspin\SmsSender\SmsFacade as Sms;

$balance = Sms::balance();