SMS API: PHP Code

Overview

Each of the following PHP functions communicate with our server, make an HTTP request and then returns our response. Once the most suitable function has been installed, sending SMS (for example) can be sent in as little as one line of code.

To use any of these functions:

  1. Save it in a location that can be accessed by your PHP script (i.e. an include file).
  2. Prepare the request, ensuring that individual values have been urlencoded.
  3. Call the function using <?$response = tm4b($request);?>.

CURL

The following function uses the CURL library.

<?
function tm4b($request)
{
if(!extension_loaded('curl')) $response = "Function requires CURL.";
else
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.tm4b.com/client/api/http.php";
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
}
return $response;
}
?>

fsockopen

The following function uses fsockopen.

<?
function tm4b($request)
{
if(!extension_loaded('sockets')) $response = "Function requires sockets.";
else
{
$http_header = "POST /client/api/http.php HTTP/1.1\r\n";
$http_header .= "Host: tm4b.com\r\n";
$http_header .= "User-Agent: HTTP/1.1\r\n";
$http_header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $http_header .= "Content-Length: ".strlen($request)."\r\n";
$http_header .= "Connection: close\r\n\r\n";
$http_header .= $request."\r\n";

$host = "ssl://tm4b.com";
$port = 443;

$out = @fsockopen($host, $port, $errno, $errstr);
if($out)
{
fputs($out, $http_header);
while(!feof($out)) $result[] = fgets($out);
fclose($out);
}
$response = $result[9];
}
return $response;
}
?>

file_get_contents

The following function uses file_get_contents.

<?
function tm4b($request)
{
return file_get_contents("https://www.tm4b.com/client/api/http.php?".$request);
}
?>
php and sms
php sender sms web
php sms support
© 2003 - 2008 SMS-Comms UK Ltd. | Service Agreement
|SEND SMS|RECEIVE SMS|SMS ONLINE|SMS API|RESOURCES|
Contact Us Client Login Register Online