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