|
||||||||
| Code | |||||
|
Sending SMS with HTTP There are an infinite number of reasons why you might want to use PHP to send SMS. You might want to add a "send by SMS" option to your headlines, you might want to provide 24/7 support in which your technican is alerted by SMS or you might want to provide your viewers with Free SMS to drive traffic to your site. Although it is possible to send SMS via e-mail, which we will cover another time, this tutorial will focus on the use of HTTP methods "get" & "post". For those of us that many not know this, using HTTP basically means the use of forms, just like a contact form <form></form>, 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, it is based on <a href="htp://www.tm4b.com/">TM4B SMS Gateway</a> because a) they are the only gateway i know that have a 'simulation' mode for tweaking your scripts, b) they don't have any set-up fees and their prices are low, and c) they are reliable and i use them. Step 0: Understanding the requirements of the gateway. Full details about connecting to TM4B are provided on their <a href="http://www.tm4b.com/connectivity/">SMS API</a> page. They basically require us to provide six mandatory pieces of data: i. username - our username ii. password - our password iii. msg - our SMS message iv. to - the one or more recipients of our message v. from - our sender id vi. route - the route of the message (i.e. first class or business class) And we will add a seventh, which is optional... sim - simulate. They will be expecting us to send our messages to them via HTTP requests, similar this one:
which you can test by pasting it into your browser's address bar: Step 1: Prepare our request The first step is to save 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:
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. 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 as the responses are transferred in chunks. Method 1 : Curl method Whilst Curl might sound new, it is a very impressive library that allows you to connect and communicate to many different types of servers with many different types of protocols. You can find more info in the <a href="http://uk2.php.net/curl">PHP Manual</a>.
Step 3: That's It Although it took me a long time to find Curl, i think it is the best, neatest and quickest option (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. The above took me ages, and i hope it saves you time. | |||||