CakePHP - Clickatell api for sending sms
Friday, February 9th, 2007I use the clickatell api for sending sms with cakephp. All I did was to grab a php class for the clickatell api which is shown below:
PHP:
-
class sendsmsComponent extends Object
-
{
-
-
var $api_id = "your_clickatell_api_id";
-
var $user = "your_clickatell_username";
-
var $password = "you_clickatell_password";
-
var $use_ssl = false;
-
-
/**
-
* Define SMS balance limit below class will not work
-
* @var integer
-
*/
-
var $balace_limit = 0;
-
-
/**
-
* Gateway command sending method (curl,fopen)
-
* @var mixed
-
*/
-
var $sending_method = "fopen";
-
var $unicode = false;
-
-
/**
-
* Optional CURL Proxy
-
* @var bool
-
*/
-
var $curl_use_proxy = false;
-
var $curl_proxy = "http://127.0.0.1:8080";
-
var $curl_proxyuserpwd = "login:secretpass";
-
-
/**
-
* Callback
-
* 0 - Off
-
* 1 - Returns only intermediate statuses
-
* 2 - Returns only final statuses
-
* 3 - Returns both intermediate and final statuses
-
* @var integer
-
*/
-
var $callback = 0;
-
var $session;
-
var $batch;
-
-
/**
-
* Class constructor
-
* Create SMS object and authenticate SMS gateway
-
* @return object New SMS object.
-
* @access public
-
*/
-
-
function startup(&$controller)
-
{
-
// This method takes a reference to the controller which is loading it.
-
// Perform controller initialization here.
-
-
if ($this->use_ssl) {
-
$this->base = "http://api.clickatell.com/http";
-
$this->base_s = "https://api.clickatell.com/http";
-
$this->base_batch = "http://api.clickatell.com/http_batch";
-
$this->base_s_batch = "https://api.clickatell.com/http_batch";
-
-
} else {
-
$this->base = "http://api.clickatell.com/http";
-
$this->base_s = $this->base;
-
$this->base_batch = "http://api.clickatell.com/http_batch";
-
$this->base_s_batch = $this->base_batch;
-
-
}
-
$this->_auth();
-
}
-
-
/*
-
function sms () {
-
if ($this->use_ssl) {
-
$this->base = "http://api.clickatell.com/http";
-
$this->base_s = "https://api.clickatell.com/http";
-
} else {
-
$this->base = "http://api.clickatell.com/http";
-
$this->base_s = $this->base;
-
}
-
$this->_auth();
-
}
-
*/
-
-
/**
-
* Authenticate SMS gateway
-
* @return mixed "OK" or script die
-
* @access private
-
*/
-
function _auth() {
-
$comm = sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $this->api_id, $this->user, $this->password);
-
$this->session = $this->_parse_auth ($this->_execgw($comm));
-
}
-
-
/**
-
* Query SMS credis balance
-
* @return integer number of SMS credits
-
* @access public
-
*/
-
function getbalance() {
-
return $this->_parse_getbalance ($this->_execgw($comm));
-
}
-
-
/**
-
* Send SMS message
-
* @param to mixed The destination address.
-
* @param from mixed The source/sender address
-
* @param text mixed The text content of the message
-
* @return mixed "OK" or script die
-
* @access public
-
*/
-
function send($to=null, $from=null, $text=null) {
-
-
/* Check SMS credits balance */
-
if ($this->getbalance() <$this->balace_limit) {
-
};
-
-
/* Check SMS $text length */
-
if ($this->unicode == true) {
-
$this->_chk_mbstring();
-
}
-
/* Does message need to be concatenate */
-
$concat = "&concat=3";
-
} else {
-
$concat = "";
-
}
-
} else {
-
}
-
/* Does message need to be concatenate */
-
$concat = "&concat=3";
-
} else {
-
$concat = "";
-
}
-
}
-
-
/* Check $to and $from is not empty */
-
}
-
}
-
$from = $this->_custom_encode($from);
-
-
/* Reformat $to number */
-
-
/* Send SMS now */
-
$this->base,
-
$this->session,
-
$from,
-
$this->encode_message($text),
-
$this->callback,
-
$this->unicode,
-
$concat
-
);
-
$myvar = $this->_parse_send ($this->_execgw($comm));
-
return $myvar;
-
}
-
-
function sendbatch($to=null, $from=null, $text=null){
-
/* Check SMS credits balance */
-
if ($this->getbalance() <$this->balace_limit) {
-
};
-
-
/* Check SMS $text length */
-
if ($this->unicode == true) {
-
$this->_chk_mbstring();
-
}
-
/* Does message need to be concatenate */
-
$concat = "&concat=3";
-
} else {
-
$concat = "";
-
}
-
} else {
-
}
-
/* Does message need to be concatenate */
-
$concat = "&concat=3";
-
} else {
-
$concat = "";
-
}
-
}
-
-
/* Check $to and $from is not empty */
-
}
-
}
-
$from = $this->_custom_encode($from);
-
-
/* get batchID now */
-
$this->base_batch,
-
$this->session,
-
$this->encode_message($text),
-
$this->callback,
-
$this->unicode,
-
$concat
-
);
-
$this->batch = $this->_batchID ($this->_execgw($comm));
-
-
$this->base_batch,
-
$this->session,
-
$this->batch
-
);
-
$myvar = $this->_parse_sendbatch ($this->_execgw($comm));
-
return $myvar;
-
}
-
-
/**
-
* Encode message text according to required standard
-
* @param text mixed Input text of message.
-
* @return mixed Return encoded text of message.
-
* @access public
-
*/
-
function encode_message ($text) {
-
if ($this->unicode != true) {
-
//standard encoding
-
//return rawurlencode($text);
-
return $this->_custom_encode($text);
-
} else {
-
//unicode encoding
-
$out_text = "";
-
-
//encode each character in text
-
for ($i=0; $i<$uni_text_len; $i++) {
-
}
-
-
return $out_text;
-
}
-
}
-
-
/**
-
* Unicode function replacement for ord()
-
* @param c mixed Unicode character.
-
* @return mixed Return HEX value (with leading zero) of unicode character.
-
* @access public
-
*/
-
function uniord($c) {
-
$ud = 0;
-
$ud = false;
-
}
-
-
/**
-
* Spend voucher with sms credits
-
* @param token mixed The 16 character voucher number.
-
* @return mixed Status code
-
* @access public
-
*/
-
function token_pay ($token) {
-
$this->base,
-
$this->session,
-
$token);
-
-
return $this->_execgw($comm);
-
}
-
-
/**
-
* Execute gateway commands
-
* @access private
-
*/
-
function _execgw($command) {
-
if ($this->sending_method == "curl")
-
return $this->_curl($command);
-
if ($this->sending_method == "fopen")
-
return $this->_fopen($command);
-
}
-
-
/**
-
* CURL sending method
-
* @access private
-
*/
-
function _curl($command) {
-
$this->_chk_curl();
-
$ch = curl_init ($command);
-
curl_setopt ($ch, CURLOPT_HEADER, 0);
-
curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
-
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
-
if ($this->curl_use_proxy) {
-
curl_setopt ($ch, CURLOPT_PROXY, $this->curl_proxy);
-
curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $this->curl_proxyuserpwd);
-
}
-
$result=curl_exec ($ch);
-
curl_close ($ch);
-
return $result;
-
}
-
-
/**
-
* fopen sending method
-
* @access private
-
*/
-
function _fopen($command) {
-
$result =