Send SMS With PHP Code

SMS (Short Message Service) is the technology behind the popular text message. SMS is usually used by cellular phones. Some website owners have been tapping into the power and popularity of SMS to allow their users to send SMS messages (i.e. text messages) from their websites. You can offer the same benefit to your website visitors. Use the PHP scripting language to add SMS functionality to your website. You will use PHP to prepare the request to send a SMS message and then use the CURL library installed on the server to send it.

1

Create an account with one of the SMS gateway providers, such as Clickatell, Ozeki NG and TM4B SMS Gateway (see Resources). The gateway provider will be in charge of sending your requests for SMS messages. As with sending text messages from cell phones, you will need a service provider in order to send SMS messages from your website. The gateway provider will provide you with a paid solution for sending SMS messages from your website.

2

Start a new blank file in a web-authoring application, such as Dreamweaver, or a plain text-editing application like Notepad. Most web-authoring application uses a WSIWYG (What You See is What You Get) environment, in which you just add the visual elements to the document and the program will automatically generate the code. You want to type in your own code in this project, so switch to “Code” or “HTML” view of the program. Also, don’t use a word processor as a text-editing program. Word processors, like Microsoft Word, add extra formatting code that will interfere with the PHP code.

3

Create the web form that will work as the user interface for entering in the information for sending the text message. Visitors to your website will type in the information for the text message. You will then collect this information and send it in the request to the SMS gateway.

 
<html>
 
<head>
 
 
 
<body>
 
<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">
 
 
Sender:
 
 
Receiver:
 
Message: <p><textarea name="message" rows="20 cols="80">
 
Click in this box and type in your text message.
#
4
 
Type in the PHP code to prepare the request to the messaging service's gateway. You have to send the gateway an HTTP request that passes along the details of the SMS message. The request should look like this:
 
 
 
"<?php
 
 
 
\\\\Takes the information from the form and save it to variables
 
$sender = $_POST['sender'];
 
$receiver = $_POST['receiver'];
 
$message = $_POST['message'];
 
 
 
\\\\Prepare the request
 
$request = "";
 
$param["username"] = "username"; //the account username provided by gateway provider
 
$param["password"] = "password"; //the account password provided by gateway provider
 
$param["msg"] = $message; //the text message that will be sent
 
$param["to"] = $receiver; //the recipient of the text message
 
$param["from"] = $receiver; //the message sender
 
$param["route"] = "frst"; //this means we are sending the message first class
 
$param["sim"] = "yes";
 
 
 
foreach($param as $key=>$val){
 
$request.= $key."=".urlencode($val);
 
$request.= "&";
 
}
 
 
 
$request = substr($request, 0, strlen($request)-1);
 
?>"
 
 
 
Change the param values above to meet your specific details.
#
5
 
Type in the code to actually send the SMS message. PHP has a CURL library included that handles these types of things.
 
 
 
"<?php
 
 
 
$url = "{add the url to the gateway API}"; //the gateway's interface provided by the gateway provider
 
 
 
$ch = curl_init;
 
curl_setopt($ch, CURLOPT_URL, $url);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 
curl_setopt($ch, CURLOPT_POST, 1);
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
 
$response = curl_exec($ch);
 
curl_close($ch);
 
?>
 
 
 
</body>
 
</head>
 
</html>

Do not change any of the code above other than removing the quotation marks from the first and last line in the code and adding the URL to the SMS gateway provider’s API interface. Check with the provider for this information. Save the document with the PHP file extension and upload it to your web server.

Do you remember back in the earlly 90’s, before everyone had cell phone, many people used to have ‘beepers’ or ‘pagers’. You would send a one-way text message to it, usually provide a phone number where to call you back and the person would receive it and call you back.

It’s amazing that now when everyone has a smartphone, can have sophisticated apps, full web browser, not to mention email right on their phone, people still using this fossilized technology to communicate?

It usually surprises people when I say I have never used an SMS to either send or receive a message in my life (not counting occasional spam SMS which I don’t read anyway)

I know, SMS is very popular among kids, I have kids, so I know.

You assume that everyone has a smartphone, and that everyone who does have a smartphone can send e-mails cheaply from everywhere in the world. I know both assumptions to be false :slight_smile:

I prefer text messages over e-mails for short messages. It simply requires less overhead, and I don’t have to know in advance if the person I’m sending it to has a smartphone which is set up to receive e-mails.

You may be forgetting that this fossilized technology has many advantages over the smartest inventions available today: it’s simple, it’s easy, it’s cheap, it’s available everywhere, supported by almost any phone device, it’s immediate and it works. You may think as hard as you can but you will not invent anything that is more practical for the majority of people for just one purpose of sending quick and unintrusive messages. While you may perceive sophistication as something positive there are a lot of people who prefer simplicity in technology - simply because they do other things in life and have no time they can spare to master all that newest and best electronic stuff.