Depiction- Webmaster Resources
Stock Image

Contributors: Advertise Here  |  Advertise Here  |  Advertise Here

Misc Links: Submit Tutorials (Earn $$) l Advertising l Privacy Policy | Site Map

Tutorials > PHP > Sending SMS Thru HTTP Page 2


Sending the request with CURL

Previously we saw that the request could be executed by pasting it into the browser window. But what we really want is for this to take place behind the scenes. The following code does exactly that using CURL.

CURL 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 PHP Manual.

This code opens up a connection with the gateway, sends the SMS message(s) and collects their message IDs which are presented within the response header.

$url = ""; 
 //this is the url of the gateway's interface
$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

First, we initialize a new CURL session. Then we set our desired options; this includes setting CURLOPT_POST because TM4B's SMS API requires us to send multiple messages using POST. Finally we execute the call and then close the handle.

Sending the Request with Sockets

CURL functions depend on an external library and PHP must have been compiled with the --with-curl flag. So while CURL is very flexible and useful, it may not be available with your PHP installation. If this is the case, you can still communicate with the SMS gateway using sockets.

//First prepare the info that relates to the connection
$host = "";
$script = "/client/api/send.php";
$request_length = strlen($request);
$method = "POST"; // must be POST if sending multiple messages
if ($method == "GET")
$script .= "?$request"; } //Now comes the header which we are going to post. $header = "$method $script HTTP/1.1\r\n"; $header .= "Host: $host\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: $request_length\r\n"; $header .= "Connection: close\r\n\r\n"; $header .= "$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); } /* the message id's will be kept in one of the $output values print ""; print_r($output); print ""; */ ?>

First we layout the information we'll need to send our SMS and use it construct the HTTP header. A socket connection is established to our gateway using fsockopen. Information is sent and received in the same manner PHP would read and write to a file. After our transfer is complete we close the socket using fclose.


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. Furthermore, whilst both fsockopen and CURL can send thousands of messages in one go, fsockopen might give you difficulties when parsing responses for large requests as the responses are transferred in chunks.

The above took me ages; I hope it saves you time.

About the Author

Farheen Rehman is the editor of BestKeptSimple, a very popular blog about SMS. She is very well versed with the commercial use of SMS and provides consultancy for organizations that want to add it to their communications mix.

Go back to page 1.



Templora Photoshop Tutorials
Tutorial Man Wallpaper Stock  More