Stock quote data needs to be retrieved from a webservice and put into a format ColdFusion can use.
ColdFusion allows developers to very easily call a webservice and pass in arguments to those services using the <cfinvoke> and <cfinvokeargument> tags.
Before starting to work with webservices, an acceptable webservice must be found. For this example the stock quote service provided at WebServiceX will be used. ( http://www.webservicex.net/stockquote.asmx?WSDL )
The webservice provided the GetQuote method and accepts a valid stock ticker symbol. In return an XML packet is returned with all the pertinent information about that stock returned. The quotes are delayed by 20 minutes.
The first step is to create an array to hold the results of the webervice calls as well as an array of stock symbols to retrieve quotes for from the webservice.
<cfset quotes = ArrayNew( 1 ) />
<cfset symbols = ["ADBE","LLY"] />
The above code creates arrays two ways. One using the ArrayNew() function and the other using implicit creation that was new to ColdFusion 8.
The next step is to loop over the array of symbols using an array loop and calling the webservice for each symbol. This particular service only allows one symbol to be passed at a time.
<cfloop array="#symbols#" index="i">
<cfinvoke
webservice="http://www.webservicex.net/stockquote.asmx?WSDL"
method="GetQuote"
returnVariable="quote"
>
<cfinvokeargument name="symbol" value="#i#" />
</cfinvoke>
The <cfinvoke> tag takes the arguments webbservice, method and returnVariable. The webservice attributes is the URL to the WSDL file for the webservice. The method attribute is the name of the method to call, in this case, GetQuote. The returnVariable specifies a variable to save the results returned from the webservice.
In order to pass in the required symbol attribute to the webservice the <cfinvokeargument> tag is used. The attrubutes name and value are used to specify the name of the attribute for the service, in this case, symbol and the value of that attributes, in this case, the two symbols ADBE and LLY.
After the service is called, the results needs to be saved the quotes array and the loop ended.
<cfset ArrayAppend( quotes, XMLParse( quote ) ) /> </cfloop>
The webservice returns the data in an XML format so the XMLParse function is used to createa a ColdFusion XML object.
When the code is run and the quotes array is dumped the following is seen:
Once again ColdFusion makes an otherwise difficult task, such as calling webservices, simple and easy!
+