byjg / sms-client
A generic and extensible lightweight sms client to publish SMS service providers like Twilio and ByJG.
Fund package maintenance!
byjg
Installs: 3 477
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.1 <8.4
- ext-curl: *
- byjg/webrequest: ^5.0
Requires (Dev)
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^5.9
README
This is a simple client to send SMS using different providers.
Features
- Low code to send SMS
- Classes totally decoupled
- Easy to implement new providers
Usage
Using 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"; }
Using ProviderFactory to send a message to multiple providers depending on the country
// 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);
Providers
The providers are the classes responsible to send the text message.
All providers have the following interface:
<?php interface ProviderInterface { public static function schema(); public function setUp(Uri $uri); public function send($to, Message $envelope): ReturnObject; }
There is no necessary call the method getConnection()
because the method publish() and consume() will call it automatically.
Use the method getConnection()
only if you need to access the connection directly.
Implemented providers
Install
composer require "byjg/sms-client"