Sample Code

Overview

The Aerialink API is compatible with all major programming languages. We have compiled samples for some of the most common programming languages below for the use with our most popular API resources. Should you require a sample that is not included here, feel free to contact our Aerialink Support Team with a request and one of our engineers will be provide the sample you need.

Messages

Method [POST] : Sending an SMS

PHP Sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
$apiKey = "[Your API Key]";
$apiSecret = "[Your Api Secret]";
$url = "https://apix.aerialink.net/v4/messages/";

$data = array();
$data["source"] = "[Aerialink Number]";
$data["destination"] = "[Mobile Number]";
$data["messageText"] = "Aerialink Send a Message Test";

//Send the message
http_post($url,$data,$apiKey,$apiSecret);

function http_post($url,$data,$username,$password){
//open connection
$ch = curl_init($url);
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
?>

Java Sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.codec.binary.Base64;

/**
* Use in conjunction with TopicPublisher to test the performance of ActiveMQ
* Topics.
*/

public class Test {

public static void main(String[] argv) throws Exception {
String username = "[ YOUR API KEY ]";
String password = "[ YOUR API TOKEN ]";

try {
// Construct data
String data = URLEncoder.encode("destination", "UTF-8") + "=" + URLEncoder.encode("[ Mobile Number ]", "UTF-8");
data += "&" + URLEncoder.encode("source", "UTF-8") + "=" + URLEncoder.encode("[ Long/Short Code ]", "UTF-8");
data += "&" + URLEncoder.encode("messageText", "UTF-8") + "=" + URLEncoder.encode("Aerialink Test Message", "UTF-8");

// Send data
String userPassword = username + ":" + password;
String encoding = Base64.encodeBase64URLSafeString(userPassword.getBytes()) + "==";

URL url = new URL("https://apix.aerialink.net/v4/messages/");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Authorization", "Basic " + encoding);

// Output Request Post Params
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Output response from the se4rver
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}

CS Sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
string uri = "http://apix.aerialink.net/v4/messages";
string parameters = "source=12345&destination=15552223333&messageText=Sample Message
string APIKey = "[insert your api key here]";
string apiSecret = "[insert your token here]";

string result = APISubmit(uri, parameters, APIKey, apiSecret);

string APISubmit(string uri, string parameters, string apiKey, string apiSecret)
{
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";

NetworkCredential submitNetworkCredential = new NetworkCredential();
submitNetworkCredential.UserName = apiKey;
submitNetworkCredential.Password = apiSecret;
// Construct the authorization header string
string usernamePassword = apiKey + ":" + apiSecret;
string AuthorizationHeader = string.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(usernamePassword)));

webRequest.Credentials = submitNetworkCredential;
webRequest.Headers.Add("Authorization", AuthorizationHeader);

webRequest.Credentials = new NetworkCredential(apiKey, apiSecret);
byte[] bytes = Encoding.ASCII.GetBytes(parameters);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Send it
}
catch (WebException ex)
{
//Response.Write(ex.Message); An error happened, put whatever you want here
}
finally
{
if (os != null)
{
os.Close();
}
}

try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{ return null; }
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
return sr.ReadToEnd().Trim();
}
catch (WebException ex)
{
//Response.Write(ex.Message); An error happened, put whatever you want here
}
return null;
} // end HttpPost

This page was last updated 1515185923000