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

December 10, 2009

There’s a communications solution for everyone, even if you have just started out with PHP and want to try your hand at getting in touch with an SMS gateway using the HTTP/S API (Application Programming Interface). This sample script will show you how to URL encode an SMS message and send it off to a destination number successfully.

Your HTTP/S API Account

Assuming you already have PHP installed and configured correctly, you’ll also need an HTTP/S API account. If you don’t know where to find one, visit Clickatell’s Developers page. This account will allow you to connect to the SMS gateway successfully to send your messages.

More About SMS Gateways

To explain the SMS Gateway more effectively, we’ll make use of a simple analogy:

Imagine arriving at an airport to take a connecting flight to a destination city. In this scenario the SMS Gateway is the airport and the API’s are the connecting flights. Just like the flights, the API’s can connect you to your contacts, whether they are co-workers and employees, friends and family or the target audience you want to connect to for marketing purposes.

The HTTP/S API PHP Script

Here’s the PHP script you’ll be able to use to send a text message. Note that this script has been written with the aim of providing all PHP newcomers with a readable example of all the elements necessary to send an SMS. Once you’re familiar with the code, feel free to change it as you want, in order to read in the variables from a database, etc.

Here’s the script:

<?
/*
Basic PHP example for sending a plain-text message to the Clickatell
HTTP API via an HTTP get.
*/
$message = array(’to’       => ‘2799991235′,
‘text’     => ‘This is a basic PHP sample!’,
‘api_id’   => ‘12345′,
‘user’     => ‘myUserName’,
‘password’ => ‘myPassword’);

# Urlencode parameters and assemble
$api_url = ‘http://api.clickatell.com/http/sendmsg?’;

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”;
?>

Once you’ve scanned through the script, read below for an explanation of each appropriate section.

PHP Code Elements

$message=array() The array() language construct is used to pass key => value pairs to the $message variable.
$api_url =  ‘http://api.clickatell.com/http/sendmsg?’; The destination URL is read into the $api_url variable.

foreach ($message as $parameter => $value )
{
$api_url .= $parameter . ‘=’ . urlencode($value) . ‘&’;
}
This URL encodes the parameters and prepares the string for the SMS Gateway API call.
$result = file_get_contents($api_url); This performs the SMS Gateway HTTP API call. This can also be done using cURL.
echo “API Response: $result\n”; Display the result of the message received from the SMS Gateway.

One thing you’ll notice about this script is that all the elements are static, meaning that should you want to change the destination number or the message, you’ll have to edit the file – this script was written to provide you with an example of how to send a plain text message using the HTTP/S API to connect to the SMS Gateway.

Now it’s time to use your own initiative and get creative with the elements presented above. To get more information on the SMS Gateway and how you can connect to it in other ways.

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 HTML & PHP How-to Integrating with the HTTP/S API to communicate with the SMS...

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. [...] 1.    SMS Gateway HTTP/S API – Simple HTML From How-to 2.    SMS Gateway HTTP/S API – Simple PHP Script How-to [...]

Join the Discussion

Subscribe via RSS or