sms script
No notes
Syntax:
PHP
<?php /** * LebLines SMS sending class * * @author Dany Moussa * @since December 26 2011 */ class sms { /** * @var Array */ private static $responses = array( 400 => 'Missing Parameter', 410 => 'Wrong Message', 99 => 'Inceficient Credit', 200 => 'Sent Successfully' ); /** * @var String */ private static $user; /** * * @var String */ private static $pass; /** * * @var String */ private static $url = 'http://www.leblines.com/api/bulkSend.php?format='; /** * * @var String */ private static $format = 'json'; /** * @var Array */ /** * @var String */ private $message; /** * @var Array */ /** * * @var Array */ /** * Allows to assign the message upon instance creation * * @param String $message */ public function __construct( $message = '' ) { if ( $message ) $this->setMessage( $message ); } /** * Add error message to the errors Array * * @param String $error_message */ private function setError( $error_message ) { $this->errors[ ] = $error_message; } /** * Returns array of errors * * @return Array */ public function getErrors() { return $this->errors; } /** * Return raw response array * * @return type */ public function getResponse() { return $this->response; } /** * Set username statically to avoid repetition on every usage * * @param String $user */ public static function setUser( $user ) { self::$user = $user; } /** * Set password statically to avoid repetition on every usage * * @param String $pass */ public static function setPass( $pass ) { self::$pass = $pass; } /** * Set String or Array of mobile numbers * * @param mixed $recipients * @return Object for chaining */ public function setRecipient( $recipients ) { { $this->setError( "The parameter passed for " . __FUNCTION__ . " isn't correct" ); } else { { $this->recipient = $recipients; } else { { } else { } } } return $this; } /** * Return mobile numbers * * @param Bool $stringfied to control the response type * @return Mixed */ public function getRecipient( $stringfied = false ) { if ( $stringfied == true ) { } return $this->recipient; } /** * Assign the message to send * * @param type $message * @return Object for chaining */ public function setMessage( $message ) { { $this->setError( "The message assigned is empty" ); } else { $this->message = $message; } return $this; } /** * Return the SMS message previously assigned * * @return String */ public function getMessage() { return $this->message; } /** * Set statically the format used to get responses * * @param String $format only Json & XML are valid */ public static function setFormat( $format ) { { throw new Exception( "This >> {$format} << is not supported, please choose xml or json" ); } else { $this->format = $format; } } /** * Set the API address statically of whcih the system will be using to submit & retrieve data * * @param String $url */ public static function setURL( $url ) { self::$url = url; } /** * Return the format previously assigned * * @return String */ public static function getFormat() { return self::format; } /** * Validate before sending * * @return Bool */ public function isValid() { $this->setError( "Authentication parameters (username & password) have to be assigned" ); $this->setError( "Message attribute cannot be left empty" ); $this->setError( "Recipient attribute cannot be left unassigned" ); } /** * Decode/Convert the JSON or XML responses retreived from API to Array * * @param String $string * @return Array */ private static function decode( $string ) { if ( self::$format == 'json' ) { } elseif ( self::$format == 'xml' ) { } } /** * Send the SMS message to the assigned recipients * The request & responses are made through POST over HTTP * * @return Object for chaining */ public function send() { if ( !$this->isValid() ) { } else { 'user' => self::$user, 'pass' => self::$pass, 'to' => $this->getRecipient( true ), 'message' => $this->getMessage() ) ); 'http' => 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ) ); $this->response = self::decode( $result ); } return $this; } /** * Return the response code returned from the API after sending the message * * @return integer */ public function getResponseCode() { $result = $this->getResponse(); return $result[ 'REQUEST_RESULT' ]; } /** * Return message based ont he response code * * @return String */ public function getResponseMessage() { return self::$responses[ $this->getResponseCode() ]; } /** * Dirty way to send SMS * Required parameters to pass are * * @param Mixed $param * @param Bool $return_response_code */ public static function sendSMS( $param, $return_response_code = true ) { try { $class = __CLASS__; $obj = new $class; // Optional parameters for configurations if ( $param[ 'user' ] ) $class::$user = $param[ 'user' ]; if ( $param[ 'pass' ] ) $class::$pass = $param[ 'pass' ]; if ( $param[ 'url' ] ) $class::$url = $param[ 'url' ]; if ( $param[ 'format' ] ) $class::$url = $param[ 'format' ]; $response_code = $obj->setMessage( $param[ 'message' ] )->setRecipient( $param[ 'recipient' ] )->send()->getResponseCode(); if ( $return_response_code ) return $response_code; } catch ( Exception $e ) { return $e->getMessage(); } } } // Configure sms::setUser( 'lebfiles' ); sms::setPass( 'lebanonsms645' ); ?>