textlocal java api code example

Example 1: textlocal java api

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class sendSMS {
	public String sendSms() {
		try {
			// Construct data
			String apiKey = "apikey=" + "yourapiKey";
			String message = "&message=" + "This is your message";
			String sender = "&sender=" + "Jims Autos";
			String numbers = "&numbers=" + "447123456789";

			// Send data
			HttpURLConnection conn = (HttpURLConnection) new URL("https://api.txtlocal.com/send/?").openConnection();
			String data = apiKey + numbers + message + sender;
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Length", Integer.toString(data.length()));
			conn.getOutputStream().write(data.getBytes("UTF-8"));
			final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			final StringBuffer stringBuffer = new StringBuffer();
			String line;
			while ((line = rd.readLine()) != null) {
				stringBuffer.append(line);
			}
			rd.close();

			return stringBuffer.toString();
		} catch (Exception e) {
			System.out.println("Error SMS "+e);
			return "Error "+e;
		}
	}
}

Example 2: txt local sms integration in php example

<?php
	require('textlocal.class.php');
	$otp = rand(1000,9999);
	$textlocal = new Textlocal(false, false,
	'XXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
	$phone = '910123456789'; // ISD code prepended
	$numbers = array($phone);
	$sender = 'TXTLCL';
	$message = 'OTP: '.$otp;

	try {
		$result = $textlocal->sendSms($numbers, $message, $sender);
	} catch (Exception $e) {
		$err['error'] = $e->getMessage();
	}
?>

Tags:

Java Example