Use your website to send SMS

You can add SMS functionality to your website. This method presents how to use a PHP script to communicate with Ozeki SMS Gateway's HTTP API. First you need to enable the PHP scripting support on your webserver.

Download php sms sending example: sendsms.zip

The presented example (php sms sending script) shows how message sending can work from your website by clicking 'Send'. The idea uses a HTML form. The user can fill this HTML form and click 'Send'. The PHP script processes the two variables in the textboxes called 'Recipient' and 'Message'. It also sends the generated HTTP request URL to the HTTP API of Ozeki SMS Gateway (Figure 1). Your webserver and Ozeki SMS Gateway can be on two different machines or on the same machine as well.

how an sms can be sent from your website
Figure 1 - How an SMS can be sent from your website

You should install Ozeki SMS Gateway in the first place and check if you can manually send SMS messages from it. Then please download the HTML+PHP script to create and use the SMS sending form.

Ozeki SMS Gateway can be obtained by
opening the download page:
Download Ozeki SMS Gateway!

Create the HTML Form for sms sending

Please save sendsms.html to get started with this solution. You should save it in the 'WWW' directory of your webserver. What does it contain? It contains the HTML form with a recipient and message textbox, plus a 'Send' button (Figure 2).


<html>
 <body>
   <h1>My SMS form</h1>
   <form method=post action='sendsms.php'>
   <table border=0>
   <tr>
     <td>Recipient</td>
     <td><input type='text' name='recipient'></td>
   </tr>
   <tr>
     <td>Message</td>
     <td><textarea rows=4 cols=40 name='message'></textarea></td>
   </tr>
   <tr>
     <td> </td>
     <td><input type=submit name=submit value=Send></td>
   </tr>
   </table>
   </form>
 </body>
</html>
Figure 2 - The source of *\WWW\sendsms.html

If you have successfully saved this file in your webserver's directory, you can open it in any webbrowser (Figure 3). You can add any telephone number to the recipient field (e.g. +4407776134588) and write your message text in the next textbox.

how the sms form should look like
Figure 3 - How the SMS form should look like

The form tag's action attribute points to sendsms.php. Which means that the data entered on the form will be forwarded to sendsms.php. The data will be sent to the HTTP API if 'Send' is pressed.

How to prepare your PHP SMS script

'sendsms.php' is the target for the recipient and message fields. This PHP script can build an URL for Ozeki SMS Gateway's HTTP API. It will invoke the URL if 'Send' is pressed. Do no forget to configure $ozeki_user, $ozeki_password and $ozeki_url variables. In case Ozeki SMS Gateway and your webserver runs on the same machine, you can leave $ozeki_url untouched. Otherwise you should change the IP to the IP address of Ozeki SMS Gateway's machine. The newest version of Ozeki SMS Gateway uses port 9505, while 9501 belongs to the older one.


<?php

########################################################
# Login information for the SMS Gateway
########################################################

$ozeki_user = "admin";
$ozeki_password = "abc123";
$ozeki_url = "http://127.0.0.1:9505/api?";

########################################################
# Functions used to send the SMS message
########################################################
function httpRequest($url){
    $pattern = "/http...([0-9a-zA-Z-.]*).([0-9]*).(.*)/";
    preg_match($pattern,$url,$args);
    $in = "";
    $fp = fsockopen("$args[1]", $args[2], $errno, $errstr, 30);
    if (!$fp) {
       return("$errstr ($errno)");
    } else {
        $out = "GET /$args[3] HTTP/1.1\r\n";
        $out .= "Host: $args[1]:$args[2]\r\n";
        $out .= "User-agent: Ozeki PHP client\r\n";
        $out .= "Accept: */*\r\n";
        $out .= "Connection: Close\r\n\r\n";

        fwrite($fp, $out);
        while (!feof($fp)) {
           $in.=fgets($fp, 128);
        }
    }
    fclose($fp);
    return($in);
}



function ozekiSend($phone, $msg, $debug=false){
      global $ozeki_user,$ozeki_password,$ozeki_url;
	  
      $url= 'action=sendmessage';
      $url.= '&username='.$ozeki_user;
      $url.= '&password='.$ozeki_password;      
      $url.= '&messagetype=SMS:TEXT';
      $url.= '&recipient='.urlencode($phone);
      $url.= '&messagedata='.urlencode($msg);

      $urltouse =  $ozeki_url.$url;
      if ($debug) { echo "Request: <br>$urltouse<br><br>"; }

      //Open the URL to send the message
      $response = httpRequest($urltouse);
      if ($debug) {
           echo "Response: <br><pre>".
           str_replace(array("<",">"),array("&lt;","&gt;"),$response).
           "</pre><br>"; }

      return($response);
}

########################################################
# GET data from sendsms.html
########################################################

$phonenum = $_POST['recipient'];
$message = $_POST['message'];
$debug = true;

ozekiSend($phonenum,$message,$debug);

?>
Figure 4 - The source code of the *\WWW\sendsms.php script

After both sendsms.html and sendsms.php are copied and modified at *\WWW\, an SMS message can be submitted by the gateway. After it gets accepted for delivery, the SMS Gateway will return the message reference number in the response (Figure 5). Messages are tracked in Ozeki SMS Gateway by their reference number.

response xml from ozeki sms gateway
Figure 5 - Response XML from Ozeki SMS Gateway

Summary

In this article, you can see how to add SMS functionality to your website. It uses the provided example PHP SMS script to reach the Ozeki SMS Gateway. Using the SMS Gateway offers the ability to route SMS messages between various endpoints and gives you the ability to control your text messages as they go through the system, to can reach anybody in your organization easily.

If you want to build a reliable and versatile SMS communication system, find more information about it on the Ozeki website. Feel free to check out the article about sending SMS from a website using MySQL, or sending SMS using C#.

To create your first high performance SMS gateway system, now is the best time to download Ozeki SMS Gateway!

More information