How To Use Africa's Talking PHP Package

19th Oct 2017

When using Laravel 5.5+, the package will auto-discover, otherwise register the package on your config/app.php file

  'providers' => [
    ...
    SmoDav\AfricasTalking\Laravel\ServiceProvider::class,
    ...
  ],
  'aliases' => [
    ...
    'SMS' => SmoDav\AfricasTalking\Laravel\Facades\SMS::class,
  ],

Once registered, publish the config file using:

php artisan vendor:publish

Bulk SMS

Laravel

Africa's Talking has a nice SMS with very little to no downtime. When using Laravel, just use the Facade to make your sms transactions. If sending bulk, the mobile numbers should be separated by a comma, eg. +254722000000,+254721000000.

\SMS::to('+254722000000')->message('Hi there')->send();

In case you have a shortcode, you can also include it with the request.

\SMS::to('+254722000000')->message('Hi there')->from(12345)->send();

If you want to add other Africa's Talking options, use the withOptions method

\SMS::to('+254722000000')->message('Hi there')->from(12345)->withOptions([])->send();

You can also use one combined function to execute the call:

\SMS::send('+254722000000', 'Hi there');
// OR
\SMS::send('+254722000000', 'Hi there', 12345);
// OR
\SMS::send('+254722000000', 'Hi there', 12345, []);

To fetch the SMS from your inbox you can use the fetch method on the Facade. This method will return only 100 items as per Africa's Talking API.

\SMS::fetch();
// OR fetch from the last fetched ID
\SMS::fetch(8147);

The helper functions below also work with Laravel. You do not need to instantiate the Config since Laravel's IoC will do it for you.

Vanilla PHP

When using vanilla PHP, you will need to instantiate the Config before you use the package. After that, just use the helper function sms:


require 'vendor/autoload.php';

use GuzzleHttp\Client;
use SmoDav\AfricasTalking\Config\Config;
use SmoDav\AfricasTalking\Native\NativeConfig;
new Config(new NativeConfig(), new Client());

// use the helper sms function
sms('+254722000000', 'Hello there.');
// add the shortcode
sms('+254722000000', 'Hello there.', 123456);
// add the options
sms('+254722000000', 'Hello there.', 123456, []);

The package also includes a fetch helper function to be used to get messages from your inbox.

fetch();
// Or fetch from the last received ID
fetch(8147);

Premium Subscription SMS

For subscription SMS, the users will be charged a given amount whenever a message is sent to them. This package allows you to subscribe, unsubscribe and also check your current subscriptions.

Laravel

You can either use the facade or the helper function. So let's register a user on our cool shortcode 12345 with the keyword 'smodav'.

\Subscription::subscribe('+254722000000')->toCode(123456)->usingKeyword('smodav')->send();

Now the user will get a USSD prompt to either accept or reject the subscription. We can also decide to unsubscribe the user in case they decide to opt out.

\Subscription::unsubscribe('+254722000000')->fromCode(123456)->usingKeyword('smodav')->send();

Alas! It's done. The mobile number has been unregistered. We can also check the current subscribers that we have by querying Africa's Talking.

\Subscription::getSubscriptions(12345, 'smodav');

If you have the last received ID, add it as the third argument.

Vanilla PHP

The package also has helper functions that are usable with either Laravel or Vanilla PHP. The initial process is the same as SMS.


require 'vendor/autoload.php';

use GuzzleHttp\Client;
use SmoDav\AfricasTalking\Config\Config;
use SmoDav\AfricasTalking\Native\NativeConfig;
new Config(new NativeConfig(), new Client());

// Register a new number
subscribeMobile('+254722000000', 12345, 'smodav');
// Un register an existing number
unsubscribeMobile('+254722000000', 12345, 'smodav');
// Check your subscribers
subscriptions(123456, 'smodav');
// If having the last recived ID
subscriptions(123456, 'smodav', 8147);

That's it. I'll keep updating this post as I add more features to the package.

TODO:
  • Payments
  • Voice
  • USSD
  • Airtime
© 2020 SmoDav Productions