= creditAlert() get's notified private static $INSTANCE=false; private $workingURL; private static $FP_FAILED = "Server Unavailable"; private $credits; public static function getInstance($username,$password) { // singleton design so the getInstance() function is called to instantiate the class // the contructor uses the username and password you get allocated from the sign up process if(self::$INSTANCE==false and $username and $password) self::$INSTANCE = new SendSMSNotifier($username,$password); return self::$INSTANCE; } public function addListener($myClass) { /// Add your listener class it must implement 2 public functions creditAlert($credits_remaining) and failureAlert($error,$number,$message,$error) $this->listeners[]=$myClass; } public function removeListener($myClass) { $myClassString=get_class($myClass); foreach($this->listeners as $obj) { if(is_a($obj,$myClassString)) ; else $tempList[]=$obj; } $this->listeners = $tempList; } public function resetListeners() { $this->listeners = null; } public function getCredits() { return $this->credits; } public function setCreditThreshold($noOfCredits) { //// Your credit threshold, set this to a value where by you will get notified if($noOfCredits > 0 and is_integer($noOfCredits)){ $this->creditAlertThreshold = $noOfCredits; return true; } else return false; } public function send($number,$message,$originator=null) { $query_string = null; $query_string .="&number=".$number; $query_string .="&message=".urlencode($message); $query_string .="&orig=".urlencode($originator); $fp =fopen($this->workingURL.$query_string,"r"); if(!$fp) { $this->notifyFailureListeners(self::$FP_FAILED,$number,$message,$originator); return false; } else{ $response = fread($fp,1024); return ($this->processResponse($response,$number,$message,$originator)); } } ///// PRIVATE FUNCTIONS ///// private function __construct($username,$password) { $this->workingURL = self::$URL."?username=$username&password=$password"; } private function processResponse($r,$n,$m,$o) { /// update the credit counter and see if the listeners need to be notified $this->parseCreditStatus($r); /// check to see if we have a succesful send if(strstr($r,"SUCCESS")!==false) { return true; } else{ $this->processErrors($r,$n,$m,$o); return false; } } private function parseCreditStatus($r) { $lines = explode("\r\n",$r); array_pop($lines); $this->credits = array_pop($lines); if($this->credits <= $this->creditAlertThreshold) $this->notifyCreditListeners(); } private function notifyCreditListeners() { foreach ($this->listeners as $listener) { $listener->creditAlert($this->credits); } } private function processErrors($r,$n,$m,$o) { $lines = explode("\r\n",$r); foreach($lines as $line) { if(stristr($line,"Error")!==false) $this->notifyFailureListeners($line,$n,$m,$o); } } private function notifyFailureListeners($e,$n,$m,$o) { foreach ($this->listeners as $listener) { $listener->failureAlert($e,$n,$m,$o); } } } /* ///// EXAMPLE OF USE class MyTestClass { private $mySMSdata = array(array("44777777777","Hello World!","Me"), array("text","This will throw an error, mobile number is text","Me"), array("4477777777","Hello World this message is way way way way wayyyyy tooooo long more than 160 chars zxcvbnmasdfghjklqwertyuiop,Hello World this message is way way way way wayyyyy tooooo long more than 160 chars zxcvbnmasdfghjklqwertyuiop!!","Me")); function __construct() { $sms = SendSMSNotifier::getInstance("myUsername","myPassword"); $sms->addListener($this); $sms->setCreditThreshold(5); foreach($this->mySMSdata as $record) { list($number,$message,$sender) = $record; $sms->send($number,$message,$sender); } echo $sms->getCredits(); print_r($this->errors); } public function failureAlert($error,$number,$message,$originator) { /// Required function for failure messages $this->errors[] = array($error,$number,$message,$originator); } public function creditAlert($credits) { /// Required function for credit alerts die("I have nearly run out of credits($credits left), abandon sending!"); } } new MyTestClass(); */ ?>