<?php
/**
* ClickATell SMS API
*
* This library provides generic API for ClickATell SMS Service in an uniform way.
*
* @package ClickATell SMS API
* @version 1.0
* @category Library
* @author Utsav Handa < handautsav at hotmail dot com >
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*
* @changelog
* -- 2009-01-01 Initial Implementation
*
*
*/
/** License
*
* Copyright (c) 2009 Utsav Handa <handautsav at hotmail dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/**
* Usage Example ::
*
* require_once('class-clickatellsms-api.php');
* $sms_obj = new ClickATellSMS('USERNAME', 'PASSWORD', 'API_ID', 'SENDER_ID');
* $sms_obj -< sendSMS('NUMBER', 'MESSAGE');
*
**/
/** Class to deal with Timezone Conversion */
/** CLass 'ClickaTell' SMS Provider API */
class ClickATellSMS {
private $_sending_methods = array('curl_init', 'file_get_contents');
private $_sending_method = null;
private $_sendURL = 'http://api.clickatell.com/http/sendmsg?api_id=%s&user=%s&password=%s&from=%s&text=%s&to=%s&callback=0&concat=1';
private $_max_message_length = '161';
private $_default_useragent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5';
private $_username = null;
private $_password = null;
private $_apiid = null;
private $_senderid = null;
public $response_message = null;
public $error_id = 0;
public $error_message = null;
/** Main Class Object constructor */
function __construct($username = null, $password = null, $apiid = null, $senderid = null) {
/** Set Param(s) */
$this->_username = ( $username ? $username : '' );
$this->_password = ( $password ? $password : '' );
$this->_apiid = ( $apiid ? $apiid : '' );
$this->_senderid = ( $senderid ? $senderid : '' );
/** Select Suuported Method */
foreach ($this->_sending_methods as $sending_method) {
if (function_exists($sending_method)) {
$this->_sending_method = $sending_method;
break;
}
}
}
/** Message Sending Method */
public function sendSMS($to = null, $text = null) {
/*************************/
/**** Validate Inputs ****/
/*************************/
/** Default Message Length */
if(strlen($text) > $this->_max_message_length) {
$this->error_message = 'ERROR: Message length exceeds maximum permissible limit';
return 0;
}
/** 'To' Empty */
if (empty($to)) {
$this->error_message = 'ERROR: destination not specified';
return 0;
}
/** Sanitize Inputs */
$text = stripslashes($text);
$text = str_replace("\r", "", $text);
/** Cleanup Message */
$cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
$to = str_replace($cleanup_chr, '', $to);
/** Build URL */
$comm = sprintf (
$this->_sendURL,
$this->_apiid,
$this->_username,
$this->_password,
$this->_senderid,
rawurlencode($text),
rawurlencode($to)
);
return ($this->_execgw($comm));
}
/** Execute Command */
private function _execgw($command = null) {
$result = '';
/** Validate 'sending method */
if (!function_exists($this->_sending_method)) {
$this->error_message = "ERROR: '".$this->_sending_method."' not supported in PHP";
return 0;
}
/** Execute Method Call */
if ($this->_sending_method == "file_get_contents") {
$result = $this->_file_get_contents($command);
} elseif ($this->_sending_method == "curl_init") {
$result = $this->_curl_init($command);
}
/** Parse API Result */
if ($result) {
$status = $status_msg = null;
if (@preg_match('/(.*?):(.*?)$/', $result, $matches)) {
$status = $matches[1];
$status_msg = $matches[2];
}
/** Process Result */
if (stristr($status, 'err')) { /** ERROR */
$this->response_message = $status;
list($this->error_id, $this->error_message) = explode(',', $status_msg);
} else { /** OK */
$this->response_message = $status;
$this->error_id = 0;
}
}
return ( $this->error_id ? 0 : 1 );
}
/** Curl Method */
private function _curl_init($url = '') {
/** Preparing CURL instance */
$o_ch = curl_init();
/** Starting Processing */
curl_setopt ($o_ch, CURLOPT_URL, $url);
curl_setopt ($o_ch, CURLOPT_USERAGENT, $this->_default_useragent);
curl_setopt ($o_ch, CURLOPT_HEADER, 0);
curl_setopt ($o_ch, CURLOPT_RETURNTRANSFER, 1);
$s_html = curl_exec ($o_ch);
curl_close ($o_ch);
unset($o_ch);
/** Cleaning HTML */
for ($ascii = 0; $ascii <= 9; $ascii++) $s_html = str_replace(chr($ascii), "", $s_html);
for ($ascii = 11; $ascii < 32; $ascii++) $s_html = str_replace(chr($ascii), "", $s_html);
for ($ascii = 127; $ascii <= 255; $ascii++) $s_html = str_replace(chr($ascii), "", $s_html);
return $s_html;
}
/** fopen Method */
private function _file_get_contents($url = null) {
/** Fetch Contents */
return file_get_contents($url);
}
}
?>