Creating and Consuming Web Services with CFML Chaz Chumley chaz@communitymx.com
What will be Covered Understanding Web Services Creating Web Services Consuming Web Services Error Handling Best Practices June 27th- 30th 2007 www.cfunited.com
About the Presenter 7 yrs CF Experience Application Developer – Lucidus.net Author/Partner – CommunityMx.com Instructor – University of Nevada Las Vegas Adobe User Group Manager – Las Vegas Frequent speaker: Conferences June 27th- 30th 2007 www.cfunited.com
Understanding Web Services What is a web service Web based application Consumed over the Internet Communicates and exchanges data Application, platform, syntax agnostic June 27th- 30th 2007 www.cfunited.com
Understanding Web Services How do web services work? SOAP – Simple Object Access Protocol XML based protocol Send and receive requests and responses WSDL – Web Services Descriptor Language Describes arguments accepted Describes methods & properties returned June 27th- 30th 2007 www.cfunited.com
Creating Web Services What is involved? Simple ColdFusion Component Method access modifier set to “Remote” Return data as string June 27th- 30th 2007 www.cfunited.com
Creating Web Services Building a Web Service - Exercise 1 Create a new CFC – hello.cfc Create new function called “getHello” Set access = “remote” Return string value <cfcomponent output="false"> <cffunction name="getHello" access="remote" returntype="string"> <cfreturn "Hello CFUnited" /> </cffunction> </cfcomponent> June 27th- 30th 2007 www.cfunited.com
Creating Web Services Testing a Web Service - Exercise 2 Viewing the WSDL http://dev.cfunited/hello.cfc?wsdl Calling Web Service method via URL http://dev.cfunited/hello.cfc&method=getHello June 27th- 30th 2007 www.cfunited.com
Consuming Web Services How do I call a web service? CFObject tag CFInvoke tag CreateObject() function June 27th- 30th 2007 www.cfunited.com
Consuming Web Services Using CFObject – Exercise 3 Object representing web service Referencing Web Service Methods Dot notation <cfobject webservice="http://dev.cfunited/hello.cfc?wsdl" name="ws"> <cfoutput>#ws.getHello()#</cfoutput> June 27th- 30th 2007 www.cfunited.com
Consuming Web Services Using CFInvoke – Exercise 4 Calls an instance of the web service <cfinvoke webservice="http://dev.cfunited/hello.cfc?wsdl" method="getHello" returnvariable="ws" /> <cfoutput>#ws#</cfoutput> June 27th- 30th 2007 www.cfunited.com
Consuming Web Services Using CreateObject() – Exercise 5 Object representing web service Referencing Web Service Methods Dot notation <cfset ws = createObject("webservice","http://dev.cfunited/hello.cfc?wsdl") /> <cfoutput>#ws.getHello()#</cfoutput> June 27th- 30th 2007 www.cfunited.com
Web Services Revisited Creating a more complex web service Web services with arguments – Exercise 6 Create new function called “getPersonalHello” Add an argument called “firstName” Return argument in a string <cffunction name="getPersonalHello" access="remote" returntype="string"> <cfargument name="firstName" type="string" default="Insert Name Here" /> <cfreturn "Hello #arguments.firstName#, hope you enjoy CFUnited" /> </cffunction> June 27th- 30th 2007 www.cfunited.com
Web Services Revisited Calling a web service with arguments Passing named argument Using CFInvokeArgument <cfinvoke webservice="http://dev.cfunited/hello.cfc?wsdl" method="getPersonalHello" returnvariable="ws"> <cfinvokeargument name="firstName" value="Chaz" /> </cfinvoke> <cfoutput>#ws#</cfoutput> June 27th- 30th 2007 www.cfunited.com
Web Services Revisited Consuming Third Party APIs SOAP based web services Easy to consume Type checked Simply output returned result REST based web services Lightweight – not a lot of extra xml markup Called using CFHttp Process result using XML functions June 27th- 30th 2007 www.cfunited.com
Web Services Revisited Consuming Yahoo Search Service http://developer.yahoo.com/search/ Get an Application ID Download the SDK Ray Camden's ColdFusion examples June 27th- 30th 2007 www.cfunited.com
Error Handling Any web service can throw errors Using a Try, Catch block Web service stub caching Any changes won't reflect until refreshed Using CF Admin to refresh proxy/stub Programatically refreshing cache Handling Null Value Arguments Omit attribute June 27th- 30th 2007 www.cfunited.com
Best Practices Creating a Web Service Alias Similar to creating a mapping Reference web service url with friendly name Limiting access to Web Service Using isSoapRequest() June 27th- 30th 2007 www.cfunited.com
Additional Resources Adobe Documentation CommunityMX ColdFusion MX 7 -- Web services -- Version 7 http://livedocs.adobe.com/coldfusion/7/ ColdFusion Developer Center http://www.adobe.com/devnet/coldfusion/webservices.html CommunityMX Consuming RSS Feeds in ColdFusion: Part 1 Using the Google API from ColdFusion Consuming the CMX Web Service with ColdFusion June 27th- 30th 2007 www.cfunited.com
Summary Simple to expose CFCs as web services Can consume SOAP and REST services Powerful prebuilt APIs from Yahoo, etc. Questions: chaz@communitymx.com June 27th- 30th 2007 www.cfunited.com