# Examples

# Node.js example

js
Copy code
    const fetch = require('node-fetch');

const sendSmsWithXML = async () => {
    try {
        const endPoint = 'https://019sms.co.il/api';
        const phone = '5xxxxxxxx';
        const userName = 'yours_user_name';
        // get yours access token from https://docs.019sms.co.il/guide/
        const accessToken = 'yours_access_token';
        const source = 'DemoAPI';
        const  message= 'This is an example';
        const  body= `<?xml version='1.0' encoding='UTF-8'?>
      <sms>
         <user>
           <username>${userName}</username>
        </user>
        <source>${source}</source>
        <destinations>
        <phone>${phone}</phone>
        </destinations>
        <message>${message}</message>
      </sms>`;
        let ans = await fetch(endPoint, {
            method: 'POST',
            body: body,
            headers: {
                'Content-Type' : 'application/xml',
                'Authorization': `Bearer ${accessToken}`
            }
        }).catch(function(err) {
            console.error('error in fetch:', err);
            return false
        });
        ans = await ans.text();
        return ans;
    } catch (err) {
        console.error('error in sendSms:', err);
        return false
    }
}

// same example but json in the body instead of xml
const sendSmsWithJson = async () => {
    try {
        const endPoint = 'https://019sms.co.il/api';
        const phone = '5xxxxxxxx';
        const userName = 'yours_user_name';
        // get yours access token from https://docs.019sms.co.il/guide/
        const accessToken = 'yours_access_token';
        const source = 'DemoAPI';
        const  message= 'This is an example';
        const  body = {
            sms: {
                user: {
                    username: userName,
                },
                source: source,
                destinations: {
                    phone: phone
                },
                message: message
            }
        };
        let ans = await fetch(endPoint, {
            method: 'POST',
            body: JSON.stringify(body),
            headers: {
                'Content-Type' : 'application/json',
                'Authorization': `Bearer ${accessToken}`
            }
        }).catch(function(err) {
            console.error('error in fetch:', err);
            return false
        });
        ans = await ans.json();
        // check the errors in https://docs.019sms.co.il/sms/errors-and-status.html
        if(ans.status == 0){
            return true
        }else{
            return false
        }
    } catch (err) {
        console.error('error in sendSms:', err);
        return false
    }
}

  

# C# example

csharp
Copy code
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text; // for class Encoding
using System.IO;
namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      String url = "https://019sms.co.il/api";
      String xml = "<?xml version='1.0' encoding='UTF8'?>
      <sms>
        <user>
            <username>xxx</username>
        </user>
        <source>019DemoAPI</source>
        <destinations>
            <phone>0555555555</phone>
            <phone>0555555550</phone>
        </destinations>
        <message>test</message>
      </sms>";
      WebRequest webRequest = WebRequest.Create(url);
      webRequest.Method = "POST";
      byte[] bytes = Encoding.UTF8.GetBytes(xml);
      webRequest.ContentType = "application/xml";
      webRequest.ContentLength = (long) bytes.Length;
      Stream requestStream = webRequest.GetRequestStream();
      requestStream.Write(bytes, 0, bytes.Length);
      requestStream.Close();
      WebResponse response = webRequest.GetResponse();
      Stream responseStream = response.GetResponseStream();
      StreamReader streamReader = new StreamReader(responseStream);
      string result = streamReader.ReadToEnd();
      streamReader.Close();
      responseStream.Close();
      response.Close();
      Console.WriteLine(result);
    }
  }
}

  

# PHP example 1

php
Copy code
    <?php
$url = "https://019sms.co.il/api";
$key = 'put_your_API_TOKEN_here';
$xml ="<?xml version='1.0' encoding='UTF-8'?>
      <sms>
         <user>
           <username>Leeroy</username>
        </user>
        <source>DemoAPI</source>
        <destinations>
        <phone id="someid1">5xxxxxxx</phone>
        <phone id="someid2">5xxxxxxx</phone>
        </destinations>
        <message>This is an example</message>
      </sms>";
$CR = curl_init();
curl_setopt($CR, CURLOPT_URL, $url);
curl_setopt($CR, CURLOPT_POST, 1);
curl_setopt($CR, CURLOPT_FAILONERROR, true);
curl_setopt($CR, CURLOPT_POSTFIELDS, $xml);
curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($CR, CURLOPT_HTTPHEADER, array(
 'charset=utf-8',
 'Content-Type: application/xml',
 'Authorization: Bearer ' . $key
 )
);
$result = curl_exec($CR);
$error = curl_error($CR);
if (!empty($error))
 die("Error: " . $error);
else
 $response = new SimpleXMLElement($result);

  

# PHP example 2

php
Copy code
    <?php
$curl = curl_init();
$key = 'put_your_API_TOKEN_here';
curl_setopt_array($curl, array(
 CURLOPT_URL => "https://019sms.co.il/api",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => "<?xml version='1.0' encoding='UTF-8'?>
 \r\n <sms>\r\n <user>\r\n <username>my_username</username>
 \r\n </user>\r\n <source>sender</source>
 \r\n <destinations>\r\n <phone
id='someid1'>0500123456</phone>
 \r\n </destinations>\r\n <message>test</message>\r\n
</sms>",
 CURLOPT_HTTPHEADER => array(
 "Cache-Control: no-cache",
 "Content-Type: application/xml",
 "Authorization: Bearer ". $key
 ),
));
$response = curl_exec($curl);
48
$err = curl_error($curl);
curl_close($curl);
if ($err) {
 echo "cURL Error #:" . $err;
} else {
 echo $response;
}

  
Last Updated: 9/13/2023, 3:24:18 PM