ActiveXperts SMS Component

Quicklinks

PHP sample - Send SMS text through a COM/USB GSM modem

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 GSM modem is required for this demo.

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

The following code will show you how to declare and create the GSM and SMS objects. We will use the 'objGsm' object to send the message itself. The 'objSmsMessage' object will be used to store information of the message and the 'objSmsConstants' object containes constant values releated to the SMS objects.

$objGsm          = new COM("AxSms.Gsm");	
$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. If any error occours while trying to open the connection, the script will end.

//Message Settings
$objSmsMessage->Clear();
$objSmsMessage->ToAddress = $_POST["txtToAddress"];
$objSmsMessage->Body = $_POST["txtBody"];
$objSmsMessage->BodyFormat = $objSmsConstants->BODYFORMAT_TEXT;

Step 4: Send the SMS message

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.

//Send the message !
$obj = $objSmsMessage;
$objGsm->SendSms($obj, $iMultipart, 10000);
$objSmsMessage = $obj;
$strMessageReference = $objSmsMessage->Reference;

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";
  
  $objGsm = new COM("AxSms.Gsm");
  $objGsm->LogFile = sys_get_temp_dir()."ActiveXperts.Gsm.log"; 
  //Windows default: 'C:\Windows\Temp\ActiveXperts.Gsm.log'
  
  //Form submitted
  if (isset($_POST["btnSendMessage"]))
  {
    $obj;
    $strMessageReference;
    $objSmsMessage = new COM("AxSms.Message");
    $objSmsConstants = new COM("AxSms.Constants");
    
    $strName = $_POST["ddlDevices"];
    $strPincode = $_POST["txtPincode"];
    $iDeviceSpeed = $_POST["ddlDeviceSpeed"];
    
    $objGsm->Clear();
    $objGsm->LogFile = $_POST["txtResult"];
    $objGsm->Open($strName, $strPincode, $iDeviceSpeed);
    
    if ($objGsm->LastError != 0)
    {
      $strResult = $objGsm->LastError . ": " . $objGsm->GetErrorDescription($objGsm->LastError);
      $objGsm->Close();
    }
    else
    {
      //Message Settings
      $objSmsMessage->Clear();
      $objSmsMessage->ToAddress = $_POST["txtToAddress"];
      $objSmsMessage->Body = $_POST["txtBody"];
      $objSmsMessage->BodyFormat = $objSmsConstants->BODYFORMAT_TEXT;
      
      $iMultipart = (isset($_POST["cbxMultipart"])) ?
        $objSmsConstants->MULTIPART_OK : $objSmsConstants->MULTIPART_TRUNCATE;
      
      if (isset($_POST["cbxFlash"]))
      {
        $objSmsMessage->DataCoding |= $objSmsConstants->DATACODING_FLASH;
      }
      
      //Send the message !
      $obj = $objSmsMessage;
      $objGsm->SendSms($obj, $iMultipart, 10000);
      $objSmsMessage = $obj;
      $strMessageReference = $objSmsMessage->Reference;
      
      $strResult = $objGsm->LastError . ": " . $objGsm->GetErrorDescription($objGsm->LastError);
      
      $objGsm->Close();
    }
  }  
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>ActiveXperts SMS Component Demo</title>
  <link rel="Stylesheet" type="text/css" href="css/Layout.css" />
</head>
<body>
  <div class="ax_maincontainer">
    <div class="ax_header">
      <div class="ax_stroke"></div>
      <div class="ax_logo"></div>
    </div><!-- /header -->
    <div class="ax_container">
      <h1>SMS Component PHP GSM Sample</h1>
      <hr />
      <p>
        This demo requires a GSM modem or GSM phone connected to your computer. A SIM card
        is required in this GSM modem. The product works with almost all available GSM modems.
      </p>
      <form action="gsm.php" method="post">
        <h2>SMS Component:</h2>
        <h3>Build: <?php echo $objGsm->Build; ?>; Module: <?php echo $objGsm->Module; ?></h3>
        
        <!-- Device -->
        <label for="Devices">Device:</label>
        <p>
          <select id="Devices" name="ddlDevices">
          <?php
            $strDevice = $objGsm->FindFirstDevice();
            
            while ($objGsm->LastError == 0)
            {
              echo '
            <option value="'.$strDevice.'">'.$strDevice.'</option>';
              $strDevice = $objGsm->FindNextDevice();
            }
            
            for ($i = 1; $i <= 20; $i++)
            {
              echo '
            <option value="COM'.$i.'">COM'.$i.'</option>';
            }
          ?>
          
          </select>
        </p>
        
        <!-- Device Speed -->
        <label for="DeviceSpeed">Device Speed:</label>
        <p>
          <select id="DeviceSpeed" name="ddlDeviceSpeed">
            <option value="0">Default</option>
            <option value="110">110</option>
            <option value="300">300</option>
            <option value="600">600</option>
            <option value="1200">1200</option>
            <option value="2400">2400</option>
            <option value="4800">4800</option>
            <option value="9600">9600</option>
            <option value="14400">14400</option>
            <option value="19200">19200</option>
            <option value="38400">38400</option>
            <option value="56000">56000</option>
            <option value="57600">57600</option>
            <option value="64000">64000</option>
            <option value="115200">115200</option>
            <option value="128000">128000</option>
            <option value="230400">230400</option>
            <option value="256000">256000</option>
            <option value="460800">460800</option>
            <option value="921600">921600</option>
          </select>
          Only applies to direct ports, i.e. COM1, COM2, etc.
        </p>
        
        <!-- Pincode -->
        <label for="Pincode">Pincode:</label>
        <p>
          <input type="password" id="Pincode" name="txtPincode" />
          Only use when SIM card requires PIN code
        </p>
        
        <!-- Empty row -->
        <div class="ax_clearRow"></div>
        
        <!-- ToAddress -->
        <label for="ToAddress">ToAddress:</label>
        <p>
          <input type="text" id="ToAddress" name="txtToAddress" value="[ToAddress]" />
          <a href="http://www.activexperts.com/support/sms-component/?kb=Q4200015" target="_blank">
            Recipient number format
          </a>
        </p>
        
        <!-- Body, Multipart, Flash -->
        <label for="Body">Body:</label>
        <p>
          <textarea id="Body" name="txtBody" style="height:55px;">
          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>
        
        <!-- Empty row -->
        <div class="ax_clearRow"></div>
        
        <!-- Send button -->
        <div class="ax_clearLabel"></div>
        <p>
          <input type="submit" name="btnSendMessage" value="Send SMS Message!" />
        </p>
        
        <!-- 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>
      </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 -->
    <div class="ax_footer">
      <div class="ax_icon"></div>
      <p>
        © 2011 <a href="http://activexperts.com" target="_blank">
          Active<font color="#CCC000000">X</font>perts Software B.V.
        </a> All rights reserved.
        <small>
          <a href="http://activexperts.com/activexperts/contact" target="_blank">
            Contact Us
          </a> |
          <a href="http://activexperts.com/activexperts/termsofuse" target="_blank">
            Terms of Use
          </a> |
          <a href="http://activexperts.com/activexperts/privacypolicy" target="_blank">
            Privacy Policy
          </a>
        </small>
      </p>
    </div><!-- /footer -->
  </div><!-- /maincontainer -->
</body>
</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.