ejimba/africas-talking-gateway

Unofficial Africas Talking Gateway for PHP

v1.0.1 2015-10-24 18:00 UTC

This package is auto-updated.

Last update: 2021-09-28 16:13:48 UTC


README

Introduction

Unofficial Africas Talking Gateway PHP package to integrate SMS, USSD, VOICE, AIRTIME in your application.

Requirements

  • PHP 5.3 or above
  • cURL

Installation

Composer Installation

composer require ejimba/africas-talking-gateway

Usage

If you are using a framework that has autoloading:

<?php

use AfricasTalkingGateway\AfricasTalkingGateway;

// Specify your login credentials
$username   = "MyAfricasTalkingUsername";
$apikey     = "MyAfricasTalkingAPIKey";

// Specify the numbers that you want to send to in a comma-separated list
// Please ensure you include the country code (+254 for Kenya in this case)
$recipients = "+254711XXXYYY,+254733YYYZZZ";

// And of course we want our recipients to know what we really do
$message    = "I'm a lumberjack and its ok, I sleep all night and I work all day";

// Create a new instance of the gateway class
$gateway    = new AfricasTalkingGateway($username, $apikey);

// Any gateway error will be captured by the custom Exception class below, 
// so wrap the call in a try-catch block
try 
{

    $results = $gateway->sendMessage($recipients, $message);
            
    foreach( $results as $result ) {
    
        // status is either "Success" or "error message"
        echo " Number: " .$result->number;
        echo " Status: " .$result->status;
        echo " MessageId: " .$result->messageId;
        echo " Cost: "   .$result->cost."\n";
    
    }

}
catch ( AfricasTalkingGatewayException $e )
{

    echo "Encountered an error while sending: ".$e->getMessage();

}

?>

If you are using flat php scripts, please remember to require autoloader

require __DIR__ . '/vendor/autoload.php';

The example above will be:

<?php

require __DIR__ . '/vendor/autoload.php';

use AfricasTalkingGateway\AfricasTalkingGateway;

// Specify your login credentials
$username   = "MyAfricasTalkingUsername";
$apikey     = "MyAfricasTalkingAPIKey";

// Specify the numbers that you want to send to in a comma-separated list
// Please ensure you include the country code (+254 for Kenya in this case)
$recipients = "+254711XXXYYY,+254733YYYZZZ";

// And of course we want our recipients to know what we really do
$message    = "I'm a lumberjack and its ok, I sleep all night and I work all day";

// Create a new instance of the gateway class
$gateway    = new AfricasTalkingGateway($username, $apikey);

// Any gateway error will be captured by the custom Exception class below, 
// so wrap the call in a try-catch block
try 
{

    $results = $gateway->sendMessage($recipients, $message);
            
    foreach( $results as $result ) {
    
        // status is either "Success" or "error message"
        echo " Number: " .$result->number;
        echo " Status: " .$result->status;
        echo " MessageId: " .$result->messageId;
        echo " Cost: "   .$result->cost."\n";
    
    }

}
catch ( AfricasTalkingGatewayException $e )
{

    echo "Encountered an error while sending: ".$e->getMessage();

}

?>

Bugs and Issues

Bugs and issues are tracked on GitHub

License

Licensed under Africas Talking License.