Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

ColdFusion MX Overview – Consuming Web Services

In our last segment, we created a Web service that anybody can use no matter which programming language they are using to call it. All we need to provide them with is our WSDL link, http://127.0.0.1:8500/dateExample.cfc?WSDL.

Lets now see what it’s like to use somebody else’s Web service in our own code. To test the waters, head over to xmethods.net, which has a nice selection of Web services for us to try out. For this example, lets click on the service named “Delayed Stock Quote”. On its page, we see the delayed stock quote WSDL link. Remember what that does?

Look towards the bottom of the “Delayed Stock Quote” service page at the section labeled “Usage Notes.” You’ll see that this Web service accepts a “symbol” argument, and returns a number (a float). If you use Dreamweaver MX, you can enter this WSDL link into its web services palette, and Dreamweaver will automatically register it and understand it. Check it out.

Macromedia ColdFusion web Services Palette DWMX

If you click on the line that says “float getQuote”, you can drag and drop it into your code pane on the right side. When you drag and drop it, the following <CFINVOKE> statement appears:

<CFINVOKE
 WEBSERVICE=”http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl
 METHOD=”getQuote”
 RETURNVARIABLE=”aQuote”>
 <CFINVOKEARGUMENT name=”symbol” value=”enter_value_here”/>
</CFINVOKE>

You should recognize <CFINVOKE> from earlier, but instead of a COMPONENT attribute, we use a WEBSERVICE attribute. With a little tweaking, we’ll create a page that tells us the current price for Macromedia stock.

showStockPrice.cfm

<CFINVOKE
 WEBSERVICE=”http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl
 METHOD=”getQuote”
 RETURNVARIABLE=”aQuote”>
 <CFINVOKEARGUMENT name=”symbol” value=”MACR”/>
</CFINVOKE>

Macromedia is currently at <CFOUTPUT>#aQuote#</CFOUTPUT>.

One thing you can do to make life easier with ColdFusion Web services is register Web services in the ColdFusion Administrator. That way, you can give a Web service a name like “stockGrabber”, and refer to it by that name, instead of a long WSDL path that could potentially change and require you to go through many pages for updating.

Log in to your ColdFusion Administrator (typically located at http://127.0.0.1:8500/cfide/administrator/index.cfm). Within the left navigation, there is a new CFMX link called “Web Services” that you should click on. Type in a name that you would like to use for this Web service. Lets call it “stockGrabber”. Paste our stock-grabbing WSDL link into the field labeled “WSDL URL”. Finally, click “Update Web Service”.

Now that it’s registered, we can call the Web service again, this time by name. For example,

showStockPrice.cfm

<CFINVOKE
 WEBSERVICE=”stockGrabber”
 METHOD=”getQuote”
 RETURNVARIABLE=”aQuote”>
 <CFINVOKEARGUMENT name=”symbol” value=”MACR”/>
</CFINVOKE>

Macromedia is currently at <CFOUTPUT>#aQuote#</CFOUTPUT>.

I expect more and more exciting Web services to start popping up all over the place. As a matter of fact, I just read that Google started Google Web APIs where you can query the Google system via Web services.

You might also check out the Web services sites at IBM and Microsoft, or the site webservices.org, which covers many topics related to Web services.

Comments