Sending SMS from your website
I have noticed that the number of websites that have the option to announce their users via SMS that an event is due to happen has increased. This article explains how to easily add this option to your website (which, of course, is not free).
The SMSs will be sent via the ClickATell gateway. First, you will have to create an accout at http://www.clickatell.com/login.php?csite=clickatell . After you sign up on ClickATell you will have to purchase credits in order to start sending SMSs (this can be done from the Billing category). The site offers a lot of payments methods from which you can choose the one that you prefer the most ( I used MoneyBookers ).
If you go to the ”Manage my Products” page, you will have the option to add a new connection to the ClickATell API. You will probably need a HTTP connection. You will get an API ID, which will later be used on your website.
This is all you have to do on ClickATell. Next you will have to update your website and connect it to the ClickATell API. I have written an open-source PHP class that is really easy to use and that can help you send SMSs really easy. You may download it from this link (Class for sending SMS from your website) . The ZIP file contains an example showing how to use the class. There are only 3 things that you have to do:
1. Import the class in your website
include("sms.class.php");
2. Instantiate class
$cls = new sms("Your_username", "Your_password", "Your_API_ID");
3. Send SMS
$result = $cls->send("PHONE_NO", "Message to be sent");
The class contains the following methods: login and send are public methods and verify and adjust are private methods, used only within the class.
1. The login() method builds a link depinding on the information the user has entered and then opens that link by using the file function available in PHP ( http://www.php.net/file ) . If the file was successfully opened, then it stores into an array each line of content. It verifies this the first line and stores the SESSION ID returned from ClickATell, which will later be used to send a SMS.
// Build the link that will be used to log in to ClickATell website
$login_link = $this->c_api_url.”/http/auth?user=”. $this->c_username.”&password=”. $this->c_password.”&api_id=”.$this->c_api_id;
// Open link and get response
$response = file($login_link);
if($response == FALSE)
return “Could not connect to the ClickATell website”;
// If success, check the response from server, which is located on the first line = $response[0]
$s_data = split(”:”,$response[0]);
$this->logged_in = $s_data[0] == “OK” ? true : false;
if(!$this->logged_in)
return “The username and password for ClickATell do not match.”;
else
$this->sess_id = trim($s_data[1]);
2. The send() method is used to send a SMS via the ClickATell API. The same method of opening a file as in the login() method is used, only that this time you pass in the URL opened the session id stored after logging in. You verify the content of the file opened to see if the message has been sent.
if($this->logged_in) {
$this->to = $to;
$this->message = $message;
// Adjust the phone no. and message
$this->adjust();
// Verify the information sent to the send() method
if(!$this->verify())
return $this->err;
// Build the link the will be used to send the message
$send_url = $this->c_api_url.”/http/sendmsg?session_id=”. $this->sess_id.”&to=”.$this->to.”&text=”. $this->message;
// Send the message
$send_msg = file($send_url);
if($send_msg == FALSE)
return “FATAL ERROR! Could not send the message. Please try again later”;
// Read response from server
$response_send = split(”:”,$send_msg[0]);
// Check if the message has been successfully sent
$is_ok = $response_send[0] == “ID” ? true : false;
if($is_ok)
return “The message has been successfully sent”.”<br />”;
else
return “Your message could not be sent. <br />Please contact the webmaster of the site for more information”;
} else return “You are not logged in into your ClickATell account”;
3. The private method verify() checks if the information passed to the send() method is valid. The phone no. and the message should not be empty ( http://www.php.net/manual/en/function.empty.php ). Also the length of the message should not exceed 255 characters.
$this->err = “”;
$is_ok = true;
// Verify the phone no.
if(empty($this->to)) {
$this->err .= “Please enter the receiver’s phone no.”;
$is_ok = false;
}
// Verify the message
if(empty($this->message)) {
$this->err .= “Please enter the message of the SMS.”;
$is_ok = false;
} elseif(strlen($this->message)>255) {
$this->err .= “Please enter a valid message (max. 255 characters).”;
$is_ok = false;
}
4. The private method adjust() solves some possible issues with the phone no. and the message. If the phone no. contains characters such as ‘(’, ‘)’, ‘+’ (e.g. +(40)74xxxxxxx), then it strips those characters from it, making it valid. The message will be passed in a link, so it needs to be properly encoded ( http://www.php.net/manual/en/function.urlencode.php ). Also, to prevent spamming or confusions, if the message contains HTML tags, they are stripped from the string using the strip_tags function in PHP ( http://www.php.net/manual/en/function.strip-tags.php ).
//Adjust the phone no.
$repl_chars = array (”+”, ” “, “(”, “)”, “\r”, “\n”, “\r\n”);
$this->to = str_replace($repl_chars, “”, $this->to);
//Adjust the message
$this->message = strip_tags($this->message);
$this->message = urlencode($this->message);
This article explains some PHP functions that are vital to working with opening files and reading their content, validating content, etc. , and it also provides useful information about how to easily integrate SMS in your website.
You may use it in a lot of ways, for instance you could set up a cronjob on your server to send SMS to your clients announcing them that they have just received a new private message from another user. If you are designing a web application for a business, you may this class to allow an employee to send an SMS to another employee.
October 10th, 2007 at 9:50 am
The class is written in an efficient manner taking in to account all possible bugs.
Explained and code well.
Thanks
October 21st, 2007 at 8:11 am
Muy interesante, creo que lo implementaré para enviar mediante Telcel y Isacell :O
October 22nd, 2007 at 12:15 pm
Great it’s help me out thanks,
October 27th, 2007 at 3:52 pm
Kindly let me know in detail about the charges for sending sms through web site.
Thanks & Regards
January 9th, 2008 at 3:46 am
I great you for your publication on sms sending. It’s very good and I was just looking for something like this. Please can you explain (facilitate) a little more the installation process of your script for beginners!
Best regards.
February 10th, 2008 at 4:40 pm
Ma intereseaza daca pe situl meu ai putea implementa un sistem prin care clientii care sunt inregistrati sa poata trimite un numar stabilit de mine de sms suportate de mine iar sms sa aiba inclus un mesaj publicitar stabilit de mine!
Astept raspuns cit se poate de urgent
Multumesc!
May 2nd, 2008 at 12:09 pm
sir,
please send me deatials how to send sms to clients , when they login in our home page, with an welcome message.
# 91 22 98 21 88 66 88 mumbai.
prasanta k.
October 2nd, 2008 at 10:52 am
what is the cost of an SMS in cents - could you let me know
thanks
Ignatius
February 24th, 2009 at 9:36 pm
I wanna know if there’s a way to use this class to send to more than one phone number. I wanna manage groups and send sms to groups
June 5th, 2009 at 4:18 am
[…] View Tutorial No Comment var addthis_pub=”izwan00″; BOOKMARK This entry was posted on Friday, June 5th, 2009 at 6:48 am and is filed under Php Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. […]
October 29th, 2009 at 8:34 am
[…] 31)Sending SMS from your website Learn how to send SMS from your website using the ClickATell API. […]
November 3rd, 2009 at 12:15 am
[…] 31)Sending SMS from your website Learn how to send SMS from your website using the ClickATell API. […]