PHP code example of weezqyd / africastalking

1. Go to this page and download the library: Download weezqyd/africastalking library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

weezqyd / africastalking example snippets




use AfricasTalking\Gateway;
use Http\Adapter\BuzzAdapter;

// These Headers are ',
        '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);
        
 

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);
}


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);
}


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);
}