SMS Gateway HTTP/S API – Simple HTML & PHP How-to

December 23, 2009

Integrating with the HTTP/S API to communicate with the SMS gateway is easier than you may think. This short how-to will show you how to send messages using an HTML form and a PHP script together. This article assumes that you are a developer familiar with HTML and PHP.

Although we are using PHP in this example, you can use virtually any language capable of making an HTTP request.

If you’ve missed our previous posts dealing respectively with how to send a message with an HTML form and a basic PHP script, choose the appropriate link to navigate to the page in question:

1.    SMS Gateway HTTP/S API – Simple HTML From How-to
2.    SMS Gateway HTTP/S API – Simple PHP Script How-to

Getting Started

The below sample HTML form will collect parameters and pass it to a PHP script for sending a message to the API.

<form method="post" action="sendMessage.php">
API_ID:        <input type="text" name="api_id">               <br/>
Username:      <input type="text" name="user">                 <br/>
Password:      <input type="password" name="password">         <br/>
Mobile Number: <input type="text" name="to">                   <br/>
Message Text:  <textarea name="text"></textarea>               <br/>
<input type="submit" value="Send my SMS">
</form>

The example PHP script below will accept parameters from the above form and use it to send a message to the HTTP/S API.

File: sendMessage.php
<?
# Collect parameters from HTML form submission
$message = array(
    'to'          => $_REQUEST['to'],
    'text'        => $_REQUEST['text'],
    'api_id'      => $_REQUEST['api_id'],
    'user'        => $_REQUEST['user'],
    'password'    => $_REQUEST['password']
);

$api_url = 'http://api.clickatell.com/http/sendmsg?';

# Urlencode parameters
foreach ($message as $parameter => $value )
{
    $api_url .= $parameter . '=' . urlencode($value) . '&';
}

# Perform an API call. You can also use cURL for this.
$result = file_get_contents($api_url);

echo "API Response: $result\n";
?>

Note: remember that parameters should be URL encoded before submission to the API.

When this script sends a message, the resulting HTTP URL that it uses will look something like this:

    http://api.clickatell.com/http/sendmsg?api_id=1234&user=myUserName&password=myPassword&to=123456789&text=The+url+encoded+message

API Response

The API response will contain either an error code with an error message, or an apimsgid (tracking code for your message).

Example API response:

    ID: f5be1b5eb7229e08fb45428600d5f36d

In this example we used only a few parameters (for authentication, message text and destination mobile number). There are many more optional parameters that give you more flexibility –  more information on this is available in the HTTP/S API technical documentation.

Related posts:

  1. SMS Gateway HTTP/S API – Simple HTML How-to Aiming to use the HTTP/S API to connect with your...
  2. SMS Gateway HTTP/S API – Simple PHP How-to There's a communications solution for everyone, even if you have...
  3. International SMS Scripts and API’s Use these SMS scripts to enable your business or international...

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: , , , , , , ,

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

One Comment On This Post

  1. [...] Once your web server is up and running, refer to the following blog post for a very basic indication of how to structure your programs to enable communication with your neighbours through SMS gateways: SMS Gateway HTTP/S API – Simple HTML & PHP How-to [...]

Join the Discussion

Subscribe via RSS or