Exemple de implementare
Exemplu CURL
<?php
$apiKey = "APIKEY"; //value provided by suport@web2sms.ro
$nonce = time();
$method = "POST";
$url = "/prepaid/message";
$sender = "";
$recipient = "07xxyyyzzz"; //To be fill in !
$message = "TEST";
//$visibleMessage = "How the message do you want to appearon the interface. If empty string than $message value will beshown";
$scheduleDate = date(); //Format timestamp
$validityDate = ''; //Format timestamp
$callbackUrl = '';
$secret = "SECRET KEY"; // value provided by suport@web2sms.ro
$string = $apiKey . $nonce . $method . $url . $sender .
$recipient . $message . $visibleMessage . $scheduleDate .
$validityDate . $callbackUrl . $secret;
$signature = hash('sha512', $string);
$data = array(
"apiKey" => $apiKey,
"sender" => $sender,
"recipient" => $recipient,
"message" => $message,
"scheduleDatetime" => $scheduleDate,
"validityDatetime" => $validityDate,
"callbackUrl" => $callbackUrl,
"userData" => "",
"visibleMessage" => $visibleMessage,
"nonce" => $nonce);
$curlurl = 'https://www.web2sms.ro/prepaid/message';
$ch = curl_init($curlurl);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":" . $signature);
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Content-length: ' . strlen(json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$postResult = curl_exec($ch);
echo $postResult;
if ($postResult === false)
{
echo "<br/>";
echo ('Curl error: ' . curl_error($ch) . "<br/>");
echo ('Curl error nr: ' .
curl_errno($ch) . "<br/>");
}
curl_close($ch);
example for javascript
/* This is intended to be a very simple implementation in javascript */
var dateTime = require('node-datetime'),
util = require('util'),
crypto = require('crypto'),
http = require('http'),
moment = require('moment'),
https = require('https');
/* Config section */
var secretKEY = "SECRET_KEY"; //to be filled in exactly as provided by web2sms suport timestamp
var apiKEY = "API_KEY"; //to be filled in exactly as provided by web2sms suport timestamp
var method2Sms = "POST";
//var url = "/send/message"; // for postpaid implementation
var url = "/prepaid/message"; // for prepaid implementation
var host2Sms = "www.web2sms.ro";
var httpsPort = 443;
/* End config section */
/* Message information. Later to be extracted from database or other source */
var sender = ''; //to be filled in if the client has rented a large account( type 1xxx or 3XXX) or has bought a label
var recipient = '407XXYYYZZZ'; //phone number
var message = '';//TO BE filled in with the message
var scheduleDatetime = ''; //format timestamp
var validityDatetime = ''; //format timestamp. If empty a default value of 30 days will be set
var callbackUrl = ''; //url to notified when a status message will be received from operator
var usrData = ''; //some proper information based on client implementation
var visibleMessage = ''; //if filled in, this value will be shown in web interface
/* End message information */
function send(senderMsg, recipientMsg, messageMsg, scheduleDateMsg, validityDatetimeMsg, cbkUrlMsg, usrDataMsg = '', visibleMessageMsg = '') {
var d = new Date();
var nonceTime = d.getTime();
var nonceTime = Math.floor(Date.now() / 1000);
var data = {
apiKey: apiKEY,
sender: senderMsg,
recipient: recipientMsg,
message: messageMsg,
scheduleDatetime: scheduleDateMsg,
validityDatetime: validityDatetimeMsg,
callbackUrl: cbkUrlMsg,
userData: usrDataMsg,
visibleMessage: visibleMessageMsg,
nonce: nonceTime
};
// console.log("DATA", data);
var stringMe = apiKEY + data.nonce + method2Sms + url + data.sender + data.recipient + data.message + data.visibleMessage + data.scheduleDatetime + data.validityDatetime + data.callbackUrl + secretKEY;
var hash = crypto.createHash('sha512').update(String(stringMe));
var signature = hash.digest('hex');
jsonObject = JSON.stringify({
"apiKey": apiKEY,
"sender": senderMsg,
"recipient": recipientMsg,
"message": messageMsg,
"scheduleDatetime": scheduleDateMsg,
"validityDatetime": validityDatetimeMsg,
"callbackUrl": cbkUrlMsg,
"userData": usrDataMsg,
"visibleMessage": visibleMessageMsg,
"nonce": nonceTime
});
var auth = "Basic " + new Buffer(apiKEY + ":" + signature).toString("base64");
var postheaders = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(jsonObject, 'utf8'),
'Authorization': auth
};
var optionspost = {
host: host2Sms,
port: httpsPort,
path: url,
method: method2Sms,
headers: postheaders
};
var reqPost = https.request(optionspost, function(res) {
res.setEncoding('utf-8');
res.on('data', function(d) {
var r = new Buffer(d);
//resolve(r.toString()); //if we use a send in a promise call
console.log(JSON.parse(r));
//TODO more to be done like saving the provider response
//TODO error and exceptions processing
});
res.on('end', function() {});
res.on('error', function(e) {
//reject(e); //used for promise call
});
});
reqPost.write(jsonObject);
reqPost.on('error', function(e) {
//console.error(e);
});
reqPost.end();
}
send(sender, recipient, message, scheduleDatetime, validityDatetime, callbackUrl, usrData, visibleMessage);
Example for .NET
try
{
byte[] result;
SHA512 shaM = new SHA512Managed();
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
string apiKey = "";
string nonce = unixTimestamp.ToString();
string method = "POST";
string url = "/prepaid/message";
string sender = "";
string recipient = "07xxxxxxxx";
string message = "mesaj test";
string visibleMessage = "mesaj test";
string scheduleDate = "";
string validityDate = "";
string callbackUrl = "";
string secret = ""; // value provided
byte[] byteArray = Encoding.UTF8.GetBytes(apiKey + nonce + method + url + sender + recipient + message + visibleMessage + scheduleDate + validityDate + callbackUrl + secret);
result = shaM.ComputeHash(byteArray);
string hashString = string.Empty;
foreach (byte x in result)
{
hashString += String.Format("{0:x2}", x);
}
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.web2sms.ro/prepaid/message"); httpWebRequest.ContentType = "application/json";
httpWebRequest.ProtocolVersion = HttpVersion.Version11;
httpWebRequest.Method = "POST";
string _auth = string.Format("{0}:{1}", apiKey, hashString);
string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
string _cred = string.Format("{0} {1}", "Basic", _enc);
httpWebRequest.Headers[HttpRequestHeader.Authorization] = _cred;
Encoding encoding = new UTF8Encoding();
string postData = "{\"apiKey\":\"" + apiKey +
"\",\"sender\":\"" + sender +
"\",\"recipient\":\"" + recipient +
"\",\"message\":\"" + message +
"\",\"scheduleDatetime\":\"" + scheduleDate +
"\",\"validityDatetime\":\"" + validityDate +
"\",\"callbackUrl\":\"" + callbackUrl +
"\",\"userData\":\"" + "" +
"\",\"visibleMessage\":\"" + visibleMessage +
"\",\"nonce\":\"" + nonce + "\"}";
byte[] data = encoding.GetBytes(postData);
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
string s = response.ToString();
StreamReader reader = new StreamReader(response.GetResponseStream());
String jsonresponse = "";
String temp = null;
while ((temp = reader.ReadLine()) != null)
{
jsonresponse += temp;
}
}
catch (WebException e)
{
using (WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream data = response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}
Exemplu verificare valoare curenta a balantei
try
{
//GET
$httpClient = new Zend_Http_Client();
$httpClient->setUri("https://www.web2sms.ro/prepaid/message");
$httpClient->setMethod(Zend_Http_Client::GET);
$httpClient->setHeaders("Content-Type", "application/json");
$httpClient->setHeaders("BALANCE", "APIKEY"); //APIKEY value provided by suport@web2sms.ro
$crtDate = new Zend_Date();
$apiKey = "APIKEY"; //APIKEY value provided by suport@web2sms.ro
$nonce = $crtDate->get(Zend_Date::TIMESTAMP);//Format timestamp
$method = "GET";
$url = "/prepaid/message";
$data = $crtDate->get(Zend_Date::TIMESTAMP);
$secret = "SECRET KEY"; // SECRET KEY value provided by suport@web2sms.ro
$string = $apiKey.$nonce.$method.$url.$secret;
$signature = hash('sha512', $string);
$httpClient->setAuth("APIKEY", $signature); //APIKEY value provided by suport@web2sms.ro
$data = array(
"apiKey" => $apiKey,
"nonce" => $nonce);
$httpClient->setRawData(Zend_Json::encode($data));
$httpResponse = $httpClient->request();
var_dump($httpResponse);
var_dump($httpResponse->getBody());
var_dump(json_decode($httpResponse->getBody()));
$response = json_decode($httpResponse->getBody());
var_dump("BALANCE: " . $response->error->message);
}
catch (Zend_Http_Client_Exception $e)
{
var_dump($e->getMessage());
}
catch (Zend_Uri_Exception $e)
{
var_dump($e->getMessage());
}