sms script

by xcoder on December 26th, 2011
No notes
Syntax: PHP
Show lines - Hide lines - Show in textbox - Download
  1. <?php
  2.  
  3. /**
  4.  * LebLines SMS sending class
  5.  *
  6.  * @author Dany Moussa
  7.  * @since December 26 2011
  8.  */
  9. class sms
  10. {
  11.  
  12. /**
  13.   * @var Array
  14.   */
  15. private static $responses = array( 400 => 'Missing Parameter', 410 => 'Wrong Message', 99 => 'Inceficient Credit', 200 => 'Sent Successfully' );
  16.  
  17. /**
  18.   * @var String
  19.   */
  20. private static $user;
  21.  
  22. /**
  23.   *
  24.   * @var String
  25.   */
  26. private static $pass;
  27.  
  28. /**
  29.   *
  30.   * @var String
  31.   */
  32. private static $url = 'http://www.leblines.com/api/bulkSend.php?format=';
  33.  
  34. /**
  35.   *
  36.   * @var String
  37.   */
  38. private static $format = 'json';
  39.  
  40. /**
  41.   * @var Array
  42.   */
  43. private $recipient = array( );
  44.  
  45. /**
  46.   * @var String
  47.   */
  48. private $message;
  49.  
  50. /**
  51.   * @var Array
  52.   */
  53. private $errors = array( );
  54.  
  55. /**
  56.   *
  57.   * @var Array
  58.   */
  59. private $response = array( );
  60.  
  61. /**
  62.   * Allows to assign the message upon instance creation
  63.   *
  64.   * @param String $message
  65.   */
  66. public function __construct( $message = '' )
  67. {
  68. if ( $message )
  69. $this->setMessage( $message );
  70. }
  71.  
  72. /**
  73.   * Add error message to the errors Array
  74.   *
  75.   * @param String $error_message
  76.   */
  77. private function setError( $error_message )
  78. {
  79. $this->errors[ ] = $error_message;
  80. }
  81.  
  82. /**
  83.   * Returns array of errors
  84.   *
  85.   * @return Array
  86.   */
  87. public function getErrors()
  88. {
  89. return $this->errors;
  90. }
  91.  
  92. /**
  93.   * Return raw response array
  94.   *
  95.   * @return type
  96.   */
  97. public function getResponse()
  98. {
  99. return $this->response;
  100. }
  101.  
  102. /**
  103.   * Set username statically to avoid repetition on every usage
  104.   *
  105.   * @param String $user
  106.   */
  107. public static function setUser( $user )
  108. {
  109. self::$user = $user;
  110. }
  111.  
  112. /**
  113.   * Set password statically to avoid repetition on every usage
  114.   *
  115.   * @param String $pass
  116.   */
  117. public static function setPass( $pass )
  118. {
  119. self::$pass = $pass;
  120. }
  121.  
  122. /**
  123.   * Set String or Array of mobile numbers
  124.   *
  125.   * @param mixed $recipients
  126.   * @return Object for chaining
  127.   */
  128. public function setRecipient( $recipients )
  129. {
  130. if ( empty( $recipients ) )
  131. {
  132. $this->setError( "The parameter passed for " . __FUNCTION__ . " isn't correct" );
  133. }
  134. else
  135. {
  136. if ( is_array( $recipients ) )
  137. {
  138. $this->recipient = $recipients;
  139. }
  140. else
  141. {
  142. if ( strpos( $recipients, ',' ) )
  143. {
  144. $this->recipient = explode( ',', $recipients );
  145. }
  146. else
  147. {
  148. $this->recipient = array( $recipients );
  149. }
  150. }
  151. }
  152.  
  153.  
  154. return $this;
  155. }
  156.  
  157. /**
  158.   * Return mobile numbers
  159.   *
  160.   * @param Bool $stringfied to control the response type
  161.   * @return Mixed
  162.   */
  163. public function getRecipient( $stringfied = false )
  164. {
  165.  
  166. if ( $stringfied == true )
  167. {
  168. return implode( ',', $this->recipient );
  169. }
  170.  
  171. return $this->recipient;
  172. }
  173.  
  174. /**
  175.   * Assign the message to send
  176.   *
  177.   * @param type $message
  178.   * @return Object for chaining
  179.   */
  180. public function setMessage( $message )
  181. {
  182. if ( empty( $message ) )
  183. {
  184. $this->setError( "The message assigned is empty" );
  185. }
  186. else
  187. {
  188. $this->message = $message;
  189. }
  190.  
  191. return $this;
  192. }
  193.  
  194. /**
  195.   * Return the SMS message previously assigned
  196.   *
  197.   * @return String
  198.   */
  199. public function getMessage()
  200. {
  201. return $this->message;
  202. }
  203.  
  204. /**
  205.   * Set statically the format used to get responses
  206.   *
  207.   * @param String $format only Json & XML are valid
  208.   */
  209. public static function setFormat( $format )
  210. {
  211. if ( !in_array( $format, array( 'json', 'xml' ) ) )
  212. {
  213. throw new Exception( "This >> {$format} << is not supported, please choose xml or json" );
  214. }
  215. else
  216. {
  217. $this->format = $format;
  218. }
  219. }
  220.  
  221. /**
  222.   * Set the API address statically of whcih the system will be using to submit & retrieve data
  223.   *
  224.   * @param String $url
  225.   */
  226. public static function setURL( $url )
  227. {
  228. self::$url = url;
  229. }
  230.  
  231. /**
  232.   * Return the format previously assigned
  233.   *
  234.   * @return String
  235.   */
  236. public static function getFormat()
  237. {
  238. return self::format;
  239. }
  240.  
  241. /**
  242.   * Validate before sending
  243.   *
  244.   * @return Bool
  245.   */
  246. public function isValid()
  247. {
  248. if ( empty( self::$user ) || empty( self::$pass ) )
  249. $this->setError( "Authentication parameters (username & password) have to be assigned" );
  250. if ( empty( $this->message ) )
  251. $this->setError( "Message attribute cannot be left empty" );
  252. if ( empty( $this->recipient ) )
  253. $this->setError( "Recipient attribute cannot be left unassigned" );
  254.  
  255. return empty( $this->errors );
  256. }
  257.  
  258. /**
  259.   * Decode/Convert the JSON or XML responses retreived from API to Array
  260.   *
  261.   * @param String $string
  262.   * @return Array
  263.   */
  264. private static function decode( $string )
  265. {
  266. if ( self::$format == 'json' )
  267. {
  268. return json_decode( $string, true );
  269. }
  270. elseif ( self::$format == 'xml' )
  271. {
  272. return json_decode( json_encode( ( array ) simplexml_load_string( $string ) ), 1 );
  273. }
  274. }
  275.  
  276. /**
  277.   * Send the SMS message to the assigned recipients
  278.   * The request & responses are made through POST over HTTP
  279.   *
  280.   * @return Object for chaining
  281.   */
  282. public function send()
  283. {
  284. if ( !$this->isValid() )
  285. {
  286. throw new Exception( implode( "\n", $this->getErrors() ) );
  287. }
  288. else
  289. {
  290. $postdata = http_build_query(
  291. 'user' => self::$user,
  292. 'pass' => self::$pass,
  293. 'to' => $this->getRecipient( true ),
  294. 'message' => $this->getMessage()
  295. )
  296. );
  297.  
  298. $opts = array(
  299. 'http' =>
  300. 'method' => 'POST',
  301. 'header' => 'Content-type: application/x-www-form-urlencoded',
  302. 'content' => $postdata
  303. )
  304. );
  305.  
  306. $context = stream_context_create( $opts );
  307.  
  308. $result = file_get_contents( self::$url . self::$format, false, $context );
  309.  
  310. $this->response = self::decode( $result );
  311. }
  312.  
  313. return $this;
  314. }
  315.  
  316. /**
  317.   * Return the response code returned from the API after sending the message
  318.   *
  319.   * @return integer
  320.   */
  321. public function getResponseCode()
  322. {
  323. $result = $this->getResponse();
  324. return $result[ 'REQUEST_RESULT' ];
  325. }
  326.  
  327. /**
  328.   * Return message based ont he response code
  329.   *
  330.   * @return String
  331.   */
  332. public function getResponseMessage()
  333. {
  334. return self::$responses[ $this->getResponseCode() ];
  335. }
  336.  
  337. /**
  338.   * Dirty way to send SMS
  339.   * Required parameters to pass are
  340.   *
  341.   * @param Mixed $param
  342.   * @param Bool $return_response_code
  343.   */
  344. public static function sendSMS( $param, $return_response_code = true )
  345. {
  346. try
  347. {
  348. $class = __CLASS__;
  349. $obj = new $class;
  350.  
  351. // Optional parameters for configurations
  352. if ( $param[ 'user' ] )
  353. $class::$user = $param[ 'user' ];
  354. if ( $param[ 'pass' ] )
  355. $class::$pass = $param[ 'pass' ];
  356. if ( $param[ 'url' ] )
  357. $class::$url = $param[ 'url' ];
  358. if ( $param[ 'format' ] )
  359. $class::$url = $param[ 'format' ];
  360.  
  361. $response_code = $obj->setMessage( $param[ 'message' ] )->setRecipient( $param[ 'recipient' ] )->send()->getResponseCode();
  362.  
  363. if ( $return_response_code )
  364. return $response_code;
  365. }
  366. catch ( Exception $e )
  367. {
  368. return $e->getMessage();
  369. }
  370. }
  371.  
  372. }
  373.  
  374.  
  375.  
  376. // Configure
  377. sms::setUser( 'lebfiles' );
  378. sms::setPass( 'lebanonsms645' );
  379.  
  380. ?>

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS