|
||||||
A Simple Windows Desktop Application in VBScriptHow to use the Ajax Technique with VBScript
If anyone thought that Ajax was limited to web pages, or even Javascript, then they's be wrong. This article shows how to obtain stock quotes without a web browser.
Every programmer involved with the Internet will be aware of Ajax (Asynchronous Javascript and XML), but they may not realize that the techniques involved are not limited to Javascript alone - the techniques can also be used, for example, with VBScript; and this raises an interesting possibility - the ability to use the Ajax technique to create a Windows Desktop application. All that's needed for the Ajax technique to work with VBScript is:
In this example:
The first step, therefore, is to set up the PHP script on the server. Setting up the Server Side of the ApplicationIn the final application the VBScript will call a PHP script and, as with any code, that script should (wherever possible):
And the PHP library for this application will look like: <?php
function get_stock_quote ($company_symbol) {
/* Format the url to be sent to the server */
$url = "http://quote.yahoo.com/d/quotes.csv?"
. "s=" . $company_symbol . "&f=l1&e=.csv";
/* Set the expected number of bytes to be returned */
$filesize = 2000;
/* Open the data stream */
$handle = fopen($url, "r");
/* Read the data */
$quote = fread($handle, $filesize);
/*Close the data stream */
fclose($handle);
/* Output the result */
echo $quote;
}
?>
If the script has been saved in a library (named, for example, yahoo_library.php) then it can be called from a PHP script (and it's this PHP script that will be called from VBScript): <?php
/* Load the library containing the function */
include ("yahoo_library.php");
/* Obtain the company symbol from the variables sent to the script */
$company_symbol = $_REQUEST["company_symbol"];
/* Pass the company symbol to the function */
get_stock_quote ($company_symbol);
?>
Testing the Server Side of the ApplicationBefore going any further it's a well to test the PHP script - and all that's needed for that is to enter the correct url into a web browser; and that url must contain a valid Yahoo! Finance company acronym; so, if the PHP script has been saved as yahoo_stock.php then the url will be something like: http://<my server>/yahoo_stock.php?company_symbol=NOVL
The resulting web page will display the current stock quote for Novell Inc. The Yahoo! Finance VBScript Client ApplicationAll of the hard work has now been done (mainly by Yahoo! Finance) and so all that's left to do is to write a simple piece of VBScript that will:
This can easily be accomplished in a few lines of code: ' The script will accept an input from either
' * the command line
' * an input box
if WScript.Arguments.Count <> 1 then
company_symbol = inputbox ("Please enter a company achronym")
else
'The company symbol is the first (and only) input variable
company_symbol = WScript.Arguments.Item(0)
end if
' Format the url
url = "http://192.168.1.3/yahoo_stock.php?" _
& "company_symbol=" & company_symbol
' Create the XML HTTP object
Set xmlhttp = CreateObject("microsoft.xmlhttp")
' Send the url to the server
xmlhttp.open "get", url, false
xmlhttp.send ""
' Output the result
if WScript.Arguments.Count <> 1 then
msgbox(xmlhttp.responseText)
else
WScript.Echo xmlhttp.responseText
end if
' Finish by cleaning up any objects created
set xmlhttp = nothing
Running the Yahoo! Finance VBScript Client ApplicationIf the VBScript is saved as yahoo.vbs then it can be run in either of two ways:
ConclusionThis simple example shows that the Ajax technique is not the preserve of web sites, and that the same on-line sources can easily be used to create Windows Desktop applications. Further ReadingUsing Yahoo! Financial Stock Quotes Currency Conversion with Yahoo! Finance Accessing Yahoo! Finance from PHP
The copyright of the article A Simple Windows Desktop Application in VBScript in Windows Programming is owned by Mark Alexander Bain. Permission to republish A Simple Windows Desktop Application in VBScript in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||