ActiveXperts SMS Component

Quicklinks

PHP sample - Send SMS text through an SMPP provider

The ActiveXperts SMS Component is a software development kit (SDK) to enhance an application or script with SMS or Pager functionality. SMS messages can be sent/received using a GSM modem, an SMPP provider or an HTTP compliant SMSC.


Introduction

In this example we are going to create a PHP page to send SMS messages. This demo project will ask the user to give a phone number and a message body in the web interface.

A subscription to an SMPP provider is required. For this demo you can send a limited number of messages through our own gateway.

Step 1: Installation of the ActiveXperts SMS Component

On each client PC, download the ActiveXperts SMS Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create the SMS objects in PHP

You must use the following code to declare the COM object(s) in PHP:

Insert the following line to declare and create the Smpp object:

$objSmpp         = new COM("AxSms.Smpp");
$objSmsMessage   = new COM("AxSms.Message");
$objSmsConstants = new COM("AxSms.Constants");

Step 3: Gather information

The following code will ask the user for the recipient telephone number and the content of the text message. This data will be stored in the 'objSmsMessage' object.

$objSmsMessage->Clear();
$objSmsMessage->RequestDeliveryReport = true;
$objSmsMessage->ToAddress = $_POST['txtToAddress'];
$objSmsMessage->Body = $_POST['txtMessage'];
$objSmsMessage->BodyFormat = $objSmsConstants->BODYFORMAT_TEXT;

Step 4: Send SMS messages

The following code shows how to send an SMS message using the data that was stored in the 'objSmsMessage' and 'objSmsConstants' objects. The timeout value is set to 10 seconds, which means that the modem will try to send the message for 10 seconds, after this value it will stop trying.

$objSmpp->SubmitSms($obj, $iMultipart);

Appendix: Full source code

Following you can find the full source code which is also included in the ActiveXperts SMS Component package.

<?php
  $strResult = "n/a";
  $strMessageStatus = "";
  
  $strAccount = "";
  $strPassword = "";
  
  try
  {
  GetSmsDemoAccountInfo($strAccount, $strPassword);
  }
  catch(Exception $e)
  {
    $strAccount = "[SysID]";
    $strPassword = "[Pwd]";
  }
  
  $objSmpp = new COM("AxSms.Smpp");
  $objSmpp->LogFile = sys_get_temp_dir()."ActiveXperts.Smpp.log"; 
  // Windows default: 'C:\Windows\Temp\ActiveXperts.Smpp.log'
  
  // Form submitted
  if (isset($_POST["btnSendMessage"]))
  {  
    $objSmsMessage = new COM("AxSms.Message");
    $objSmsConstants = new COM("AxSms.Constants");
    
    $objSmpp->Connect($_POST['txtServer'], $_POST['txtServerPort'], 5000);
    
    if ($objSmpp->LastError != 0)
    {
      $strResult = $objSmpp->LastError.": " . $objSmpp->GetErrorDescription($objSmpp->LastError);
    }
    else
    {
      $iSystemMode = $objSmsConstants->SMPP_BIND_TRANSCEIVER;
      $iVersion = $objSmsConstants->SMPP_VERSION_34;
      $objSmpp->Bind($iSystemMode, 
          $_POST['txtSystemID'], 
          $_POST['txtPassword'], 
          $_POST['txtSystemType'], 
          $iVersion, 0, 0, "", 5000);
      
      if ($objSmpp->LastError != 0)
      {
        $strResult = $objSmpp->LastError;
      }
      else
      {
        $strMessageReference;
        $bSearchDeliveryReport = true;
        $obj;
        
        $objSmsMessage->Clear();
        $objSmsMessage->RequestDeliveryReport = true;
        $objSmsMessage->ToAddress = $_POST['txtToAddress'];
        $objSmsMessage->Body = $_POST['txtMessage'];
        $objSmsMessage->BodyFormat = $objSmsConstants->BODYFORMAT_TEXT;
        
        if (isset($_POST['cbxFlash']))
        {
          $objSmsMessage->DataCoding |= $objSmsConstants->DATACODING_FLASH;
        }
        
        $iMultipart = (isset($_POST['cbxMultipart'] )) ?
          $objSmsConstants->MULTIPART_OK : $objSmsConstants->MULTIPART_TRUNCATE;
        
        $obj = $objSmsMessage;
        $objSmpp->SubmitSms($obj, $iMultipart);
        
        if ($objSmpp->LastError != 0)
        {
          $strResult = $objSmpp->LastError;
        }
        else
        {
          while ($objSmpp->WaitForSmsUpdate(1000))
          {
            $objSmsMessage = $objSmpp->FetchSmsUpdate();
          
            if ($objSmpp->LastError == 0)
            {
              break;
            }
            
            sleep(0.1);
          }
          
          $strMessageReference = $objSmsMessage->Reference;
          
          while ($bSearchDeliveryReport)
          {
            $objMessage = $objSmpp->ReceiveMessage();
            while ($objSmpp->LastError == 0)
            {
              if ($objMessage->SmppIsDeliveryReport)
              {
  if ($strMessageReference == substr($objMessage->Body, strrpos($objMessage->Body, "id:") + 3, 8))
                {
        $strMessageStatus = substr($objMessage->Body, strrpos($objMessage->Body, "stat:") + 5, 7);
                  $bSearchDeliveryReport = false;
                  break;
                }
              }
              $objMessage = $objSmpp->ReceiveMessage();
            }
            sleep(1);
          }
          $strResult = $objSmpp->LastError;
        }
      }
    }
  }
  
  function GetSmsDemoAccountInfo(&$strGwAccount, &$strGwPassword)
  {
    // NOTE: during installation of the ActiveXperts SMS Component, a unique login and password is 
    // created for you. This login and password can be used to send a few SMS messages through 
    // the smpp.activexperts-labs.com gateway, for free
    // The 'GetSmsDemoAccountInfo' reads the unique account info from a file that was created 
    // during installation
    
    $objShell = new COM("WScript.Shell");
    $strDirectory = $objShell->RegRead("HKEY_LOCAL_MACHINE\\SOFTWARE\\" . 
                                       "ActiveXperts\\SMS Component\\InstallRoot");
    $strDirectory .= "\\Utilities\\activexperts-labs.txt";
    
    $objFile = fopen($strDirectory, "r");
    
    fgets($objFile, 4096);          //1st line is a comment line; skip it
    $strGwAccount = fgets($objFile, 4096);  //2nd line is the account
    $strGwPassword = fgets($objFile, 4096);  //3rd line is the password
  }
  
//HTML-CSS layout includes, no code there!
include('css/Header.html'); 
include('css/Menu.html'); 
?>
    <div class="ax_container">
      <h1>SMS Component PHP SMPP Sample</h1>
      <hr />
      <p>
      This demo allows you to send SMS messages over SMPP. If you don't have an SMPP subscription,
      you can use the ActiveXperts SMPP Demo Gateway. You can send out a few SMS messages
      for free through this gateway.
      </p>
      <form action="smpp.php" method="post">
        <h2>SMS Component:</h2>
        <h3>Build: <?php echo $objSmpp->Build; ?>; Module: <?php echo $objSmpp->Module; ?></h3>
        
        <!-- Server -->
        <label for="Server">Server:</label>
        <p>
          <input type="text" id="Server" name="txtServer" value="smpp.activexperts-labs.com" />
          : <input type="text" name="txtServerPort" style="width: 75px;" value="2775" />
        </p>
        
        <!-- System ID -->
        <label for="SystemID">System ID:</label>
        <p>
        <input type="text" id="SystemID" name="txtSystemID" value="<?php echo $strAccount; ?>" />
        </p>
        
        <!-- Password -->
        <label for="Password">Password:</label>
        <p>
        <input type="text" id="Password" name="txtPassword" value="<?php echo $strPassword; ?>" />
        </p>
        
        <!-- System Type -->
        <label for="SystemType">System Type:</label>
        <p>
        <input type="text" id="SystemType" name="txtSystemType" value="SMPP" />
        </p>
        
        <!-- Recipient -->
        <label for="ToAddress">Recipient:</label>
        <p>
        <input type="text" id="ToAddress" name="txtToAddress" value="[ToAddress]" />
        </p>
        
        <!-- Message, Multipart, Flash -->
        <label for="Message">Message:</label>
        <p>
          <textarea id="Message" name="txtMessage" style="height:45px;">
            Hello world send from ActiveXperts SMS Component!
          </textarea>
          <br />
          
          <input type="checkbox"class="ax_cbFix" id="Multipart" name="cbxMultipart" value="1" />
          <label for="Multipart">Allow Multipart</label><br />
          
          <input type="checkbox"class="ax_cbFix" id="Flash" name="cbxFlash" value="1" />
          <label for="Flash">Flash</label>
        </p>
        
        <!-- Send button -->
        <div class="ax_clearLabel"></div>
        <p>
          <input type="submit" name="btnSendMessage" value="Send SMS Message!" />
        </p>
        
        <!-- Empty row -->
        <div class="ax_clearRow"></div>
        
        <!-- Result -->
        <label for="Result"><b>Result:</b></label>
        <p>
          <input type="text" id="Result" name="txtResult"class="ax_FullWidth Bold" 
              value="<?php echo $strResult; ?>" />
        </p>
        
        <!-- Message status -->
        <label for="Status">Message status:</label>
        <p>
          <input type="text" id="Status" name="txtMessageStatus"class="ax_FullWidth" 
              value="<?php echo $strMessageStatus; ?>" />
        </p>
      </form>
      <p>
        This demo uses the ActiveXperts SMS Component, an 
        <a href="http://www.activexperts.com" target="_blank">
            ActiveXperts Software
        </a> 
        product.<br />
        <a href="index.php">Back to main page</a> 
      </p>
    </div><!-- /container -->
<?php 
//HTML-CSS layout include, no code there!
include('css/Footer.html'); ?>

You can download the full source code of this project from the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/sms-component. There are many other working samples included with the product or on the FTP site.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The ActiveXperts SMS Component project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.