weezqyd / africastalking
PHP Api gateway for AfricasTalking
Requires
- weezqyd/http-adapters: ^0.0.1
Requires (Dev)
- guzzlehttp/guzzle: ^6.3
- kriswallsmith/buzz: ~0.10
- phpunit/phpunit: ~4.6|~6.1
This package is auto-updated.
Last update: 2024-11-13 22:23:27 UTC
README
Install the package from composer
$ composer require weezqyd/africastalking @dev
You then need to install one of the following: But we recomend GuzzleHttp
$ composer require guzzlehttp/guzzle $ composer require kriswallsmith/buzz $ composer require nategood/httpful
Configure the Adapter
This package uses guzzlehttp as the default adapter, if you decide to use another http client good for you all you need is to configure your client and pass the adapter as the fourth parameter to the gateway's constructor. You can also create your own adapter as long as it implements Http\Adapter\AdapterInterface
.
An example is worth a thousand words
require_once 'vendor/autoload.php'; use AfricasTalking\Gateway; use Http\Adapter\BuzzAdapter; // These Headers are required $headers = [ 'apikey' => 'API-KEY', 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', ]; $adapter = new BuzzAdapter($headers); // Pass the adapter and your Africastalking Username to the gateway // The third parameter is a sandbox flag when true the Api wiil run in the sandbox, The defaults is false // Because we are using a custom client you dont need provide the API KEY to the gateway // Instead pass an empty string or null $gateway = new Gateway('USERNAME', null, true, $adapter);
Sending an SMS Message
Now let us send an SMS notification
use AfricasTalking\Gateway; use Http\Exceptions\HttpException; $gateway = new Gateway('API-TOKEN', 'USERNAME'); try { $response = $gateway->sms->sendMessage('+254700123XXX', 'My sample message'); var_dump($response); } catch(HttpException $e) { print_r($e); }
Passing additional options
The SMS API accept additional options to be passed allong with the request
Send with options
use Http\Exceptions\HttpException; // ..... Rest of adapter setup $options = [ 'from' => '22123', 'enqueque' => 1 ]; try { $response = $gateway->sms->sendMessage('+254700123456', 'My sample message', $options); var_dump($response); } catch(HttpException $e) { print_r($e); }
Sending to multiple recipients
At times you would want to do somthing with the API response. Well worry no more. To acheive this pass a callback funtion as the fourth parameter of the sendMessage()
method. This callback will be run for each message that is sent
use Http\Exceptions\HttpException; // ..... Rest of adapter setup $recipients = ['+254700123456', '+254700123123', '+254700123000']; try { $gateway->sms->sendMessage($recipients, 'My sample message', ['from' => '22123'], funtion($recipient) { // $recipient->status; // $recipient->messageId; // $recipient->cost; // $recipient->number; }); } catch(HttpException $e) { print_r($e); }