WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Sending SMS Thru HTTP
Categories : PHP, HTTP, SMS Click here to Update Your Picture
farheen rehman
Date : May 04th 2005
Grade : 1 of 5 (graded 2 times)
Viewed : 7883
File : No file for this code example.
Images : No Images for this code example.
Search : More code by farheen rehman
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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:

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 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:

<?php
$request
= ""; //initialise the request variable
$param[username] = "abcdef"; //this is the username of our TM4B account
$param[password] = "12345"; //this is the password of our TM4B account
$param[msg] = "This is sample message."; //this is the message that we want to send
$param[to] = "447768254545|447956219273|447771514662"; //these are the recipients of the message
$param[from] = "MyCompany";//this is our sender if
$param[route] = "frst";//we want to send the message via first class
$param[sim] = "yes";//we are only simulating a broadcast

foreach($param as $key=>$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.

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
<?php
   
//First prepare the info that relates to the connection
   
$host = "tm4b.com";//although you can use an ip address, it is easier to just use tm4b.com
   
$request_length = strlen($request);// when we post the header, we have to also include it's length

   
$script = "/client/api/send.php";
   
$method = "POST"; //Replace with "GET" if required.
   
if($method=="GET") $script .= "?$request";//Appends the request if "GET" is being used.

    //Now comes the header which we are going to post. This is where our messages details will be sent over.
   
$header =     "$method $script HTTP/1.1\r\n". //
                             
"Host: $host\r\n".
                             
"User-Agent: HTTP/1.1\r\n".
                             
"Content-Type: application/x-www-form-urlencoded\r\n".
                             
"Content-Length: $request_length\r\n".
                             
"Connection: close\r\n\r\n".
                             
"$request\r\n";

   
//Now we open up the connection
   
$socket = @fsockopen($host, 80, $errno, $errstr);
    if (
$socket) //if its open, then...
   
{
       
fputs($socket, $header); // send the details over
       
while(!feof($socket)) $output[] = fgets($socket); //get the results
       
fclose($socket);
    }
   
//print "<pre>";print_r($output);print "</pre>";//the message id's will be kept in one of the $output values
?>


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>.

<?php
$url
= "https://www.tm4b.com/client/api/send.php"; //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
?>

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.





PHP Domain Availability Checker
Categories : PHP, Complete Programs, Regexps, HTTP, Sockets
Easy upload class
Categories : PHP Classes, Filesystem, HTTP, PHP
2-way SMS, EMS, Ringtone, Logo script
Categories : PHP, SMS, Cellular, Ring Tones
A function to check if a URL exists
Categories : PHP, CURL, HTTP
Gonx Proxy - This class is meant to act as an HTTP proxy to serve pages of a remote server as if they were local pages.
Categories : PHP, PHP Classes, HTTP
redirect redirection ip address authentication authenticate addr
Categories : Authentication, HTTP, Network, PHP
Function that does language negotiation based on the Accept-Language header, a cookie or host name
Categories : HTTP, PHP, Cookies
The first step Guest Book ... ^^
Categories : MySQL, PHP, Apache, HTML, HTTP
Script to check values being submitted by POST or GET method from a form. This script may help diagnose what variables are being supplied by a browser to other php scripts.
Categories : HTML, Variables, Debugging, PHP, HTTP
Simple pipe delimited file export program that downloads to a local machine
Categories : PHP, Filesystem, Databases, MySQL, HTTP
Browser Detection, Redirection Type, MSIE, MOZILLA, Netscape Navigator, NS, $HTTP_USER_AGENT, HTTP_USER_AGENT
Categories : PHP, HTTP, Browsers, HTML and PHP
Request Method Class - seful for situations like form processing or API development. Requires PHP5 for the magic __call() method.
Categories : PHP, PHP Classes, HTTP, Headers
Inserting data to a MySQL Database using AJAX
Categories : AJAX, HTTP, PHP, Databases, MySQL
Simple Password example
Categories : PHP, Authentication, Security, HTTP
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
 Sarah King wrote :1324
A nice wee promo for TM4B and the code is good too :)