承接 vluebridge/sms-engine 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vluebridge/sms-engine

Composer 安装命令:

composer require vluebridge/sms-engine

包简介

The SMS engine provides a way to send any SMS message to any mobile number and provide abstraction to SMS suppliers so it just works.

README 文档

README

SMS Engine is a PHP Library that wraps the implementation of SMS gateways (Semaphore, MyBusyBee) so you can send SMS using two simple lines of code:

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

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

Table of Contents

Supported SMS Suppliers

Right now, these are the only two SMS suppliers (or SMS Gateways) that are supported in this package:

If you wish you support an SMS gateway of your choice, feel free to submit a pull request.

Installation

composer require vluebridge/sms-engine

How To Use

It's important to note that you need to have a registered account from the supported SMS Suppliers above. You'll get your own API key when you login on their application.

For performance reasons, I highly recommend that you send SMS through a job, especially if you're gonna send 20+ SMS in one HTTP request.

Your server will communicate with the SMS Supplier's API server and you could potentially hit your max_execution_time especially if you put this code in a loop.

For Any PHP Project

Just write the following lines of code to wherever you want to start sending SMS.

<?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}");

For Laravel

  1. Create a config file in config/sms-engine.php with the following contents:
    <?php
    return [
        'suppliers' => [
            'semaphore' => [
                'api_key' => env('SEMAPHORE_API_KEY')
            ],
    
            'mybusybee' => [
                'api_key' => env('MYBUSYBEE_API_KEY')
            ],
        ]
    ];
  2. Add these variables in your .env file:
    MYBUSYBEE_API_KEY={YOUR API KEY}
    SEMAPHORE_API_KEY={YOUR API KEY}
  3. Write this code somewhere in your Laravel app:
    $supplier = 'semaphore';
    $engine = SmsEngine::init($supplier, config("sms-engine.suppliers.{$supplier}"));
    $engine->send("09xxxxxxxxx", "Hello po :)", "{SENDER NAME}");

I created a small Laravel command to make this even simpler. Just download this SendSmsCommand class and put it in your app/Console/Commandsdirectory.

Once installed, you will be able to send sms through this command:

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

Exception Handling

There are a lot of potential problems when you send an SMS. If you don't want your application to crash when it faces an error, it's a good idea to wrap it around a try-catch and catch the following exceptions:

  • InsufficientCreditsException
  • InvalidApiKeyException
  • InvalidMessageException
  • InvalidMobileNumberException

    If used with bulk mobile number, this exception is thrown if any one mobile number is invalid.

  • InvalidSenderNameException

    Thrown if you use a sender name that's not valid for your account. Usually, you have to buy this from your SMS supplier and wait for it to be approved.

  • MaxSmsRecipientReachedException

    Only 1,000 recipients limit per api call. For performance reasons.

  • SmsSendingException

    Occurs if some unknown error happens in sending SMS.

All these exceptions returns the SMS Supplier's API response body when you run $exception->getMessage(). Use it to figure out what's wrong, especially for SmsSendingException.

Registering your own SMS gateway

You may wish to support your own SMS supplier (like Nexmo, Twilio, etc.) in your own project. This is how you'll do it without touching the code base of this package:

  1. Create an adapter class and implement the SmsSupplierInterface contract. Example:

    <?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. Make sure to implement the exception handling depending on the API server's response.

  3. Register your adapter class by executing SmsEngine::registerSupplier('twilio', TwilioAdapter::class)

  4. Your SMS gateway should now be supported.

  5. (Optional) Make a pull request and share your implementation with us! Sharing is caring 😊

vluebridge/sms-engine 适用场景与选型建议

vluebridge/sms-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 vluebridge/sms-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vluebridge/sms-engine 我们能提供哪些服务?
定制开发 / 二次开发

基于 vluebridge/sms-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-25