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.
124 if ($this->use_ssl
) {
125 $this->base
= "http://api.clickatell.com/http";
126 $this->base_s
= "https://api.clickatell.com/http";
128 $this->base
= "http://api.clickatell.com/http";
129 $this->base_s
= $this->base
;
135 * Authenticate SMS gateway
136 * @return mixed "OK" or script die
140 $comm = sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s
, $this->api_id
, $this->user
, $this->password
);
141 $this->session
= $this->_parse_auth ($this->_execgw($comm));
145 * Query SMS credis balance
146 * @return integer number of SMS credits
149 function getbalance() {
150 $comm = sprintf ("%s/getbalance?session_id=%s", $this->base
, $this->session
);
151 return $this->_parse_getbalance ($this->_execgw($comm));
156 * @param to mixed The destination address.
157 * @param from mixed The source/sender address
158 * @param text mixed The text content of the message
159 * @return mixed "OK" or script die
162 function send($to=null, $from=null, $text=null) {
164 /* Check SMS credits balance */
165 if ($this->getbalance() < $this->balace_limit
) {
166 die ("You have reach the SMS credit limit!");
169 /* Check SMS $text length */
170 if ($this->unicode
== true) {
171 $this->_chk_mbstring();
172 if (mb_strlen ($text) > 210) {
173 die ("Your unicode message is too long! (Current lenght=".mb_strlen ($text).")");
175 /* Does message need to be concatenate */
176 if (mb_strlen ($text) > 70) {
177 $concat = "&concat=3";
182 if (strlen ($text) > 459) {
183 die ("Your message is too long! (Current lenght=".strlen ($text).")");
185 /* Does message need to be concatenate */
186 if (strlen ($text) > 160) {
187 $concat = "&concat=3";
193 /* Check $to and $from is not empty */
195 die ("You not specify destination address (TO)!");
198 die ("You not specify source address (FROM)!");
201 /* Reformat $to number */
202 $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
203 $to = str_replace($cleanup_chr, "", $to);
206 $comm = sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s&unicode=%s%s",
211 $this->encode_message($text),
216 return $this->_parse_send ($this->_execgw($comm));
220 * Encode message text according to required standard
221 * @param text mixed Input text of message.
222 * @return mixed Return encoded text of message.
225 function encode_message ($text) {
226 if ($this->unicode
!= true) {
228 return rawurlencode($text);
231 $uni_text_len = mb_strlen ($text, "UTF-8");
234 //encode each character in text
235 for ($i=0; $i<$uni_text_len; $i++
) {
236 $out_text .= $this->uniord(mb_substr ($text, $i, 1, "UTF-8"));
244 * Unicode function replacement for ord()
245 * @param c mixed Unicode character.
246 * @return mixed Return HEX value (with leading zero) of unicode character.
249 function uniord($c) {
251 if (ord($c{0})>=0 && ord($c{0})<=127)
253 if (ord($c{0})>=192 && ord($c{0})<=223)
254 $ud = (ord($c{0})-192)*64 +
(ord($c{1})-128);
255 if (ord($c{0})>=224 && ord($c{0})<=239)
256 $ud = (ord($c{0})-224)*4096 +
(ord($c{1})-128)*64 +
(ord($c{2})-128);
257 if (ord($c{0})>=240 && ord($c{0})<=247)
258 $ud = (ord($c{0})-240)*262144 +
(ord($c{1})-128)*4096 +
(ord($c{2})-128)*64 +
(ord($c{3})-128);
259 if (ord($c{0})>=248 && ord($c{0})<=251)
260 $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);
261 if (ord($c{0})>=252 && ord($c{0})<=253)
262 $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);
263 if (ord($c{0})>=254 && ord($c{0})<=255) //error
265 return sprintf("%04x", $ud);
269 * Spend voucher with sms credits
270 * @param token mixed The 16 character voucher number.
271 * @return mixed Status code
274 function token_pay ($token) {
275 $comm = sprintf ("%s/http/token_pay?session_id=%s&token=%s",
280 return $this->_execgw($comm);
284 * Execute gateway commands
287 function _execgw($command) {
288 if ($this->sending_method
== "curl")
289 return $this->_curl($command);
290 if ($this->sending_method
== "fopen")
291 return $this->_fopen($command);
292 die ("Unsupported sending method!");
296 * CURL sending method
299 function _curl($command) {
301 $ch = curl_init ($command);
302 curl_setopt ($ch, CURLOPT_HEADER
, 0);
303 curl_setopt ($ch, CURLOPT_RETURNTRANSFER
,1);
304 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER
,0);
305 if ($this->curl_use_proxy
) {
306 curl_setopt ($ch, CURLOPT_PROXY
, $this->curl_proxy
);
307 curl_setopt ($ch, CURLOPT_PROXYUSERPWD
, $this->curl_proxyuserpwd
);
309 $result=curl_exec ($ch);
315 * fopen sending method
318 function _fopen($command) {
320 $handler = @fopen
($command, 'r');
322 while ($line = @fgets
($handler,1024)) {
328 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.");
333 * Parse authentication command response text
336 function _parse_auth ($result) {
337 $session = substr($result, 4);
338 $code = substr($result, 0, 2);
340 die ("Error in SMS authorization! ($result)");
346 * Parse send command response text
349 function _parse_send ($result) {
350 $code = substr($result, 0, 2);
352 die ("Error sending SMS! ($result)");
360 * Parse getbalance command response text
363 function _parse_getbalance ($result) {
364 $result = substr($result, 8);
369 * Check for CURL PHP module
372 function _chk_curl() {
373 if (!extension_loaded('curl')) {
374 die ("This SMS API class can not work without CURL PHP module! Try using fopen sending method.");
379 * Check for Multibyte String Functions PHP module - mbstring
382 function _chk_mbstring() {
383 if (!extension_loaded('mbstring')) {
384 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.");