How to use Africa's Talking PHP Package

How to use Africa's Talking PHP Package

Hey, I finally got tired of using Africa's talking "PHP file" and finally decided to make a clean fluent package to make life easier. The package is fully compatible with Laravel and Vanilla PHP. If using another framework, you just need to import it and use the native implementation. So let's get started. If using vanilla PHP, add the following installer script so that the package can copy for you the config file once installation is complete. Laravel users don't need to add the post-update script as Laravel handles the configs pretty well.

"scripts": {
"post-update-cmd": [

Pull in the package using composer:

composer require smodav/africastalking

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

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

Once registered, publish the config file using:

php artisan vendor:publish

Bulk SMS


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.

// OR fetch from the last fetched ID

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.

// Or fetch from the last received ID

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.


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'.


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.


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.

  • Payments
  • Voice
  • USSD
  • Airtime

Related Articles