HTTP API Sample Code

Sample code for using HTTP API in various programming languages

Amy avatar
Written by Amy
Updated over a week ago

Below is some sample code in various programming languages using HTTP API.


C#

private string SMSGlobalData(string username, string password, string source, string destination, string text)
{

System.Text.StringBuilder postData = new System.Text.StringBuilder("action=sendsms");
postData.Append("&user=");
postData.Append(System.Web.HttpUtility.UrlEncode(username));
postData.Append("&password=");
postData.Append(System.Web.HttpUtility.UrlEncode(password));
postData.Append("&from=");
postData.Append(System.Web.HttpUtility.UrlEncode(source));
postData.Append("&to=");
postData.Append(System.Web.HttpUtility.UrlEncode(destination));
postData.Append("&text=");
postData.Append(System.Web.HttpUtility.UrlEncode(text));

return postData.ToString();

}

private string SendSms(string url, string postData)
{

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] data = encoding.GetBytes(postData);

HttpWebRequest smsRequest = System.Net.WebRequest.Create(url);
smsRequest.Method = "POST";
smsRequest.ContentType = "application/x-www-form-urlencoded";
smsRequest.ContentLength = data.Length;

System.IO.Stream smsDataStream = null;
smsDataStream = smsRequest.GetRequestStream();
smsDataStream.Write(data, 0, data.Length);
smsDataStream.Close();

System.Net.WebResponse smsResponse = smsRequest.GetResponse();

byte[] responseBuffer = new byte[smsResponse.ContentLength];
smsResponse.GetResponseStream().Read(responseBuffer, 0, smsResponse.ContentLength - 1);
smsResponse.Close();

return encoding.GetString(responseBuffer);

}


PHP

<?php  
$username = 'USERNAME';
$password = 'PASSWORD';
$destination = 'PHONE NUMBER';
$source = 'FROM NAME';
$text = 'SAMPLE TEXT';

$content = 'action=sendsms'.
'&user='.rawurlencode($username).
'&password='.rawurlencode($password).
'&to='.rawurlencode($destination).
'&from='.rawurlencode($source).
'&text='.rawurlencode($text);

$smsglobal_response = file_get_contents('https://api.smsglobal.com/http-api.php?'.$content);

//Sample Response
//OK: 0; Sent queued message ID: 04b4a8d4a5a02176 SMSGlobalMsgID:6613115713715266

$explode_response = explode('SMSGlobalMsgID:', $smsglobal_response);

if(count($explode_response) == 2) { //Message Success
$smsglobal_message_id = $explode_response[1];

//SMSGlobal Message ID
echo $smsglobal_message_id;
} else { //Message Failed
echo 'Message Failed'.'<br />';

//SMSGlobal Response
echo $smsglobal_response;
}
?>


cURL

$smsGlobalURL = 'https://api.smsglobal.com/http-api.php?';
$query = http_build_query([
'action' => 'sendsms',
'user' => 'USERNAME',
'password' => 'PASSWORD',
'from' => 'SENDER',
'to' => '614XXXXXXXXX',
'text' => 'TEXT MESSAGE HERE'
]);
$smsGlobalURL = $smsGlobalURL.$query;

// create a new curl resource and set options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $smsGlobalURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);


For any further API related assistance, don't hesitate to reach out to our Customer Support Team.


โ€‹

Did this answer your question?