Sending SMS Thru HTTP - Prepare the Request
(Page 33 of 36 )
Now the actual message-delivery process is handled by the gateway. All they want us to do is pass them the details of the message(s) in the form of an HTTP request, similar to 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 |
You can test the above example (which uses GET) by pasting it into your browser's address bar. You should get a response saying that the username is invalid, which is normal because this is just to demonstrate.
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 = ""; //initialize 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 $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 ?> |
We assign our credentials and routing information in the $param array. You'll notice that multiple recipients can be defined by separating them with the pipe-character. Each parameter value needs to be urlencoded and multiple key/value pairs are separated by ampersands. A final ampersand probably would not cause any problems but substr is still used to produce a tidy request.
The script will produce the following request that can be sent to the SMS gateway:username=abcdef&password=12345&msg=This+is+sample+message.&to=447768254545%7C447956219273%7C447771514662&from=MyCompany&route=frst&sim=yes
Next: Sending the request with CURL >>
More Miscellaneous Articles
More By Codewalkers
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|