Hi Reader's,
Welcome to FindNerd, today we are going to discuss how to implement SMS Gateway in cake php.
In web applications sending SMS Gateway is a very important feature for sending SMS in a application.
So clickatell SMS Gateway is very simple and easy to implement SMS functionality using php.
If you want implement SMS gateway, then firstly you have create your clickatell account.
You can see below link for creating a clickatell account.
https://www.clickatell.com/developers/sms-gateway/
With the help of above URL you can fill your basic details and create a free account. After creating account you will get email with username and password.
So when you will login with username and password then you will get below detail:
VALID SAMPLE CODE
http://api.clickatell.com/http/sendmsg?user=USERNAME&password=PASSWORD&api_id=3600992&to=918888858888&text=Message
In above URL you can fill your username, password and fill a valid number in which you want send the message.
Now you can see below code:
<?php
/**
* SMS component for CakePHP using the Clickatell HTTP API interface.
* @author Doug Bromley <doug.bromley@gmail.com>
* @copyright Doug Bromley
* @link http://www.cakephp.org CakePHP
* @link http://www.clickatell.com Clickatell
*
*/
class ClickatellComponent extends Component {
/**
* The username for the Clickatell API
* @access public
* @var string
*/
var $api_user = '';
/**
* The password for the Clickatell API
* @access public
* @var string
*/
var $api_pass = '';
/**
* Who will be shown as the sender of the text at the receivers handset.
* @access public
* @var string
*/
var $api_from = '';
/**
* The API id for this product.
* @access public
* @var string
*/
var $api_id = 3600992;
/**
* The Clickatell XML API url
*/
const API_XML_URL = 'http://api.clickatell.com/xml/xml';
/**
* The Clickatell HTTP API url for sending GET or POST requests too.
*/
const API_HTTP_URL = 'http://api.clickatell.com/http/';
/**
* Post a message to the Clickatell servers for the number provided
* @param string $tel The telephone number in international format. Not inclduing a leading "+" or "00".
* @param string $message The text message to send to the handset.
* @return string
* @see SmsComponent::api_id
* @see SmsComponent::api_user
* @see SmsComponent::api_pass
* @see SmsComponent::api_from
*/
function postSms($tel, $message) {
$message = trim(str_replace(array("\n", "\r"), ' ', $message));
$postdata = http_build_query(
array(
'user' => $this->api_user,
'password' => $this->api_pass,
'api_id' => $this->api_id,
'MO'=>1,
'from' => $this->api_from,
'to' => $tel,
'concat' => '3',
'text' => $message
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$response = file_get_contents(self::API_HTTP_URL.'sendmsg', false, $context);
return $response;
}
/**
* Get the balance of your Clickatell account.
* @return float
* @see SmsComponent::api_id
* @see SmsComponent::api_user
* @see SmsComponent::api_pass
* @see SmsComponent::api_from
*/
function queryBalance() {
$postdata = http_build_query(
array(
'api_id' => $this->api_id,
'user' => $this->api_user,
'password' => $this->api_pass
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$response = file_get_contents(self::API_HTTP_URL.'getbalance', false, $context);
return $response;
}
}
?>
In the above given code you have to put your credential and use it where you want. You can also check your clickatell balance that you have, SMS balance or not to send SMS.
If you also want to check record that how much messages you have send, then you can check with clickatell account.
So this is the simple way for implementing a SMS gateway in cakephp.
1 Comment(s)