I 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:
-
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 = '';
-
if ($handler) {
-
$result .= $line;
-
}
-
return $result;
-
} else {
-
Please check does PHP have OpenSSL support and check does PHP version is greater than 4.3.0.");
-
}
-
}
-
-
/**
-
* Parse authentication command response text
-
* @access private
-
*/
-
function _parse_auth ($result) {
-
if ($code!="OK") {
-
return $result;
-
}
-
else {
-
return $session;
-
}
-
}
-
-
/**
-
* Parse send command response text
-
* @access private
-
*/
-
function _parse_send ($result) {
-
if ($code!="ID") {
-
//die ("Error sending SMS! ($result)");
-
return $result;
-
} else {
-
$code = "OK";
-
return $result;
-
}
-
}
-
-
/**
-
* Parse batchID
-
* @access private
-
*/
-
function _batchID ($result) {
-
if ($code!="ID") {
-
} else {
-
$code = "OK";
-
}
-
return $batchID;
-
}
-
-
/**
-
* Parse batch start response text
-
* @access private
-
*/
-
function _parse_sendbatch ($result) {
-
/* $code = substr($result, 0, 2);
-
$batchID = substr($result,4,33);
-
if ($code!="ID") {
-
die ("Error sending SMS! ($result)");
-
} else {
-
$code = "OK";
-
}
-
return $batchID;
-
*/
-
return $result;
-
}
-
-
/**
-
* Parse getbalance command response text
-
* @access private
-
*/
-
function _parse_getbalance ($result) {
-
return (int)$result;
-
}
-
-
/**
-
* Check for CURL PHP module
-
* @access private
-
*/
-
function _chk_curl() {
-
}
-
}
-
-
/**
-
* Check for Multibyte String Functions PHP module - mbstring
-
* @access private
-
*/
-
function _chk_mbstring() {
-
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.");
-
}
-
}
-
-
// encode the message so that the
-
function _custom_encode($text){
-
-
// numbers and english letters do not need to be encoded
-
// the following characters are not supported:
-
// ^ ~ { } [ ] `
-
// and are replaced as:
-
// { -> (
-
// } -> )
-
// [ -> (
-
// ] -> )
-
//
-
// ~ and ^ are replaced by a space
-
"Ά" => "A",
-
"Β" => "B",
-
"Γ"=> "%13",
-
"Δ"=> "%10",
-
"Ε"=> "E",
-
"Έ"=> "E",
-
"Ζ"=> "Z",
-
"Η"=> "H",
-
"Ή"=> "H",
-
"Θ"=> "%19",
-
"Ι"=> "I",
-
"Ί"=> "I",
-
"Ϊ"=> "I",
-
"Κ"=> "K",
-
"Λ"=> "%14",
-
"Μ"=> "M",
-
"Ν"=> "N",
-
"Ξ"=> "%1A",
-
"Ο"=> "O",
-
"Ό"=> "O",
-
"Π"=> "%16",
-
"Ρ"=> "P",
-
"Σ"=> "%18",
-
"Τ"=> "T",
-
"Υ"=> "Y",
-
"Ύ"=> "Y",
-
"Ϋ"=> "Y",
-
"Φ"=> "%12",
-
"Χ"=> "X",
-
"Ψ"=> "%17",
-
"Ω"=> "%15",
-
"Ώ"=> "%15",
-
" "=>urlencode(" "),
-
"α" => "A",
-
"ά" => "A",
-
"β" => "B",
-
"γ"=> "%13",
-
"δ"=> "%10",
-
"ε"=> "E",
-
"έ"=> "E",
-
"ζ"=> "Z",
-
"η"=> "H",
-
"ή"=> "H",
-
"θ"=> "%19",
-
"ι"=> "I",
-
"ί"=> "I",
-
"ϊ"=> "I",
-
"ΐ"=> "I",
-
"κ"=> "K",
-
"λ"=> "%14",
-
"μ"=> "M",
-
"ν"=> "N",
-
"ξ"=> "%1A",
-
"ο"=> "O",
-
"ό"=> "O",
-
"π"=> "%16",
-
"ρ"=> "P",
-
"σ"=> "%18",
-
"ς"=> "%18",
-
"τ"=> "T",
-
"υ"=> "Y",
-
"ύ"=> "Y",
-
"ϋ"=> "Y",
-
"ΰ"=> "Y",
-
"φ"=> "%12",
-
"χ"=> "X",
-
"ψ"=> "%17",
-
"ω"=> "%15",
-
"ώ"=> "%15",
-
'~'=>'%1B3D',
-
"!"=>urlencode("!"),
-
"@"=>urlencode("@"),
-
"#"=>urlencode("#"),
-
"$"=>urlencode("$"),
-
"%"=>urlencode("%"),
-
'^'=>'%1B14',
-
"&"=>urlencode("&"),
-
"*"=>urlencode("*"),
-
"("=>urlencode("("),
-
")"=>urlencode(")"),
-
"-"=>"-",
-
"+"=>urlencode("+"),
-
'`'=>urlencode("'"),
-
"_"=>"_",
-
"="=>urlencode("="),
-
"["=>urlencode("("),
-
"]"=>urlencode(")"),
-
";"=>urlencode(";"),
-
'{'=>urlencode("("),
-
'}'=>urlencode(")"),
-
"|"=>urlencode("|"),
-
";"=>urlencode(";"),
-
":"=>urlencode(":"),
-
'"'=>urlencode('"'),
-
"'"=>urlencode("'"),
-
"<"=>urlencode("<"),
-
">"=>urlencode(">"),
-
","=>urlencode(","),
-
"."=>".",
-
"?"=>urlencode("?"),
-
"/"=>urlencode("/"),
-
//"€"=>urlencode("€")
-
"€"=>'%1B65'
-
);
-
-
// convert to uppercase (for english letters that are not translated by the function above)
-
return $text;
-
}
-
-
}
This is a customized class adapted for cakephp and for greek characters, but will work for latin chatacters as well. I dont remember the writer of the original class, if you know who he is, please contact me.
You will need to add this file to this location in your cakephp app folder: controllers/components/sendsms.php
Now, in you sms_controller.php add the following line:
Now, in order to see your remaining credit use the sendsms component in your sms_controller.php like this:
-
function getBalance(){
-
$balance = $this->Sendsms->getbalance();
-
$this->set('balance',$balance);
-
}
or, to dispatch an sms:
-
$results = $this->Sendsms->send($contact_to['Contact']['mobile'],$from,$message);
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.