Sending SMS Through HTTP Using PHP


The more and more you see and read about how other websites are using SMS, the more and more you begin to wonder why you're not. So I'll show you how (in PHP, because it's the only language i know!). Although it is possible to send SMS via e-mail, which I might cover another time.

This tutorial focuses on the use of HTTP methods "get" & "post". For those of us that may not know this, using HTTP basically means the use of forms, except that these will be submitted automatically as opposed to manually.

Although this tutorial can be used for any gateway that provides access via HTTP, this tutorial is based on the TM4B Bulk SMS Gateway because:

  • They are the only gateway I know that have a 'simulation' mode for tweaking your scripts.
  • They don't have any set-up fees and their prices are low.
  • They are reliable and I use them.

*************************

Step 0 Understanding the requirements of the gateway.

Any SMS gateway will provide details about how you can connect up to them. In the case of TM4B, these are provided on their SMS API page. They basically require us to provide six mandatory pieces of data:

  1. username - our username
  2. password - our password
  3. msg - our SMS message
  4. to - the one or more recipients of our message
  5. from - our sender id
  6. route - the route of the message (i.e. first class or business class)
  7. And we'll add a seventh (optional) one... 'sim' - i.e. simulate.

They will be expecting us to send our messages to them via HTTP requests, similar to this:

http://www.tm4b.com/client/api/send.php?
username=abcdef&password=12345&msg=
This+is+sample+message.&to=447768254545%7C447956219273%
7C447771514662&from=MyCompany&route=frst&sim=yes

which you can test by clicking on it or pasting it into your browser's address bar.


*************************


Step 1: Prepare our request

The first step we have to take is to store our data as variables and then convert them into a url request. There are different ways of doing this, but this is a very innovative and useful way borrowed from the TM4B site itself:

$val)

//traverse through each member of the param array

{

$request.= $key."=".urlencode($val);

//we have to urlencode the values

$request.= "&";

//append the ampersand (&) sign after each paramter/value pair

}

$request = substr($request, 0, strlen($request)-1);

//remove the final ampersand (&) sign from the request

/*This will produce the following request:username=abcdef&password=12345&
msg=This+is+sample+message.&to=447768254545%7C447956219273%
7C447771514662&from=MyCompany&route=frst&sim=yes */

?>

*************************

Step 2: Open up our connection with TM4B and send the request

In step 0, we saw that the request could be actioned by pasting it into the browser window. But what we really want is for this to take place behind the scenes and we wouldn't want anyone knowing our username and password.

The following 2 pieces of code do exactly that. They open up a connection with the gateway, send the SMS message(s) and collect their message ID's which are presented within the response header.


*************************

Method 1 : fosckopen method

Whilst fsockopen may be more familar to most of us, it can only handle non-secure URL's. Furthermore, difficulty may be experienced when parsing responses for large requests (i.e. hundreds of messages) as the response is transferred in chunks.

Method 2 : Curl method

Whilst "Curl" might sound new, it is a really cool way of connecting and communicate to many different types of servers. You can find more info in the PHP Manual.

//although we have used https, you can also use http

$ch = curl_init();

//initialize curl handle

curl_setopt($ch, CURLOPT_URL, $url);

//set the url

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

//return as a variable

curl_setopt($ch, CURLOPT_POST, 1);

//set POST method

curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

//set the POST variables

$response = curl_exec($ch);

//run the whole process and return the response

curl_close($ch);

//close the curl handle

//print $response;

//show the result onscreen for debugging

?>

That's It!!! No more. Now you should know how to send one or more SMS messages through an SMS Gateway. By the way, I think Curl is the better, neater and quicker option of the two (assuming your version of PHP supports it) as it can send thousands of messages in one go, gives no problems in parsing the message ID's and uses either a secure or non-secure url.


//content from http://www.theukwebdesigncompany.com/articles/article.php?article=1583

No comments:

Post a Comment