# Soap examples
# Java (SOAP)
java
Copy code
import javax.xml.soap.*;
import java.util.ArrayList;
public class Main {
public static SOAPElement SMSContainerElem;
public static SOAPElement sendSMSBodyElem;
public static void main(String args[]) {
String soapEndpointUrl = "https://019sms.co.il/soap?wsdl";
String soapAction = "https://019sms.co.il/soap/";
String token = ""; // TODO: Enter your token here
String message = ""; // TODO: Enter the message you wish to send here
String source = ""; // TODO: Enter your source here
String username = ""; // TODO: Enter your username here
ArrayList<String> destinations = new ArrayList<String>();
//TODO: modify this part to fetch destination from file/db and iterate
over destinations with loop
destinations.add("55999xxxx");
callSoapWebService(soapEndpointUrl, soapAction, username, token, message,
source, destinations);
}
private static void createSoapEnvelope(SOAPMessage soapMessage, String
username, String message, String source) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "xsi";
String myNamespaceURI = "http://www.w3.org/2001/XMLSchema";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
sendSMSBodyElem = soapBody.addChildElement("sendBulkSms");
SOAPElement usernameElem = sendSMSBodyElem.addChildElement("username");
usernameElem.addTextNode(username);
SOAPElement passwordElem = sendSMSBodyElem.addChildElement("password");
passwordElem.addTextNode("");
SMSContainerElem = sendSMSBodyElem.addChildElement("sms");
}
public static void addDestinationToBulk(String phone, String message, String
source) {
SOAPElement SMSBodyElem = null;
try {
SMSBodyElem = SMSContainerElem.addChildElement("sms");
SOAPElement destinationsElem =
SMSBodyElem.addChildElement("destinations");
SOAPElement innerPhoneElem =
destinationsElem.addChildElement("phone");
innerPhoneElem.addTextNode(phone);
destinationsElem.addChildElement(innerPhoneElem);
SOAPElement messageElem = SMSBodyElem.addChildElement("message");
messageElem.addTextNode(message);
SOAPElement sourceElem = SMSBodyElem.addChildElement("source");
sourceElem.addTextNode(source);
SMSContainerElem.addChildElement(SMSBodyElem);
} catch (SOAPException e) {
e.printStackTrace();
}
}
private static void callSoapWebService(String soapEndpointUrl, String
soapAction, String username, String token, String message, String source,
ArrayList<String> destinations) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection =
soapConnectionFactory.createConnection();
// Create SOAP Request
SOAPMessage soapRequest = createSOAPRequest(soapAction, username,token, message, source);
for (String destination : destinations) {
addDestinationToBulk(destination, message, source);
}
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(soapRequest,
soapEndpointUrl);
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println();
soapConnection.close();
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to
Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String soapAction, String
username, String token, String message, String source) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage, username, message, source);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
headers.addHeader("Authorization", "Bearer " + token);
soapMessage.saveChanges();
/* Print the request message, just for debugging purposes */
System.out.println("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println("\n");
return soapMessage;
}
}
# Java (XML)
java
Copy code
public class Http {
public static String post(String urlString,String urlParameters){
URL url;
HttpURLConnection connection = null;
try {
//Create connection url =
new URL(urlString);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", application/xml");
connection.setRequestProperty("Accept","text/html");
connection.setRequestProperty("Accept","application/xhtml+xml");
connection.setRequestProperty("Accept","application/xml");
connection.setRequestProperty("Cache-Control","max-age=0");
connection.setRequestProperty("Accept-Language", "he-IL");
connection.setRequestProperty("Accept-Language", "en");
connection.setRequestProperty("Accept-Language", "en-US");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false); connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ()); wr.write(urlParameters.getBytes());
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer(); while((line =
rd.readLine()) != null) { response.append(line);
response.append('\r');
} rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
public static void main(String[] args) throws UnsupportedEncodingException {
String user ="";
String message =" this is a test message….";
String url="https://019sms.co.il/api";
String xml = "<?xml version='1.0' encoding='UTF-8'?>"
+ "<sms>" +
"<user>" +
"<username>"+user+"</username>" +
"</user>" +
"<source>DemoAPI</source>" +
"<destinations>" +
"<phone>0xxxxxxxxx</phone>" +
"<phone>0xxxxxxxxx</phone>" +
"<phone>0xxxxxxxxx</phone>" +
"<phone>0xxxxxxxxx</phone>" +
"</destinations>" +
"<message>"+ message +"</message>" +
"<add_unsubscribe>0</add_unsubscribe>" +
"<reponse>0</reponse>" +
"</sms>";
String response="";
response = post(url,xml);
System.out.println(response );
}
# PHP
Declare classes
php
Copy code
class Phones {
public $phone=[];
}
class Phone {
public $id;
public $phone;
public $cl_id;
}
class Sms {
public $destinations;
public $message;
public $timing;
public $source;
public $tag;
public $add_unsubscribe;
}
class DlrRequest {
public $id;
public $from;
public $to;
}
set connection
php
Copy code
$url = "https://019sms.co.il/soap?wsdl";
$access_token=your_API_TOKEN_here';
// form an array listing the http header
$httpHeaders = array(
'http' => array('protocol_version' => 1.1,
'header' => "Authorization:Bearer " . $access_token . "\r\n",
));
// form a stream context
$context = stream_context_create($httpHeaders);
// pass it in an array
$params = array('stream_context' => $context);
$wsclient = new SoapClient($url,$params);
Example of sendSms
php
Copy code
$number = '055XXXXXXX';
$number1 = '052XXXXXXX';
$message = 'sendSms test';
$source = 'Test';
$sms = new Sms();
$sms->source = $source;
$sms->message = $message;
$phone = new Phone();
$phone->phone = [$number,$number1]
$phone->id = "externalid1";
$sms->destinations = $phone;
$response = $wsclient->sendSms("username","", $sms);
print_r($response);
Example of sendBulkSms
php
Copy code
$number = '0523961883';
$number1 = '0523961883';
$message = 'sendBulkSms test';
$message1 = 'sendBulkSms test1';
$source = 'Test';
$source1 = 'Test1';
$sms = new Sms();
$sms1 = new Sms();
$sms->source = $source;
$sms->message = $message;
$sms1->source = $source1;
$sms1->message = $message1;
$phone = new Phone();
$phone1 = new Phone();
$phone->phone = $number;
$phone->id = "externalid";
$phone1->phone = $number1;
$phone1->id = "externalid1";
$sms->destinations = $phone;
$sms1->destinations = $phone1;
$bulk=array($sms,$sms1);
$response = $this->wsclient->sendBulkSms("username","", $bulk);
print_r($response);
Example of getDlrReport
php
Copy code
$wsclient = new SoapClient("https://019sms.co.il/soap?wsdl",array(
'encoding' => 'UTF-8'));
$dlr = new DlrRequest();
$dlr->id = array("externalid1","externalid2");
$dlr->from = "01/05/14 00:00";
$dlr->to = "07/05/14 18:29";
$response = $wsclient->getDlrReport("username","",$d);