API Link
SMS Sending API URL (HTML Output): http://api.greenweb.com.bd/api.php
Or,
SMS Sending API URL (JSON Output): http://api.greenweb.com.bd/api.php?json
SSL Version: You can use https protocol
Generate Token ( From SMS panel):https://gwb.li/token Required Parameters ( For Sending SMS): message, to, token
Request Method: POST or GET
Other API Usages:
URL: http://api.greenweb.com.bd/g_api.php (html output)
URL: http://api.greenweb.com.bd/g_api.php?json (JSON)
Check Balance: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&balance
Check Balance (JSON Format): http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&balance&json
Check SMS Rate: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&rate
Total Sent SMS Stats Using Token: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&tokensms
Total Sent SMS Stats From Your Account: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&totalsms
Total Sent SMS Monthly (within current month) Stats From Your Account: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&monthlysms
Total Sent SMS Monthly (within current month) Stats From The token: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&tokenmonthlysms
Total Sent SMS in Any Month (Write month in m-Y format. Ex: 01-2024) From Your Account: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&monthlysms=11-2024
Total Sent SMS in Any Month (Write month in m-Y format. Ex: 01-2024) From Specific Token: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&tokenmonthlysms=11-2024
SMS Validity: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&expiry
Everything Together: http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&expiry&rate&tokensms&totalsms&monthlysms&tokenmonthlysms
Everything Together (json): http://api.greenweb.com.bd/g_api.php?token=yourtokencodehere&expiry&rate&tokensms&totalsms&monthlysms&tokenmonthlysms&json
Demo Token: 1234567890123456789
Request Methods: POST (recommended) & GET
Parameters: to, message, token
You can use this API in GET and POST method.
First Login Your SMS Portal and Generate Access Token from the below link
https://sms.greenweb.com.bd/index.php?ref=gen_token.php
If you dont have any account you can use this demo token for integrating purposes: 1234567890123456789
It will not send any SMS but will return response like the real token, so for integrating our API with your software you can use it. Please replace this demo token with the real one after completing your integrating.
Request Method & Parameters:
token = Generate it from sms panelto = Recipient Numbers । Format must be +8801xxxxxxxxx or 01xxxxxxxxx (Bangladeshi Number format) message = whatever you want to send
For Example:
POST Method
If you're using GET METHOD you have to URL ENCODE message text. For URL Encoding you can use rawurlencode() in php । if its javascript you can use encodeURI()
JSON Data Format:
By Default Our API Retun Line by Line seperated html response । You can take each line in array and match Ok: for success and for the error you can match Error: word । For example:
Ok: SMS Sent Successfully To +8801xxxxxxxxx
Error: +8801xxxxxxxxx Invalid Number !
You got the above response now take it in an array
$smsresultarray = preg_split('/\r\n|\r|\n/', $smsresult);
now process the array
foreach($smsresultarray as $result) {
$status = mb_substr($result, 0, 2);
if ($status == 'Ok') {
//sms successfully sent do whatever you want
echo $result;
} else {
//failed to sent do whatever you want
echo $result;
}
}
You can get JSON Response by input &json at the end of the url like this http://api.greenweb.com.bd/api.php?json
For example :
<?php
$to = "017xxxxxxx,+88016xxxxxxx";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$message = "Test SMS using API";
//Put &json in here ->
$url = "http://api.greenweb.com.bd/api.php?json";
$data= array(
'to'=>"$to",
'message'=>"$message",
'token'=>"$token",
'json'=>"1",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$smsresult = curl_exec($ch);
//Result
echo $smsresult;
//Error Display
echo curl_error($ch);
?>
Now you'll get output like this :
[
{
"to": "+8801xxxxxxx",
"message": "test sms",
"status": "SENT",
"statusmsg": "SMS Sent Successfully To +8801xxxxxxx"
},
{
"to": "+8801xxxxxxx",
"message": "test sms",
"status": "FAILED",
"statusmsg": "+8801xxxxxxx Invalid Number"
}
]
How to process it in php?
$smsresult = json_decode($smsresult);
foreach ($smsresult as $result) {
if ($result->status == "SENT") {
echo "SMS sent successfully";
echo $result->to;
echo $result->status;
echo $result->statusmsg;
} else {
echo "Failed to send";
echo $result->to;
echo $result->status;
echo $result->statusmsg;
}
}
For sending sms you need three data. 1. to (it is the receiver phone number), 2. message (it is the text you want to send) 3. token (you have to generate it from the panel, it will works as a key)
<?php
//Take Data from your database in your own way in a loop
$dblink = mysqli_connect("localhost", "dbusername", "dbpassword", "dbname");
/* If connection fails throw an error */
if (mysqli_connect_errno()) {
echo "Could not connect to database: Error: ".mysqli_connect_error();
exit();
}
$sqlquery = "SELECT number FROM table_name";
if ($result = mysqli_query($dblink, $sqlquery)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
$number = $row["number"];
$to = "$number,$to";
}
}
// We got all numbers in a string called $to now we will send sms
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$message = "Test SMS using API";
$url = "http://api.greenweb.com.bd/api.php";
$data= array(
'to'=>"$to",
'message'=>"$message",
'token'=>"$token"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$smsresult = curl_exec($ch);
//Result
echo $smsresult;
//Error Display
echo curl_error($ch);
?>
(Many to Many)
Send Different Text to Different numbers simultaneously
JSON Format: [{"to":"+8801xxxxxxxxx","message":"firstsmstest"},{"to":"018xxxxxxxxxxx","message":"secondmesssagehere"}]
(Your message must be urlencoded or rawurldecoded)
Live preview:http://jsfiddle.net/greenwebbd/hp2Lr460/embedded/result
Code:http://jsfiddle.net/greenwebbd/hp2Lr460/embedded/html
Parameters:
token = Generate from above link
smsdata = valid json data containing to and message text
<?php
// Start the session
session_start();
//give your token here
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
function GetRealUserIp($default = NULL, $filter_options = 12582912) {
$HTTP_CLIENT_IP = "";
$HTTP_X_FORWARDED_FOR = $_SERVER["HTTP_X_FORWARDED_FOR"];
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $HTTP_CLIENT_IP = $_SERVER['HTTP_CLIENT_IP']; }
$HTTP_CF_CONNECTING_IP = $_SERVER["HTTP_CF_CONNECTING_IP"];
$REMOTE_ADDR = $_SERVER["REMOTE_ADDR"];
$all_ips = explode(",", "$HTTP_X_FORWARDED_FOR,$HTTP_CLIENT_IP,$HTTP_CF_CONNECTING_IP,$REMOTE_ADDR");
foreach ($all_ips as $ip) {
if ($ip = filter_var($ip, FILTER_VALIDATE_IP, $filter_options))
break;
}
return $ip?$ip:$default;
}
$ip = GetRealUserIp();
echo "<center>";
if (isset($_POST['code'])) {
//csrf protection
if (($_SESSION["csrftoken"]) != ($_POST['csrftoken'])) {
echo "OTP Abuse detected ! Please refresh the page and try again !";
exit();
}
//check for matching
if ($_SESSION["otp"] == $_POST['code']) {
echo "You have verified your mobile number successfully</br>";
$_SESSION['verified'] = "1";
//Reset otp request limit
$_SESSION[$ip] = "0";
} else {
echo "You have entered wrong otp code, try again later.";
}
}
if ((isset($_SESSION['verified'])) AND ($_SESSION['verified'] == '1')) {
//you can post your page content here or below the page.
} else {
if(isset($_POST['otp'])) {
//check csrf for protection
if (($_SESSION["csrftoken"]) != ($_POST['csrftoken'])) {
echo "OTP Abuse detected ! Please refresh the page and try again !";
exit();
} else {
$csrftoken = $_POST['csrftoken'];
}
// Check how many times an otp requested from an ip and take precautions if its high
if ($_SESSION[$ip] > "5") {
echo "You're allowed to request otp for 5 times per session ! Your request is blocked.";
exit();
}
// Generate Random 5 digits otp
$code = substr(md5(mt_rand()), 0, 5);
//send otp to mobile via api
$to = preg_replace("|[^0-9 \+\/]|", '', $_REQUEST['number']);
//message text
$message = "(YourCompanyName) আপনার ওটিপি কোড: $code
@youdomainname, #$code";
$url = "http://api.greenweb.com.bd/api.php";
$data= array(
'to'=>"$to",
'message'=>"$message",
'token'=>"$token"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$smsresult = curl_exec($ch);
$result = mb_substr($smsresult, 0, 2);
if ($result == 'Ok') {
//Per ip OTP Limit
if (isset($_SESSION[$ip])) {
$_SESSION[$ip] = ($_SESSION[$ip] + 1);
} else {
$_SESSION[$ip] = "1";
}
echo "Otp code is successfully sent to your mobile, you may have to wait upto 5 min to receive your code";
// save otp code on the session
$_SESSION["otp"] = $code;
//show code input form
echo "
Enter the verification code below </br>
<form action='' method='POST'>
<input type='text' name='code'>
<input type='hidden' name='csrftoken' value='$csrftoken' >
<button type='submit' value='code' name='otp'>Verify</button>
</form>";
exit();
} else {
echo "Failed to send Otp. Please try again after sometime";
exit();
}
} else {
//generate csrf token it's require to protect your otp sms form from the abusers
$csrftoken = substr(md5(mt_rand()), 0, 15);
$_SESSION["csrftoken"] = $csrftoken;
echo "
Enter your mobile number to receive OTP code </br>
<form action='' method='POST'>
<input type='text' name='number'>
<input type='hidden' name='csrftoken' value='$csrftoken' >
<button type='submit' value='otp' name='otp'>Get Otp</button>
</form>";
exit();
}
//it's not required but for extra safety
exit();
}
?>
function myFunction(sms){
var to = "016150505xx";
var row = 1; //grab cell data from row 1
var col = 1; //grab cell data from column 1
var gwcelldata = SpreadsheetApp.getActiveSheet().getRange(row, col).getValue(); //get active sheet data
var sms = encodeURI("Test Message"+gwcelldata);
var url = "https://sms.greenweb.com.bd/api.php?token=yourtokenhere&to="+to+"&message="+sms+"&submit";
var response = UrlFetchApp.fetch(url);
Logger.log(response);
}
For more information check: https://developers.google.com/apps-script/reference/spreadsheet/sheet
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace GreenwebBDAPI{
class Program{
static void Main(string[] args){
string result = "";
WebRequest request = null;
HttpWebResponse response = null;
try{
String to = "016xxxxxxxx,017xxxxx"; //Recipient Phone Number multiple number must be separated by comma
String token = "entertokencodehere"; //generate token from the control panel
String message = System.Uri.EscapeUriString("my messages"); //do not use single quotation (') in the message to avoid forbidden result
String url = "http://api.greenweb.com.bd/api.php?token=" + token + "&to=" + to + "&message=" + message;
request = WebRequest.Create(url);
// Send the 'HttpWebRequest' and wait for response.
response = (HttpWebResponse) request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
StreamReader reader = new
System.IO.StreamReader(stream, ec);
result = reader.ReadToEnd();
Console.WriteLine(result);
reader.Close();
stream.Close();
} catch (Exception exp){
Console.WriteLine(exp.ToString());
} finally {
if (response != null)
response.Close();
}
}
}
}
using System.Net.Http;
Setup:
It is recommended to instantiate one HttpClient for your application's lifetime and share it.
private static readonly HttpClient client = new HttpClient();
POST:
var values = new Dictionary
{
{ "token", "yourtokenhere" },
{ "to", "016xxxxxxxxx" },
{ "message", "test" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://api.greenweb.com.bd/api.php?", content);
var responseString = await response.Content.ReadAsStringAsync();
Newer library sporting a fluent API and testing helpers. HttpClient under the hood. Portable. Available via NuGet.
using Flurl.Http;
POST
var responseString = await "http://api.greenweb.com.bd/api.php?"
.PostUrlEncodedAsync(new { token = "yourtokencodehere", to = "015xxxxxxx", message = "my message" })
.ReceiveString();
Imports System.Web
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Resources
Public Class greenwebApi
Public Function greenwebApi()
Dim token = "YourTokenCodeHere"
Dim message = "Hello World"
Dim mobile = "017xxxxxxxx"
Dim url As String = "http://api.greenweb.com.bd/api.php?"
Dim strPost As String
strPost = url + "token=" + token _
+ "&to=" + mobile _
+ "&message=" + WebUtility.UrlEncode(message)
Dim request As WebRequest = WebRequest.Create(strPost)
request.Method = "POST"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(strPost)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Console.WriteLine(responseFromServer)
Console.ReadLine()
reader.Close()
dataStream.Close()
response.Close()
If responseFromServer.Length > 0 Then
Return responseFromServer
Else
Return CType(response, HttpWebResponse).StatusDescription
End If
End Function
End Class
Private Sub Command1_Click()
Dim DataToSend As String
Dim objXML As Object
Dim message As String
Dim token As String
Dim smsto As String
Dim URL As String
'Set Your Token Here. Generate it from the control panel
token = "xxxxxxxxxxxxxxxxxxxx"
'set/declare your recipient number here
smsto = "01xxxxxxxxxxx"
'Set your Message here
message = URLEncode(" Hello World This is Greenweb BD ")
' do not change anything below
URL = "http://api.greenweb.com.bd/api.php?"
Set objXML = CreateObject("Microsoft.XMLHTTP")
objXML.Open "POST", URL, False
objXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXML.send "token=" + token + "&to=" + smsto + "&message=" + message
'response box start, You can remove the response code from here
If Len(objXML.responseText) > 0 Then
MsgBox objXML.responseText
End If
'response end remove upto this if you want
End Sub
Function URLEncode(ByVal Text As String) As String
Dim i As Integer
Dim acode As Integer
Dim char As String
URLEncode = Text
For i = Len(URLEncode) To 1 Step -1
acode = Asc(Mid$(URLEncode, i, 1))
Select Case acode
Case 48 To 57, 65 To 90, 97 To 122
' don't touch alphanumeric chars
Case 32
' replace space with "+"
Mid$(URLEncode, i, 1) = "+"
Case Else
' replace punctuation chars with "%hex"
URLEncode = Left$(URLEncode, i - 1) & "%" & Hex$(acode) & Mid$ _
(URLEncode, i + 1)
End Select
Next
End Function