Message-ID: <646478148.15693.1614957639338.JavaMail.confluence@wiki> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_15692_1678730141.1614957639338" ------=_Part_15692_1678730141.1614957639338 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
<?php // Send SMS Example class SendSMS { =09private $url =3D 'https://api.textmarketer.co.uk/services/rest/sms'; // = url of the service =09private $username =3D 'myAPIusername'; // CHANGE THIS!!! =09private $password =3D 'myAPIpassword'; // CHANGE THIS!! =09private $message_id,$credits_used; =09function __construct() =09{ =09} =09public function getMessageID() =09{ =09=09return $this->message_id; =09} =09public function getCreditsUsed() =09{ =09=09return $this->credits_used; =09} =09// public function to commit the send =09public function send($message,$mobile,$originator) =09{ =09=09$url_array=3D array('message'=3D>$message,'mobile_number'=3D>$m= obile, 'originator'=3D>$originator, =09=09'username'=3D>$this->username, 'password'=3D>$this->passw= ord); =09=09$url_string =3D $data =3D http_build_query($url_array, '', '&'); =09=09// we're using the curl library to make the request =09=09$curlHandle =3D curl_init(); =09=09curl_setopt($curlHandle, CURLOPT_URL, $this->url); =09=09curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); =09=09curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $url_string); =09=09curl_setopt($curlHandle, CURLOPT_POST, 1); =09=09$responseBody =3D curl_exec($curlHandle); =09=09$responseInfo =3D curl_getinfo($curlHandle); =09=09curl_close($curlHandle); =09=09return $this->handleResponse($responseBody,$responseInfo); =09} =09private function handleResponse($body,$info) =09{ =09=09if ($info['http_code']=3D=3D200){ // successful submission =09=09=09$xml_obj =3D simplexml_load_string($body); =09=09=09// extract message id and credit usuage =09=09=09$this->message_id =3D (int) $xml_obj->message_id; =09=09=09$this->credits_used =3D (int) $xml_obj->credits_used; =09=09=09return true; =09=09} =09=09else{ =09=09=09$this->message_id =3D null; =09=09=09$this->credits_used =3D null; =09=09=09// error handling =09=09=09return false; =09=09} =09} } /* * Example of use * Remember to change the username and password! */ /* $sms =3D new SendSMS(); if($sms->send("hello this is a test",'07712345678',"Achme= Ltd")) echo "Yay, sent!"; else echo "Boo, not sent"; */ ?>=20
import java.io.DataOutputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.textmarketer.Example.SMSResult.TextMarketerError; public class Example { =09/** =09 * Example code to demonstrate the use of the SendSms(...) static method= . =09 */ =09public static void main(String[] args) { =09=09//Replace these parameters with your own =09=09SMSResult result =3D sendSms("Java Test", "077777777&q= uot;, "SenderId", "username", "password"); =09=09if (result.errors =3D=3D null) { =09=09=09//Show the results of a successful request =09=09=09System.out.println("SMS sent OK on " + result.processedD= ate.toString() + ". MessageID=3D" + result.messageId + ", Cr= edits Used=3D" + result.creditsUsed); =09=09} =09=09else { =09=09=09//Show the results on an unsuccessful request =09=09=09System.out.println("SMS sending failed with the following err= ors:"); =09=09=09for (TextMarketerError error : result.errors) { =09=09=09=09System.out.println("Code: " + error.errorCode + "= ; Text: " + error.errorText); =09=09=09} =09=09} =09} =09/** =09 * This method sends an SMS request to the Text Marketer SMS web service= , and returns details of the result of the request. =09 * @param message The body of the text message to send =09 * @param mobileNumber The phone number to send the text message to =09 * @param originator Text or a phone number, to indicate who the sender = is to the recipient =09 * @param username The username of your Text Marketer account =09 * @param password The password of your Text Marketer account =09 * @return An object representing the result of the call to the web serv= ice =09 */ =09public static SMSResult sendSms(String message, String mobileNumber, Str= ing originator, String username, String password) { =09=09// Create a string representing the data elements to post, then conve= rt it to a byte array =09=09StringBuilder postData =3D new StringBuilder(); =09=09try { =09=09=09postData.append("message=3D").append(URLEncoder.encode(m= essage, "UTF-8")); =09=09=09postData.append("&mobile_number=3D").append(URLEncod= er.encode(mobileNumber, "UTF-8")); =09=09=09postData.append("&originator=3D").append(URLEncoder.= encode(originator, "UTF-8")); =09=09=09postData.append("&username=3D").append(URLEncoder.en= code(username, "UTF-8")); =09=09=09postData.append("&password=3D").append(URLEncoder.en= code(password, "UTF-8")); =09=09} =09=09catch (UnsupportedEncodingException e) { =09=09} =09=09String postDataStr =3D postData.toString(); =09=09URL url; =09=09HttpURLConnection connection =3D null; =09=09try { =09=09=09// Create connection =09=09=09url =3D new URL("https://api.textmarketer.co.uk/services/rest= /sms"); =09=09=09connection =3D (HttpURLConnection) url.openConnection(); =09=09=09connection.setRequestMethod("POST"); =09=09=09connection.setRequestProperty("Content-Type", "appl= ication/x-www-form-urlencoded"); =09=09=09connection.setRequestProperty("Content-Length", "&q= uot; + Integer.toString(postDataStr.getBytes().length)); =09=09=09connection.setUseCaches(false); =09=09=09connection.setDoInput(true); =09=09=09connection.setDoOutput(true); =09=09=09// Send request =09=09=09DataOutputStream wr =3D new DataOutputStream(connection.getOutputS= tream()); =09=09=09wr.writeBytes(postDataStr); =09=09=09wr.flush(); =09=09=09wr.close(); =09=09=09// Get Response =09=09=09InputStream inputStreams =3D connection.getInputStream(); =09=09=09// Convert the response data into an XML document and parse the ex= pected =09=09=09// elements to get the individual response fields =09=09=09DocumentBuilderFactory factory =3D DocumentBuilderFactory.newInsta= nce(); =09=09=09DocumentBuilder builder =3D factory.newDocumentBuilder(); =09=09=09Document document =3D builder.parse(inputStreams); =09=09=09String date =3D document.getElementsByTagName("response"= ).item(0).getAttributes().getNamedItem("processed_date").getNodeV= alue(); =09=09=09String reFromattedDate =3D date.substring(0, 22) + date.substring(= 23); =09=09=09SimpleDateFormat format =3D new SimpleDateFormat("yyyy-MM-dd'= T'HH:mm:ssZ"); =09=09=09Date processedDate =3D format.parse(reFromattedDate); =09=09=09String messageId =3D document.getElementsByTagName("message_i= d").item(0).getFirstChild().getNodeValue(); =09=09=09int creditsUsed =3D Integer.parseInt(document.getElementsByTagName= ("credits_used").item(0).getFirstChild().getNodeValue()); =09=09=09SMSResult result =3D new SMSResult(processedDate, messageId, credi= tsUsed, null); =09=09=09return result; =09=09} =09=09catch (Exception e) { =09=09=09try { =09=09=09=09InputStream errorStream =3D connection.getErrorStream(); =09=09=09=09// Convert the response data into an XML document and parse the= expected =09=09=09=09// error elements to get the individual response fields =09=09=09=09DocumentBuilderFactory factory =3D DocumentBuilderFactory.newIn= stance(); =09=09=09=09DocumentBuilder builder =3D factory.newDocumentBuilder(); =09=09=09=09Document document =3D builder.parse(errorStream); =09=09=09=09String date =3D document.getElementsByTagName("response&qu= ot;).item(0).getAttributes().getNamedItem("processed_date").getNo= deValue(); =09=09=09=09String reFromattedDate =3D date.substring(0, 22) + date.substri= ng(23); =09=09=09=09SimpleDateFormat format =3D new SimpleDateFormat("yyyy-MM-= dd'T'HH:mm:ssZ"); =09=09=09=09Date processedDate =3D format.parse(reFromattedDate); =09=09=09=09NodeList errorNodeList =3D document.getElementsByTagName("= error"); =09=09=09=09TextMarketerError[] errors =3D new TextMarketerError[errorNodeL= ist.getLength()]; =09=09=09=09for (int i =3D 0; i < errorNodeList.getLength(); i++) { =09=09=09=09=09Node errorNode =3D errorNodeList.item(i); =09=09=09=09=09String errorCode =3D errorNode.getAttributes().getNamedItem(= "code").getNodeValue(); =09=09=09=09=09String errorText =3D errorNode.getFirstChild().getNodeValue(= ); =09=09=09=09=09TextMarketerError error =3D new TextMarketerError(Integer.pa= rseInt(errorCode), errorText); =09=09=09=09=09errors[i] =3D error; =09=09=09=09} =09=09=09=09SMSResult result =3D new SMSResult(processedDate, null, 0, erro= rs); =09=09=09=09return result; =09=09=09} =09=09=09catch (Exception e2) { =09=09=09} =09=09} =09=09finally { =09=09=09if (connection !=3D null) { =09=09=09=09connection.disconnect(); =09=09=09} =09=09} =09=09return null; =09} =09/** =09 * Encapsulates the results of a call to the Text Marketer web service. =09 */ =09public static class SMSResult { =09=09public Date processedDate; =09=09public String messageId; =09=09public int creditsUsed; =09=09public TextMarketerError[] errors; =09=09public SMSResult(Date processedDate, String messageId, int creditsUse= d, TextMarketerError[] errors) { =09=09=09this.processedDate =3D processedDate; =09=09=09this.messageId =3D messageId; =09=09=09this.creditsUsed =3D creditsUsed; =09=09=09this.errors =3D errors; =09=09} =09=09public static class TextMarketerError { =09=09=09public int errorCode; =09=09=09public String errorText; =09=09=09public TextMarketerError(int errorCode, String errorText) { =09=09=09=09this.errorCode =3D errorCode; =09=09=09=09this.errorText =3D errorText; =09=09=09} =09=09} =09} }=20
using System; using System.Text; using System.Net; using System.IO; using System.Xml; using System.Collections; namespace TextMarketer { =09class Example { =09=09/// <summary> =09=09/// Example code to demonstrate the use of the SendSms(...) static me= thod. =09=09/// </summary> =09=09/// <param name=3D"args"></param> =09=09static void Main(string[] args) { =09=09=09//Declare the OUT parameters that are used to return the results o= f the SendSms request =09=09=09DateTime processedDate; =09=09=09string messageId; =09=09=09int creditsUsed; =09=09=09TextMarketerError[] errors; =09=09=09//Replace these parameters with your own =09=09=09bool success =3D SendSms("This is my message", "018= 118055", "me", "myUserName", "myPassword"= ;, out processedDate, out messageId, out creditsUsed, out errors); =09=09=09if (success) { =09=09=09=09//Show the results of a successful request =09=09=09=09Console.WriteLine("SMS sent OK on " + processedDate.T= oString() + ". MessageID=3D" + messageId + ", Credits Used= =3D" + creditsUsed); =09=09=09} =09=09=09else { =09=09=09=09//Show the results on an unsuccessful request =09=09=09=09Console.WriteLine("SMS sending failed with the following e= rrors:"); =09=09=09=09foreach (TextMarketerError error in errors) { =09=09=09=09=09Console.WriteLine("Code: " + error.errorCode + &qu= ot; Text: " + error.errorText); =09=09=09=09} =09=09=09} =09=09} =09=09/// <summary> =09=09/// This method sends an SMS request to the Text Marketer SMS web ser= vice, and returns details of the result of the request. =09=09/// </summary> =09=09/// <param name=3D"message">The body of the text mess= age to send</param> =09=09/// <param name=3D"mobileNumber">The phone number to = send the text message to</param> =09=09/// <param name=3D"originator">Text or a phone number= , to indicate who the sender is to the recipient</param> =09=09/// <param name=3D"username">The username of your Tex= t Marketer account</param> =09=09/// <param name=3D"password">The password of your Tex= t Marketer account</param> =09=09/// <param name=3D"processedDate">Returns the date an= d time at which the server processed your request</param> =09=09/// <param name=3D"messageId">Returns a unique Id by = which the server identifies the message</param> =09=09/// <param name=3D"creditsUsed">Returns the number of= Text Marketer credits consumed by sending the text message</param> =09=09/// <param name=3D"errors"></param>Returns an a= rray of TextMarketerError objects identifying an errors in your request. =09=09/// <returns>True if the request was successful, false if the s= erver could not process your request (see 'errors' out-parameter for detail= s)</returns> =09=09public static bool SendSms(string message, string mobileNumber, strin= g originator, string username, string password, out DateTime processedDate,= out string messageId, out int creditsUsed, out TextMarketerError[] errors)= { =09=09=09//Create a string representing the data elements to post =09=09=09StringBuilder postData =3D new StringBuilder(); =09=09=09postData.Append("message=3D").Append(message); =09=09=09postData.Append("&mobile_number=3D").Append(mobileNu= mber); =09=09=09postData.Append("&originator=3D").Append(originator)= ; =09=09=09postData.Append("&username=3D").Append(username); =09=09=09postData.Append("&password=3D").Append(password); =09=09=09//Encode the data string into ASCII bytes =09=09=09ASCIIEncoding encoding =3D new ASCIIEncoding(); =09=09=09byte[] data =3D encoding.GetBytes(postData.ToString()); =09=09=09//Create the web request to the Text Marketer web service and atta= ch the data =09=09=09HttpWebRequest webRequest =3D (HttpWebRequest)WebRequest.Create(&q= uot;https://api.textmarketer.co.uk/services/rest/sms"); =09=09=09webRequest.Method =3D "POST"; =09=09=09webRequest.ContentType =3D "application/x-www-form-urlencoded= "; =09=09=09webRequest.ContentLength =3D data.Length; =09=09=09Stream newStream =3D webRequest.GetRequestStream(); =09=09=09newStream.Write(data, 0, data.Length); =09=09=09newStream.Close(); =09=09=09string resultString; =09=09=09try { =09=09=09=09//Action the post to the server and get the response data strea= m =09=09=09=09HttpWebResponse webResponse =3D (HttpWebResponse)webRequest.Get= Response(); =09=09=09=09using (StreamReader streamReader =3D new StreamReader(webRespon= se.GetResponseStream())) { =09=09=09=09=09resultString =3D streamReader.ReadToEnd(); =09=09=09=09=09streamReader.Close(); =09=09=09=09} =09=09=09=09//Convert the response data into an XML document and parse the = expected elements to get the individual response fields =09=09=09=09XmlDocument xmlDocument =3D new XmlDocument(); =09=09=09=09xmlDocument.LoadXml(resultString); =09=09=09=09processedDate =3D DateTime.Parse(xmlDocument.GetElementsByTagNa= me("response")[0].Attributes.GetNamedItem("processed_date&qu= ot;).Value); =09=09=09=09messageId =3D xmlDocument.GetElementsByTagName("message_id= ").Item(0).InnerText; =09=09=09=09creditsUsed =3D Int32.Parse(xmlDocument.GetElementsByTagName(&q= uot;credits_used").Item(0).InnerText); =09=09=09=09errors =3D null; =09=09=09=09return true; =09=09=09} =09=09=09catch (WebException e) { =09=09=09=09//If we get here, the service returned a server error, so read = the server response from the exception =09=09=09=09using (StreamReader streamReader =3D new StreamReader(e.Respons= e.GetResponseStream())) { =09=09=09=09=09resultString =3D streamReader.ReadToEnd(); =09=09=09=09=09streamReader.Close(); =09=09=09=09} =09=09=09=09//Convert the response data into an XML document and parse the = expected error elements to get the individual response fields =09=09=09=09XmlDocument xmlDocument =3D new XmlDocument(); =09=09=09=09xmlDocument.LoadXml(resultString); =09=09=09=09processedDate =3D DateTime.Parse(xmlDocument.GetElementsByTagNa= me("response")[0].Attributes.GetNamedItem("processed_date&qu= ot;).Value); =09=09=09=09XmlNodeList errorsNodeList =3D xmlDocument.GetElementsByTagName= ("errors"); =09=09=09=09XmlNodeList errorNodeList =3D errorsNodeList.Item(0).ChildNodes= ; =09=09=09=09errors =3D new TextMarketerError[errorNodeList.Count]; =09=09=09=09IEnumerator errorNodeListEnum =3D errorNodeList.GetEnumerator()= ; =09=09=09=09int i =3D 0; =09=09=09=09while (errorNodeListEnum.MoveNext()) { =09=09=09=09=09XmlNode errorNode =3D (XmlNode)errorNodeListEnum.Current; =09=09=09=09=09string errorCode =3D errorNode.Attributes.GetNamedItem("= ;code").Value; =09=09=09=09=09string errorText =3D errorNode.InnerText; =09=09=09=09=09TextMarketerError error =3D new TextMarketerError(Int32.Pars= e(errorCode), errorText); =09=09=09=09=09errors[i++] =3D error; =09=09=09=09} =09=09=09=09messageId =3D null; =09=09=09=09creditsUsed =3D 0; =09=09=09=09return false; =09=09=09} =09=09} =09=09/// <summary> =09=09/// Represents an individual error response element from the Text Mar= keter SMS service =09=09/// </summary> =09=09public class TextMarketerError { =09=09=09public int errorCode; =09=09=09public String errorText; =09=09=09public TextMarketerError(int errorCode, string errorText) { =09=09=09=09this.errorCode =3D errorCode; =09=09=09=09this.errorText =3D errorText; =09=09=09} =09=09} =09} }=20
You must know!
=20 =20Our example code is an illustra= tion of how you might integrate with our systems and is not certified for p= roduction environments. You are responsible for testing and QA.