samrap / teleapi-sms
Teleapi SMS package.
Requires
- php: >=5.6
- guzzlehttp/psr7: ^1.3
- php-http/client-implementation: ^1.0
- php-http/discovery: ^1.1
- php-http/httplug: ^1.1
- php-http/message: ^1.4
Requires (Dev)
- php-http/mock-client: ^0.3.3
- phpunit/phpunit: ^5.7
This package is not auto-updated.
Last update: 2024-11-10 01:54:31 UTC
README
This package provides a fluent interface to the Teleapi SMS service.
Installation
Install via Composer:
composer require samrap/teleapi-sms
Usage
The Teleapi SMS package provides two classes which allow you to easily create and send text messages over HTTP.
The Client
The Client is used to send text messages to the Teleapi SMS service. The HTTP implementation is abstracted using httplug, allowing you to define any PSR-7 compliant client or adapter as the HTTP layer. This gives you full control over how requests are sent and allows you to easily mock API requests in unit tests.
To get started, you will need to choose the HTTP client you want to use. We recommend using the PHP HTTP CURL Client for simple applications and Guzzle for more complex apps. Of course, any PSR-7 compliant library can be used. In this example we will use the CURL Client.
First, we will add the CURL Client to our requirements:
composer require php-http/curl-client
Now that our HTTP client is installed, we can instantiate and use our SMS client:
use Teleapi\Sms\Client; $client = new Client('api_token');
We have now created our SMS client which is ready for use. Notice how we did not specify the HTTP client to use. Behind the scenes, the SMS client searches for any installed package that provides a php-http/client-implementation
and loads it automagically. This will only work for some clients, it is better practice to specify the HTTP client explicitly. Have a look at Clients and Adapters in the PHP HTTP documentation on how to instantiate a client. Then, simply pass it as the second argument to the SMS Client's constructor:
use Teleapi\Sms\Client; // Some code to create our HTTP client... $client = new Client('api_token', $httpClient);
In any case, we are now ready to use our SMS client, so let's create a Message and send it off!
All messages are represented by the Teleapi\Sms\Message
class and are extremely simple to create:
use Teleapi\Sms\Message; $to = '7148675309'; $from = '7141234567'; $text = 'Welcome to our amazing product!'; $message = new Message($to, $from, $text);
Now, we can send the message on its way by calling the client's send
method and passing the Message as its single argument:
$client->send($message);
We can check if the message sent successfully by calling the its sent
method:
$client->send($message); if ($message->sent()) { echo 'Successfully sent message!'; }
A complete example might look like the following:
use Teleapi\Sms\Client; // Some code to create our HTTP client... $client = new Client('api_token', $httpClient); $message = new Message( '7148675309', '7141234567', 'Welcome to our amazing product!' ); $client->send($message); if ($message->sent()) { echo 'Successfully sent message!'; }