byjg/sms-client

一个通用的、可扩展的轻量级短信客户端,用于发布Twilio和ByJG等短信服务提供商。

4.9.0 2024-01-05 23:27 UTC

This package is auto-updated.

Last update: 2024-09-16 15:53:32 UTC


README

Build Status Opensource ByJG GitHub source GitHub license GitHub release

这是一个简单的客户端,用于使用不同的提供商发送短信。

功能

  • 低代码发送短信
  • 完全解耦的类
  • 易于实现新的提供商

用法

使用ProviderFactory

// Register the provider and associate with a scheme
ProviderFactory::registerProvider(TwilioMessagingProvider::class);

// Create a provider
$provider = ProviderFactory::create(new Uri("twilio://$accountSid:$authToken@default"));

// Send a message
$response = $byjg->send("12221234567", (new \ByJG\SmsClient\Message("This is a test message")->withSender("+12223217654"));

// Check if the message was sent
if ($response->isSent()) {
    echo "Message sent";
} else {
    echo "Message not sent";
}

使用ProviderFactory根据国家发送消息到多个提供商

// Register the provider and associate with a scheme
ProviderFactory::registerProvider(TwilioMessagingProvider::class);
ProviderFactory::registerProvider(ByJGSmsProvider::class);

// Define the provider according to the country prefix
ProviderFactory::registerServices("twilio://accoundId:authToken@default", ["+1"]);
ProviderFactory::registerServices("byjg://username:password@default", ["+55"]);

// Send a message and select the provider according to the country prefix
$response = ProviderFactory::createAndSend("+5521900001234", (new \ByJG\SmsClient\Message("This is a test message")));
var_dump($response);

$response = ProviderFactory::createAndSend("+12221234567", (new \ByJG\SmsClient\Message("This is a test message"))->withSender("+12223217654"));
var_dump($response);

提供商

提供商是负责发送文本消息的类。

所有提供商都拥有以下接口

<?php
interface ProviderInterface
{
    public static function schema();

    public function setUp(Uri $uri);

    public function send($to, Message $envelope): ReturnObject;
}

没有必要调用方法 getConnection(),因为方法 publish() 和 consume() 会自动调用它。只有在需要直接访问连接时,才使用方法 getConnection()

已实现提供商

安装

composer require "byjg/sms-client"

依赖关系

开源ByJG

开源ByJG