PHP SMS API for PHP Developers - How to add SMS functionality to a website

Download source code (tgz format): php-sms-api-php-sms-example.tgz

Download source code (zip format): php-sms-api-php-sms-example.zip

This guide gives you step by step instructions on how to add SMS functionality to a PHP website using an SQL to SMS gateway configuration. The example explains all the steps necessary and gives links to sections detailing each step.

Phase I - setting up the SQL to SMS gateway configuration

Phase one gives you the configuration steps that are necessary to configure an SQL to SMS gateway architecture.

Step 1.) The first step is to attach a GSM modem to the SMS gateway computer with a datacable. For a GSM modem it is best to use one of the supported models, for example a Wavecom Fastrack MB1306B is a good option. Other models can be found in the Recommended GSM modems for SMS sending guide.

Step 2.) Step two is to install Ozeki NG on the SMS gateway computer, following the instructions provided in the quick startup guide. The instructions in this guide will help setup the GSM modem and to send your first SMS messages from the graphical user interface of the SMS gateway.

Step 3.) Then you should setup a MySQL database on the webserver, and you should create two database tables. One would be used for sending SMS messages and another would be used to receive SMS messages. This MySQL SMS Gateway Configuration / database table layout section contains the MySQL create table script you can use to create the database.

Step 4.) The next step is to install MyODBC v5.1 on the sms gateway computer. MyODBC will make it possible for the SMS gateway to connect to your MySQL database server. MyODBC is a database connectivity driver.

Step 5.) The final, most important step is to configure the SQL to SMS gateway configuration for MySQL in Ozeki NG. These pages giveg you up to date instructions on this topic:

https://ozekisms.com/p_2164-send-and-receive-sms-messages-from-a-database-application.html
https://ozekisms.com/p_2168-sql-to-sms-gateway-configuration-mysql-overview.html

Phase II - The PHP script

Once the SMS gateway is in place, your PHP website can send SMS messages by simply placing records into a mysql database table called ozekimessageout. For example you can create an HTML form, that the visitors of the website will fill in. When the form is submitted you can create record in the ozekimessagout database table using an SQL INSERT statement. See example:

Example 1: How to send an SMS message


<HTML>
<BODY>

<H1> Send an SMS </H1>

<?php
$phonenum = $_GET['phonenum'];
$messagetext = $_GET['messagetext'];

if ($phonenum<>'') {

  $conn = mysql_connect("localhost", 'ozeki', 'abc123');
  if (!$conn) {
    die('Could not connect to database ' . mysql_error());
  }

  mysql_select_db('ozekisms');
  $sql = "INSERT INTO ozekimessageout (receiver,msg,status) ".
         "VALUES ('$phonenum','$messagetext','send')";
  mysql_query($sql);
  mysql_close($conn);

  echo "The message has been submitted for sending <br><br>";
}
?>

<FORM action=send.php METHOD=GET>
  Mobil phone number:
  <INPUT TYPE="TEXT" SIZE="16" NAME="phonenum" VALUE="+44777888999">
  <br>
  <TEXTAREA NAME="messagetext" ROWS=5 COLS=40>Hello world</TEXTAREA>
  <br>
  <INPUT TYPE=SUBMIT VALUE=SEND>
</FORM>

</BODY>
</HTML>

To receive SMS messages all you have to do is select the records from the ozekimessagin database table. This can be done by issuing a simple SQL SELECT statement. Take a look at the following example.

Example 2: How to receive an SMS message

<HTML>
<BODY>

<H1>List incoming messages</H1>

<?php
  $conn = mysql_connect("localhost", 'ozeki', 'abc123');
  mysql_select_db('ozekisms');
  $sql = "SELECT sender,senttime,msg FROM ozekimessagein ORDER BY senttime desc";
  $res = mysql_query($sql);
  $cn = mysql_num_rows($res);

  for ($x=0;$x<$cn;$x++) {
     list ($sender,$senttime,$msg) = mysql_fetch_row($res);
     echo "<li>$senttime, <b>$sender</b>, $msg";
  }

  mysql_close($conn);
?>

</BODY>
</HTML>

More information