25th September 2017 10:18 pm
A Generic PHP SMS Library
This weekend I published sms-client, a generic PHP library for sending SMS notifications. It’s intended to offer a consistent interface when sending SMS notifications by using swappable drivers. That way, if your SMS service provider suddenly goes out of business or bumps up their prices, it’s easy to switch to a new one.
Out of the box it comes with drivers for the following services:
In addition, it provides the following test drivers:
Here’s an example of how you might use it with the ClockworkSMS driver:
use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\Response; use Matthewbdaly\SMS\Drivers\Clockwork; use Matthewbdaly\SMS\Client; $guzzle = new GuzzleClient; $resp = new Response; $driver = new Clockwork($guzzle, $resp, [ 'api_key' => 'MY_CLOCKWORK_API_KEY', ]); $client = new Client($driver); $msg = [ 'to' => '+44 01234 567890', 'content' => 'Just testing', ]; $client->send($msg);
If you want to roll your own driver for it, it should be easy - just create a class that implements the
Matthewbdaly\SMS\Contracts\Driver interface. Most of the existing drivers work using Guzzle to send HTTP requests to an API, but you don’t necessarily have to do that - for instance, you could create a driver for a mail-to-SMS gateway by using Swiftmailer or the PHP mail class. If you create a driver for it, please feel free to submit a pull request so I can add it to the repository.
For Laravel or Lumen users, there’s an integration package that should make it easier to use. For users of other frameworks, it should still be fairly straightforward to integrate.