Accessing the REST API from VB6/VBScript

Help with the Esendex API

Accessing the REST API from VB6/VBScript

Postby Alex » Thu Dec 23, 2010 6:39 pm

Here's a little example of showing how to access the REST API from VB6 or VBScript. It uses the MSXML2.ServerXMLHttp object from the Microsoft MSXML parser but should also work with the older Microsoft.XMLHTTP object. The ServerXMLHttp object should always be used in server environments e.g. from ASP script on a web server.

For information on how to use a proxy server with the ServerXMLHttp object please see the setProxy and setProxyCredentials methods in the MSXML documentation http://msdn.microsoft.com/en-us/library/ms757828(v=VS.85).aspx

The script is a Windows Script Host file so simply paste it into a file ending with .wsf, edit the credentials and double click it to run. The code is only illustrative and should be easy to copy to VBScript or VB6.

Code: Select all
<job id="main">
    <script language="VBScript">
Option Explicit

Dim mUsername
Dim mPassword
Dim mAccountReference

mUsername = "myusername"
mPassword = "mypassword"
mAccountReference = "myaccount"

Const ServiceURL = "http://api.esendex.com/v1.0/messagedispatcher"

Function SubmitCommand(RESTRequest)

    Dim Server
   
    Set Server = CreateObject("MSXML2.ServerXMLHttp")
   
    Server.Open "POST", ServiceURL, False, mUsername, mPassword
   
    Server.setRequestHeader "Content-Type", "text/xml"
    Server.setRequestHeader "Content-Length", Len(RESTRequest)   
    'ERROR HERE
    'On Error GoTo ErrHandle
    Server.send RESTRequest
   
    WScript.Echo "Response HTTP status: " & Server.Status & " " & Server.StatusText   

    SubmitCommand = Server.responseText
    Exit Function
ErrHandle:
       
End Function

Function SendTextMessage(strTo, strBody)

    Dim strCommand     
    Dim responseText   
   
    strCommand ="<?xml version=""1.0"" encoding=""utf-8"" ?>" & vbCrLf & _
                "<messages>" &_
                "    <accountreference>" & mAccountReference & "</accountreference>" &_
                "    <message>" & _
                "       <to>" & strTo & "</to>" & _
                "       <body>" & strBody & "</body>" & _
                "    </message>" &_               
                "</messages>"

    responseText = SubmitCommand(strCommand)                           
    SendTextMessage = responseText
       
End Function

Dim response
WScript.Echo "Sending message"
response = SendTextMessage ("07875488241", "This is a where your text message content should go!")
WScript.Echo "Response: " & response
    </script>
Alex
 
Posts: 1
Joined: Fri Mar 05, 2010 10:20 pm

Return to API



cron
© Esendex Ltd 2010
Company Registration Number: 04217280