vluebridge/sms-engine

短信引擎提供了一种将任何短信发送到任何手机号码的方法,并提供对短信供应商的抽象,使其轻松工作。

v1.0 2021-01-25 11:33 UTC

This package is auto-updated.

Last update: 2024-09-25 19:35:27 UTC


README

短信引擎是一个PHP库,它封装了短信网关(如Semaphore、MyBusyBee)的实现,这样您可以使用两行简单的代码发送短信

$engine = SmsEngine::init('semaphore', 'xxxxxxx');

$engine->send("09xxxxxxxxx", "Your message", "Sender Name");
// Your `Sender Name` must be purchased & approved, or it'll throw InvalidSenderNameException

目录

支持的短信供应商

目前,这个包只支持以下两种短信供应商(或短信网关)

如果您希望支持您选择的短信网关,请随时提交一个拉取请求。

安装

composer require vluebridge/sms-engine

如何使用

请注意,您需要从上述 支持的短信供应商 中注册一个账户。您登录他们的应用程序后,将获得自己的API密钥。

出于性能考虑,我强烈建议您通过 工作 发送短信,特别是如果您打算在一次HTTP请求中发送20条以上的短信。

您的服务器将与短信供应商的API服务器通信,您可能会遇到 max_execution_time,特别是如果您将此代码放入循环中。

适用于任何PHP项目

只需将以下几行代码写入您想开始发送短信的地方。

<?php
require_once('vendor/autoload.php');

use Vluebridge\SmsEngine;

$engine = SmsEngine::init('semaphore', '{API KEY}');
$engine->send("09xxxxxxxxx", "Hello po :)", "{SENDER NAME}");
// You can send the same message in multiple numbers by adding a comma in mobile number string.
// For example:
// $engine->send("09xxxxxxxxx,09xxxxxxxxx,09xxxxxxxxx", "You all the best", "{SENDER NAME}");

适用于Laravel

  1. config/sms-engine.php 中创建一个配置文件,并包含以下内容
    <?php
    return [
        'suppliers' => [
            'semaphore' => [
                'api_key' => env('SEMAPHORE_API_KEY')
            ],
    
            'mybusybee' => [
                'api_key' => env('MYBUSYBEE_API_KEY')
            ],
        ]
    ];
  2. 在您的 .env 文件中添加以下变量
    MYBUSYBEE_API_KEY={YOUR API KEY}
    SEMAPHORE_API_KEY={YOUR API KEY}
  3. 在您的Laravel应用程序中某个位置写入以下代码
    $supplier = 'semaphore';
    $engine = SmsEngine::init($supplier, config("sms-engine.suppliers.{$supplier}"));
    $engine->send("09xxxxxxxxx", "Hello po :)", "{SENDER NAME}");

我创建了一个小的Laravel命令来使这个过程更加简单。只需下载这个 SendSmsCommand类 并将其放入您的 app/Console/Commands 目录。

安装完成后,您可以通过此命令发送短信

php artisan sms:send semaphore 09xxxxxxxxx "Hello darkness my old friend" --sender="Sender Name"

异常处理

发送短信时可能会有很多潜在的问题。如果您不希望应用程序在遇到错误时崩溃,将代码包装在 try-catch 中并捕获以下异常是一个好主意

  • InsufficientCreditsException
  • InvalidApiKeyException
  • InvalidMessageException
  • InvalidMobileNumberException

    如果使用批量手机号码,当任何手机号码无效时,将抛出此异常。

  • InvalidSenderNameException

    当您使用一个不适合您账户的发送者名称时,将抛出此异常。通常,您需要从您的短信供应商那里购买它并等待其批准。

  • MaxSmsRecipientReachedException

    每个API调用仅限1,000个收件人。出于性能考虑。

  • SmsSendingException

    在发送短信时发生未知错误时发生。

所有这些异常在运行 $exception->getMessage() 时都返回短信供应商的API响应体。使用它来找出问题所在,特别是对于 SmsSendingException

注册自己的短信网关

您可能希望在您的项目中支持自己的短信供应商(如Nexmo、Twilio等)。这是在不修改此包的代码库的情况下完成此操作的方法

  1. 创建一个适配器类并实现 SmsSupplierInterface 接口。示例

    <?php
    
    use Vluebridge\Contracts\SmsSupplierInterface;
    
    class TwilioAdapter implements SmsSupplierInterface {
        protected $client;
        
        public function __construct($api_key) {
            // Support argument of single string of API Key
            // and an array of configuration in case you need more info like `api_secret`
            if(is_array($api_key)) {
                if(! isset($api_key['api_key'])) {
                    throw new \InvalidArgumentException("Missing `api_key` value in array");
                }
    
                $api_key = $api_key['api_key'];
            }
            
            // Your SMS Supplier usually provides their own PHP package so you don't have to
            // make one from scratch. You can use that here and create the $client field
            // and get the API key from this $conf variable.
            $this->client = new WhateverApiPackage($api_key);
        }
    
        public function send($recipients, $message, $sender_name = null) {
            // Insert code here how they implement their own SMS sending feature.
            // Usually, it's something like this:
            $this->client->send($recipients, $message, $sender_name);
        }
     }
  2. 确保根据API服务器的响应实现异常处理

  3. 通过执行SmsEngine::registerSupplier('twilio', TwilioAdapter::class)注册您的适配器类。

  4. 现在应该支持您的短信网关。

  5. (可选)提交一个拉取请求并与我们分享您的实现!分享就是关爱 😊