REST SMS API Code Examples
Posted on March 22nd, 2010 by Jay.
Categories: All SMS Gateway Documentation, Development Blog, PHP Classes, Technical
Tags: All SMS Gateway Documentation, php, php5, RESTful, sms api, support
This blog post has been superseded with our website documentation
In our REST API specification document we saw, in general terms, how to make a request to our REST SMS API and how to deal with the response. Here we provide you with some more concrete examples that you can copy & paste!
NOTE: You will want to add additional error checking to the examples below.
In each of the languages used here we create a request, send it, receive the response, and parse its contents.
PHP
Example for getting the number of credits:
$url = 'http://www.textmarketer.biz/services/rest/credits';
$username = 'myAPIusername'; // CHANGE THIS!!!
$password = 'myAPIpassword'; // CHANGE THIS!!!
$url = "$url?username=$username&password=$password";
// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$responseBody = curl_exec($curlHandle);
$responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
// deal with the response
if ($responseInfo['http_code']==200)
{
$xml_obj = simplexml_load_string($responseBody);
$credits = (int) $xml_obj->credits;
// do something with the result
echo $credits;
} else {
// handle the error
var_dump($responseBody);
}
Example for getting the list of delivery reports:
$url = 'http://www.textmarketer.biz/services/rest/deliveryReports';
$username = 'myAPIusername'; // CHANGE THIS!!!
$password = 'myAPIpassword'; // CHANGE THIS!!!
$url = "$url?username=$username&password=$password";
// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$responseBody = curl_exec($curlHandle);
$responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
// deal with the response
if ($responseInfo['http_code']==200)
{
$xml_obj = simplexml_load_string($responseBody);
$atts = $xml_obj->reports->attributes();
$num_reports = (int) $atts->quantity;
if ($num_reports > 0)
{
echo "$num_reports reports\n";
$reports = array();
foreach ($xml_obj->reports->report as $xml_report)
{
$atts = $xml_report->attributes();
$name = (string) $atts->name;
$updated = (string) $atts->last_updated;
$reports[] = array('name'=>$name, 'modified'=>$updated);
}
// do something with the report details
var_dump($reports);
} else {
echo 'No reports available';
}
} else {
// handle the error
var_dump($responseBody);
}
Example for getting the details of a particular delivery report:
// TODO the URL will change according to the delivery report to retrieve...
$url = 'http://www.textmarketer.biz/services/rest/deliveryReport/GatewayAPI_09-02-10';
$username = 'myAPIusername'; // CHANGE THIS!!!
$password = 'myAPIpassword'; // CHANGE THIS!!!
$url = "$url?username=$username&password=$password";
// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$responseBody = curl_exec($curlHandle);
$responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
// deal with the response
if ($responseInfo['http_code']==200)
{
$xml_obj = simplexml_load_string($responseBody);
$reportRows = array();
foreach ($xml_obj->report->reportrow as $xml_reportRow)
{
$atts = $xml_reportRow->attributes();
$mobile = (string) $atts->mobile_number;
$updated = (string) $atts->last_updated;
$msgID = (int) $atts->message_id;
$status = (string) $atts->status;
$reports[] = array('mobile'=>$mobile, 'modified'=>$updated, 'messageID'=>$msgID, 'status'=>$status);
}
// do something with the report details
var_dump($reports);
} else {
// handle the error
var_dump($responseBody);
}
Example for sending an SMS:
$url = 'http://www.textmarketer.biz/services/rest/sms';
$username = 'myAPIusername'; // CHANGE THIS!!!
$password = 'myAPIpassword'; // CHANGE THIS!!!
$data = array('message'=>'hello','mobile_number'=>'447777777777', 'originator'=>'me',
'username'=>$username, 'password'=>$password);
$data = http_build_query($data, '', '&');
// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlHandle, CURLOPT_POST, 1);
$responseBody = curl_exec($curlHandle);
$responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
// deal with the response
if ($responseInfo['http_code']==200)
{
$xml_obj = simplexml_load_string($responseBody);
// do something with the result
echo "Message ID: $xml_obj->message_id\n";
echo "Credits used: $xml_obj->credits_used\n";
var_dump($responseBody);
} else {
// handle the error
var_dump($responseInfo);
var_dump($responseBody);
}
Example for transferring credits:
<?php/*** POST request on the 'credits' resource (credit transfer)*/// for testing:$username = 'myAPIusername'; // CHANGE THIS!!! - the username of the account you're transferring FROM$password = 'myAPIpassword'; // CHANGE THIS!!!$targetAccountNumber = 0; // CHANGE THIS!!$data = array('quantity'=>'1','target'=>$targetAccountNumber,'username'=>$username, 'password'=>$password);$data = http_build_query($data, '', '&');// we're using the curl library to make the request$curlHandle = curl_init();curl_setopt($curlHandle, CURLOPT_URL, $url);curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $data);curl_setopt($curlHandle, CURLOPT_POST, 1);$responseBody = curl_exec($curlHandle);$responseInfo = curl_getinfo($curlHandle);curl_close($curlHandle);// deal with the responseif ($responseInfo['http_code']==200){// success - you may want to read in the return data here to see how many credits were left on each account} else {// handle the error here// var_dump($responseInfo);// var_dump($responseBody);}?>
For more details on creating REST requests in PHP, see here.
Popularity: 13%
Related posts:
- .NET Examples
Example of a .NET Bulk SMS Gateway / API integration For more information regarding the bulk sending API click here.... - PHP 5 Delivery Report Helper Class for our SMS Gateway
Class Name: ProcessDeliveryReport This class enables you to access your delivery reports in a nice simplified way. You can search for... - PHP 5 SMS Send Class implementing listeners on our SMS Gateway
Class Name: SendSMSNotifier This class is designed around the FREE Text Marketer SMS API, you need to sign up here... - Using our short code and gateway APIs to create a game
The Concept To create an interactive game using Text Messaging and to show how easy it is to create interactive... - Text Marketer short code / txtUs API options
Introduction The keyword on your system (on short code 88802) or your txtUs number can be used to “route” incoming... - SMS Gateway API – Specification
This blog post has been superseded with our website documentation It is recommended that you use the RESTful version of the API which has largely superseded this document... - Using the SMS Gateway API, Automating Delivery Report Collection
This blog post has been superseded with our website documentation Introduction If you are using the API / Gateway to send bulk SMS... - How to use the Text Marketer Text Response system Using Short Code 88802
Introduction This article explains how to use the incoming text feature or shortcode SMS part of your Text Marketer account.... - PHP 5 Simple SMS send Class
This blog post has been superseded with our website documentation 2 classes available in php 5, SendSMS and SendSMSXML (recommended) Class Name: SendSMS... - How to integrate our SMS Gateway API into your systems
Introduction Adding SMS as a communication method to a system is often very useful, for instance you don’t have issues...



