Simple WebService Data Consumer (Time string and Apache version).

 

// PHP 5.3.x for Windows can be downloaded here: http://windows.php.net

// WAMPServer can be downloaded here: http://www.wampserver.com/en/

//////////////////////////////////////////////////////////////////////////////////

' Author: SY

' Copyright (c) 2012

' ScriptEZ.API based application which uses facilities from RAD API:

' . Demoing a "Webservice Consumer"

' . Echo() method usage which displays always messages into a console window

'   instead of WScript.Echo() with 2 behaviours for cscript.exe or wscript.exe

' . ScriptEZ.API Remoting Control Interface usage by NPTelnet.exe program

'   (Connect to ScriptEZ.API process with <processid (PID)> as connection name)

'

' Purpose: Consuming a "Webservice like" from a PHP page or other active pages,

'          by using alternatively HTTP/GET or HTTP/POST methods

////////////////////////////////////////////////////////////////////////////////////

' These below const can be modified

const METHOD = "GET"               ' "POST" or "GET"

const HEADER = "7"                 ' size of header area in byte

 

const URL    = "http://localhost"  ' Apache Webserver is running on local machine

 

' POST method

const POST_VARS = "time=1&header=" ' Each variable must be encoded with URLEncode()

const RAW_REMOTEFILENAME = "/requester.php"

 

' GET method

const REMOTEFILENAME = "/requester.php?time&header="

'const REMOTEFILENAME = "/requester.php?loop=100&output=ascii&header"

'const REMOTEFILENAME = "/requester.php?loop=100&output=html&header"

 

const SCRIPTEZ_API = "ScriptEZ.API" ' Component name

' start the script

'/////////////////

Main()

 

' Main Subroutine

'////////////////

Sub Main()

 

  Set WshShell = CreateObject("WScript.Shell")

  Set fso      = CreateObject("Scripting.FileSystemObject")

 

  Dim ScriptEZMain 

  Dim Windir

  Dim FullQualifiedEngineName

  ' if Win64, restart with WScript.exe/CScript.exe 32 bits

  On Error Resume Next

  Windir = WshShell.ExpandEnvironmentStrings("%WinDir%")

  FullQualifiedEngineName = Windir+"\SysWOW64" + Mid(WScript.FullName,InStrRev(WScript.FullName,"\"),Len(WScript.FullName))

  If fso.FolderExists(Windir+"\SysWOW64") And LCase(WScript.FullName) <>  LCase(FullQualifiedEngineName) Then

    WshShell.Run FullQualifiedEngineName + " " + Chr(34) + CStr(WScript.ScriptFullName) + Chr(34)

    Exit Sub

  End If

 

  On Error Resume Next

  ' attempt to instantiate component object with callback interface

  Set ScriptEZMain = WScript.CreateObject(SCRIPTEZ_API)

 

  If Not IsObject(ScriptEZMain) Then

    MsgBox "Please, download and register " + SCRIPTEZ_API + _

           " component from http://sovann.googlepages.com and try again...",vbInformation

    Exit Sub

  End If

 

  ScriptEZMain.ClearConsole

  ScriptEZMain.SetCPUCoreAffinity "0"

  ' Register App for receiving AppEvent(s) with onAppEvent(EventID)

  ScriptEZMain.AppRegister

 

  Dim readTotalBytes, readBytes

  readTotalBytes = 0

  ' endless loop

  Do Until 0

   ' http file opening and executing

   If( METHOD = "GET") Then

     ' (1) using GET method for sending request

     httpFileHandle = ScriptEZMain.OpenHttpFile(URL,REMOTEFILENAME+HEADER,HEADER)

     Title = REMOTEFILENAME + HEADER

   ElseIf(METHOD = "POST") Then

     ' (2) using POST method for sending request

     httpFileHandle = ScriptEZMain.OpenHttpAndPost(URL,RAW_REMOTEFILENAME,POST_VARS + HEADER,"","",HEADER)

     Title = RAW_REMOTEFILENAME

   End If

 

   If httpFileHandle > 0 Then

     ' http file reading

     If ScriptEZMain.HttpReadFile(httpFileHandle) = 1 Then

     readBytes = ScriptEZMain.HttpGetReadBytes(httpFileHandle)

     readTotalBytes = readTotalBytes + readBytes

 

     ' if more than one reading operation, iterate into this loop

     Do Until readBytes <= 0

      ' http file fetching data

      time_string = ScriptEZMain.HttpGetReadBufferAsBSTR(httpFileHandle)

      ScriptEZMain.ClearConsole

      ' just displaying data here....

      ScriptEZMain.Echo vbLF + time_string + " (" + CStr(readBytes) + " Bytes)"

      ScriptEZMain.FreeBSTR time_string

 

      If readTotalBytes < 1024 Then

        ScriptEZMain.SetConsoleTitle CStr(FormatNumber(readTotalBytes,0,0,-2)) + " Bytes read from " + _

                                     URL + Title + " : hdr("+ HEADER + ") by HTTP/" + METHOD

      Else

        ScriptEZMain.SetConsoleTitle CStr(FormatNumber(readTotalBytes/1024,0,0,-2)) + _

                                     " KBytes (" +  CStr(readTotalBytes) + " Bytes) read from " + _

                                     URL Title + " : hdr("+ HEADER + ") by HTTP/" + METHOD

      End If

 

      ' http file reading   

      r = ScriptEZMain.HttpReadFile(httpFileHandle)

      readBytes = ScriptEZMain.HttpGetReadBytes(httpFileHandle)

      readTotalBytes = readTotalBytes + readBytes

     Loop

 

     ' http file closing

     ScriptEZMain.CloseHttpFile httpFileHandle

     End If

   End If 

   ' pause 1 second

   WScript.Sleep 1000

  Loop

 

  ' App unregister before ending

  ScriptEZMain.AppUnregister

  Set ScriptEZMain = Nothing

 

End Sub