PHP 5 SMS Send Class implementing listeners on our SMS Gateway

PHP 5 SMS Send Class implementing listeners on our SMS Gateway

Class Name: SendSMSNotifier

This class is designed around the FREE Text Marketer SMS API, you need to sign up here to use it.
SendSMSNotifier is a singleton class design, incorporating listeners. Your class must include the following public functions: creditAlert($credits_remaining) and failureAlert($error,$number,$message,$error).
Download the class from here

Example of use

class MyTestClass
{
        /// simple array of data in: number, message, sender Id (aka originator) format, ideally the data will come from a database
	private $mySMSdata = array(array("44777777777","Hello World!","Me"),
			                             array("text","This will throw an error, mobile number is text","Me"));
	function __construct()
	{
		$sms = SendSMSNotifier::getInstance("myUsername","myPassword"); // your account login details
		$sms->addListener($this); // Your class gets added to the listeners that will get notified on an event
		$sms->setCreditThreshold(5); // You want o be notified when you have 5 credits left
		foreach($this->mySMSdata as $record) // send some data
		{
			list($number,$message,$sender) = $record;
			$sms->send($number,$message,$sender); // send to the api
		}
		echo $sms->getCredits();
		print_r($this->errors);
	}
	public function failureAlert($error,$number,$message,$originator)
	{
		/// Required function for failure messages, you could stop the sending here or log the results
		$this->errors[] = array($error,$number,$message,$originator);
	}
	public function creditAlert($credits)
	{
		/// Required function for credit alerts, this basic implementation stops sending anymore SMS's
		die("I have nearly run out of credits($credits left), abandon sending!");
	}
}

So here’s what happens. You send a bunch of text messages to the gateway using your class (in this example MyTestClass), if you get a failure such as a badly formatted number your function failureAlert gets called with the parameters of the failure and data, you can then decide on the implmentation. In the above example it just stores them in an array, once you have sent all the data you could then use the data to clean your database etc.
If the credit limit (in the above case this is 5 $sms->setCreditThreshold(5) ) your function creditAlert will get called, in this case we think this is bad so we use the hammer of the die() function to stop the sending, obviously you would handle this much more elegantly 🙂



Popular posts

View our infographics View our helpful guides SMS Web Pages