ColdFusion Web Services

You can make any of your ColdFusion components available to any system you wish over the internet by publishing it as a web service. ColdFusion makes this extremely easy to do. In fact, you only need to add one attribute (access="remote") to any of your component's methods to turn it into a web service.

In practice though, you will determine the requirements before creating your web services.

Creating ColdFusion Web Services

Here, I'll try and use a "real life" scenario.

Lets say you sell books and you want other companies to be able to advertise your books on their own website (we'll call it the "remote site"). If a user clicks on the ad on the remote site, they are redirected to your website (where you try your hardest to close the sale and direct the user to your shopping cart!)

Create the following component and save it as book.cfc

The above web service outputs data in XML format so that remote (and potentially non-ColdFusion) systems can read it. This can be acheived by creating a method that retrieves data from a database. It then loops through the recordset using the <cfoutput> tag. As it loops through, you mark up each field with XML tags and the XMLformat() function and then save it all to a variable using the <cfsavecontent> tag. This variable is what the method returns to the calling page.

Testing/calling your ColdFusion Web Services

You should test your web services thoroughly before publishing for external consumption.

The above code calls the web service using the URL that the web service is located at. Appended to the URL is wsdl. This tells ColdFusion to generate the WSDL description for the web service. This allows the caller (aka consumer) to interface with the web service.

This example uses the <cfdump> tag to output the results of the XML returned by the web service. In practice, the consumer would take the XML and transform it into its required language using XSL Transformations (eXtensible Stylesheet Language for Transformations). The method for doing this will depend on the platform consuming the web service. One of the best things about XML is that it can be transformed into any language the consumer wishes.