5 * This class is meant to send SMS messages (with unicode support) via
6 * the Clickatell gateway and provides support to authenticate to this service,
7 * spending an vouchers and also query for the current account balance. This class
8 * use the fopen or CURL module to communicate with the gateway via HTTP/S.
10 * For more information about CLICKATELL service visit http://www.clickatell.com
14 * @author Aleksandar Markovic <mikikg@gmail.com>
15 * @copyright Copyright (c) 2004 - 2007 Aleksandar Markovic
16 * @link http://sourceforge.net/projects/sms-api/ SMS-API Sourceforge project page
17 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
27 * require_once ("sms_api.php");
29 * echo $mysms->session;
30 * echo $mysms->getbalance();
31 * // $mysms->token_pay("1234567890123456"); //spend voucher with SMS credits
32 * $mysms->send ("38160123", "netsector", "TEST MESSAGE");
42 * @link http://sourceforge.net/forum/forum.php?thread_id=1005106&forum_id=344522 How to get CLICKATELL API ID?
45 var $api_id = "YOUR_CLICKATELL_API_NUMBER";
51 var $user = "YOUR_CLICKATELL_USERNAME";
57 var $password = "YOUR_CLICKATELL_PASSWORD";
60 * Use SSL (HTTPS) protocol
66 * Define SMS balance limit below class will not work
69 var $balace_limit = 0;
72 * Gateway command sending method (curl,fopen)
75 var $sending_method = "fopen";
78 * Does to use facility for delivering Unicode messages
87 var $curl_use_proxy = false;
93 var $curl_proxy = "http://127.0.0.1:8080";
96 * Proxy username and password
99 var $curl_proxyuserpwd = "login:secretpass";
104 * 1 - Returns only intermediate statuses
105 * 2 - Returns only final statuses
106 * 3 - Returns both intermediate and final statuses
119 * Create SMS object and authenticate SMS gateway
120 * @return object New SMS object.
123 function __construct()
125 if ($this->use_ssl
) {
126 $this->base
= "http://api.clickatell.com/http";
127 $this->base_s
= "https://api.clickatell.com/http";
129 $this->base
= "http://api.clickatell.com/http";
130 $this->base_s
= $this->base
;
137 * Authenticate SMS gateway
138 * @return mixed "OK" or script die
143 $comm = sprintf("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s
, $this->api_id
, $this->user
, $this->password
);
144 $this->session
= $this->_parse_auth($this->_execgw($comm));
148 * Query SMS credis balance
149 * @return integer number of SMS credits
152 function getbalance()
154 $comm = sprintf("%s/getbalance?session_id=%s", $this->base
, $this->session
);
155 return $this->_parse_getbalance($this->_execgw($comm));
160 * @param to mixed The destination address.
161 * @param from mixed The source/sender address
162 * @param text mixed The text content of the message
163 * @return mixed "OK" or script die
166 function send($to = null, $from = null, $text = null)
169 /* Check SMS credits balance */
170 if ($this->getbalance() < $this->balace_limit
) {
171 die("You have reach the SMS credit limit!");
174 /* Check SMS $text length */
175 if ($this->unicode
== true) {
176 $this->_chk_mbstring();
177 if (mb_strlen($text) > 210) {
178 die("Your unicode message is too long! (Current lenght=".mb_strlen($text).")");
181 /* Does message need to be concatenate */
182 if (mb_strlen($text) > 70) {
183 $concat = "&concat=3";
188 if (strlen($text) > 459) {
189 die("Your message is too long! (Current lenght=".strlen($text).")");
192 /* Does message need to be concatenate */
193 if (strlen($text) > 160) {
194 $concat = "&concat=3";
200 /* Check $to and $from is not empty */
202 die("You not specify destination address (TO)!");
206 die("You not specify source address (FROM)!");
209 /* Reformat $to number */
210 $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
211 $to = str_replace($cleanup_chr, "", $to);
215 "%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s&unicode=%s%s",
220 $this->encode_message($text),
225 return $this->_parse_send($this->_execgw($comm));
229 * Encode message text according to required standard
230 * @param text mixed Input text of message.
231 * @return mixed Return encoded text of message.
234 function encode_message($text)
236 if ($this->unicode
!= true) {
238 return rawurlencode($text);
241 $uni_text_len = mb_strlen($text, "UTF-8");
244 //encode each character in text
245 for ($i=0; $i<$uni_text_len; $i++
) {
246 $out_text .= $this->uniord(mb_substr($text, $i, 1, "UTF-8"));
254 * Unicode function replacement for ord()
255 * @param c mixed Unicode character.
256 * @return mixed Return HEX value (with leading zero) of unicode character.
262 if (ord($c{0})>=0 && ord($c{0})<=127) {
266 if (ord($c{0})>=192 && ord($c{0})<=223) {
267 $ud = (ord($c{0})-192)*64 +
(ord($c{1})-128);
270 if (ord($c{0})>=224 && ord($c{0})<=239) {
271 $ud = (ord($c{0})-224)*4096 +
(ord($c{1})-128)*64 +
(ord($c{2})-128);
274 if (ord($c{0})>=240 && ord($c{0})<=247) {
275 $ud = (ord($c{0})-240)*262144 +
(ord($c{1})-128)*4096 +
(ord($c{2})-128)*64 +
(ord($c{3})-128);
278 if (ord($c{0})>=248 && ord($c{0})<=251) {
279 $ud = (ord($c{0})-248)*16777216 +
(ord($c{1})-128)*262144 +
(ord($c{2})-128)*4096 +
(ord($c{3})-128)*64 +
(ord($c{4})-128);
282 if (ord($c{0})>=252 && ord($c{0})<=253) {
283 $ud = (ord($c{0})-252)*1073741824 +
(ord($c{1})-128)*16777216 +
(ord($c{2})-128)*262144 +
(ord($c{3})-128)*4096 +
(ord($c{4})-128)*64 +
(ord($c{5})-128);
286 if (ord($c{0})>=254 && ord($c{0})<=255) { //error
290 return sprintf("%04x", $ud);
294 * Spend voucher with sms credits
295 * @param token mixed The 16 character voucher number.
296 * @return mixed Status code
299 function token_pay($token)
302 "%s/http/token_pay?session_id=%s&token=%s",
308 return $this->_execgw($comm);
312 * Execute gateway commands
315 function _execgw($command)
317 if ($this->sending_method
== "curl") {
318 return $this->_curl($command);
321 if ($this->sending_method
== "fopen") {
322 return $this->_fopen($command);
325 die("Unsupported sending method!");
329 * CURL sending method
332 function _curl($command)
335 $ch = curl_init($command);
336 curl_setopt($ch, CURLOPT_HEADER
, 0);
337 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
338 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, 0);
339 if ($this->curl_use_proxy
) {
340 curl_setopt($ch, CURLOPT_PROXY
, $this->curl_proxy
);
341 curl_setopt($ch, CURLOPT_PROXYUSERPWD
, $this->curl_proxyuserpwd
);
344 $result=curl_exec($ch);
350 * fopen sending method
353 function _fopen($command)
356 $handler = @fopen
($command, 'r');
358 while ($line = @fgets
($handler, 1024)) {
365 die("Error while executing fopen sending method!<br>Please check does PHP have OpenSSL support and check does PHP version is greater than 4.3.0.");
370 * Parse authentication command response text
373 function _parse_auth($result)
375 $session = substr($result, 4);
376 $code = substr($result, 0, 2);
378 die("Error in SMS authorization! ($result)");
385 * Parse send command response text
388 function _parse_send($result)
390 $code = substr($result, 0, 2);
392 die("Error sending SMS! ($result)");
401 * Parse getbalance command response text
404 function _parse_getbalance($result)
406 $result = substr($result, 8);
411 * Check for CURL PHP module
416 if (!extension_loaded('curl')) {
417 die("This SMS API class can not work without CURL PHP module! Try using fopen sending method.");
422 * Check for Multibyte String Functions PHP module - mbstring
425 function _chk_mbstring()
427 if (!extension_loaded('mbstring')) {
428 die("Error. This SMS API class is setup to use Multibyte String Functions module - mbstring, but module not found. Please try to set unicode=false in class or install mbstring module into PHP.");