SMS Gateway HTTP API

Code Examples

Code Examples

The following code examples illustrate how HTTP requests can be programmatically sent to the SMS HTTP Interface and the associated HTTP response read.

 

PHP Example

Send SMS
<?
$myaccount=urlencode("USERNAME");
$mypasswd=urlencode("PASSWORD");
// For default alphabet
$mymsg=urlencode("Hello world");
// For unicode:
//  $unimsg = mb_convert_encoding("Hello world", 'UTF-16BE', 'auto');
//  $mymsg=urlencode($unimsg);
// For iso-8859-1:
//  $isomsg = mb_convert_encoding("Hej världen", 'ISO-8859-1', 'auto');
//  $mymsg = urlencode($isomsg);
$myto="46731111111";
$myfrom=urlencode("4673222222");
$route="G1";
//Contact your Account-manager to check settings for your account
$sendsms = "http://imghttp.fortytwotele.com/api/current/send/message.php" . "?username=$myaccount" .
"&password=$mypasswd" . "&to=$myto" . "&from=$myfrom" . "&message=$mymsg" . "&route=$route";
// For unicode:
// "&encoding=utf8&coding=unicode";
$getsmsstatus=file($sendsms);
$splitstatus = explode(",",$getsmsstatus[0]); if ($splitstatus[0] == "1") {
$msgid = trim($splitstatus[1]);
echo "Message sent to gateway. smsid:" . $msgid;
} else {
$msgerror=trim($splitstatus[1]);
echo "Couldn't send message. Errorcode:" . $msgerror;
}
?>

 

Convert accented characters which are not part of the gsm7 character set to plain characters which are supported. For example, convert Ï to I
function gsm_unsupported_fix($msgIn){
// Replace accented characters in $msgIn (contains message) with standard
// alphabetic character equivalent as these characters are not included in
// the GSM7 character set
$gsmChar=array('ç','ï','Ï','ÿ','Ÿ','â','Â','ê','î','ô','Ô','û','È','À','Ù','Œ','œ','Î','Ë','ë','Ê','ê','Ò','ó','Ì','Ó','«','»','º','ª','Á','Ã','Í','Õ','Ú','á','ã','í','õ','ú');
$gsmHttp=array('c','i','I','y','Y','a','A','e','i','o','O','u','E','A','U','OE','oe','I','E','e','E','e','O','o','I','O','"','"','','','A','A','I','O','U','a','a','i','o','u');
    $msgOut = $msgIn; 
    for($i=0;$i<count($gsmChar);$i++){
        $msgOut=str_replace($gsmChar[$i],$gsmHttp[$i],$msgOut);
    }
    return $msgOut;
}

 

Receive callback and handle callbacks for message status changes
<?
$mysmsid = $_GET["smsid"]; // to get sms id
$mystatus = $_GET["status"]; // to get status
$mydate = $_GET["date"]; // to get date
$myfrom = $_GET["from"]; // to get from
$myto = $_GET["to"]; // to get to
$timestamp = $_GET['timestamp'];
$date = date("Y-m-d H:i:s", $timestamp); //Use to update database etc. echo "OK";
?>

 

JAVA Example

try {
    // Generate the HTTP POST request parameters
    String params = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode("USERNAME", "UTF-8");
    params += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode("PASSWORD", "UTF-8");
    params += "&" + URLEncoder.encode("to", "UTF-8")+ "=" + URLEncoder.encode("46731111111", "UTF-8");
    params += "&" + URLEncoder.encode("from", "UTF-8")+ "=" + URLEncoder.encode("4673222222", "UTF-8");
    params += "&" + URLEncoder.encode("route", "UTF-8")+ "=" + URLEncoder.encode("G1", "UTF-8");
    params += "&" + URLEncoder.encode("message", "UTF-8")+ "=" + URLEncoder.encode("Hello World", "UTF-8");

    // Open a connection to the HTTP interface URL url = new 
URL("http://imghttp.fortytwotele.com/api/current/send/message.php"); URLConnection conn = url.openConnection(); conn.setDoOutput(true);
    // Send the HTTP POST Request OutputStreamWriter wr = new 
OutputStreamWriter(conn.getOutputStream()); wr.write(params);
    wr.flush();
    // Read the HTTP Response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String response = rd.readLine(); System.out.println(response);
} catch (UnsupportedEncodingException e) { e.printStackTrace();
} catch (MalformedURLException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
Get Started