ActiveXperts SMS Component

Quicklinks

PHP sample - Send SMS text through an HTTP 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 HTTP 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: Declare and create the ActiveXperts SMS Component objects

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

$objHttp           = new COM("AxSms.Http"); 
$objSmsMessage     = new COM("AxSms.Message"); 
$objSmsConstants   = new COM("AxSms.Constants");

Step 3: Gathering 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->BodyFormat = $objSmsConstants->BODYFORMAT_UNICODE;      
$objSmsMessage->Body = $_POST['txtBody'];
$objSmsMessage->ToAddress = $_POST['txtToAddress'];

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.

$obj = $objSmsMessage;
$objHttp->SendSms($obj, $objSmsConstants->MULTIPART_OK);

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";
  $strLastResponse = "";
  
  $objHttp           = new COM("AxSms.Http");    
  $objSmsConstants   = new COM("AxSms.Constants");    
  $objHttp->LogFile  = sys_get_temp_dir()."ActiveXperts.Http.log"; 
  //Windows default: 'C:\Windows\Temp\ActiveXperts.Http.log'
                
  //Form submitted
  if (isset($_POST["btnSendMessage"]))
  {      
    $objSmsMessage = new COM("AxSms.Message");      
    $objSmsMessage->Clear();
    $obj;
    
    $objSmsMessage->BodyFormat = $objSmsConstants->BODYFORMAT_UNICODE;      
    $objSmsMessage->Body = $_POST['txtBody'];
    $objSmsMessage->ToAddress = $_POST['txtToAddress'];
    
    $objHttp->Clear();
    $objHttp->Url = $_POST['txtUrl'];
    
    $obj = $objSmsMessage;
    $objHttp->SendSms($obj, $objSmsConstants->MULTIPART_OK);
    
    $strResult = $objHttp->LastError . ": " . $objHttp->GetErrorDescription($objHttp->LastError);
    $strLastResponse = $objHttp->LastResponseCode;
  }
?>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ActiveXperts SMS Component Demo</title>
  </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 HTTP Sample</h1>
        <hr />
        <p>
          The HTTP protocol is used for medium volume SMS messaging. Messages are first 
          routed to the service provider via Internet/VPN. The provider delivers the 
          messages to the recipient's phone. If you are not subscribed to a provider, use 
          the ActiveXperts gateway to send 10 SMS messages for free.
        </p>      
        <form action="http.php" method="post">
          <h2>SMS Component:</h2>
          <h3>Build: <?php echo $objHttp->Build; ?>; Module: <?php 
          echo $objHttp->Module; ?></h3>
          
          <!-- Connection URL -->
          <label for="Url">Connection URL:</label>
          <p>   
            <?php $strInfo = "post.activexperts-labs.com:8080/sendsms/default.asp" .
                             "?username=myUsername" . 
                             "&password=myPassword" . 
                             "&text=".$objSmsConstants->HTTP_PLACEHOLDER_BODY . 
                             "&to=".$objSmsConstants->HTTP_PLACEHOLDER_TOADDRESS;
            ?>
            <input type="text" id="Url" name="txtUrl"class="ax_FullWidth" value="<?php echo 
            $strInfo ?>" />
          </p>
          
          <!-- 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 -->
          <label for="Body">Body:</label>
          <p>
              <textarea id="Body" name="txtBody" style="height:55px;">
                Hello world send from ActiveXperts SMS Component!
              </textarea>
          </p>
          
          <!-- 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>
          
          <!-- Last Response -->
          <label for="Response">Last Response:</label>
          <p>
            <input type="text" id="Response" name="txtLastResponse"class="ax_FullWidth" 
              value="<?php echo $strLastResponse; ?>" />
          </p>
        </form>
      </div><!-- /container -->
    </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.