We use cookies to give you the best possible service. Carry on browsing or see our cookie policy hereClose X

PHP 5 SMS Send Class implementing listeners on our SMS Gateway

  • Why not share:
  • Social sharing button for Facebook
  • Social sharing button for Linkedin
  • Social sharing button for Twitter
  • Social sharing button for Googleplus
  • Social sharing button for Pinterest

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

	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 :-)

Related articles

  1. SMS Gateway API – Specification
    This blog post has been superseded with our website documentation It is recommended that you use the  RESTful version of the API which has largely superseded this document...
  2. How to integrate our SMS Gateway API into your systems
    Introduction Adding SMS as a communication method to a system is often very useful, for instance you don’t have issues...
  3. How to use an SMS Gateway
    Our SMS Gateway is extremely powerful indeed. It allows companies to connect to our systems and send out messages 1...
  4. Is there such a thing as ‘quality’ in Bulk SMS?
    There are a vast number of different ‘gateways’ that bulk SMS service providers can use to connect to the UK...