Creating and Consuming Web Services wi

Document Sample
Creating and Consuming Web Services wi Powered By Docstoc
					  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
    About the Presenter
   8 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
        Understanding Web
            Services
       What is a web service
         Web based application
         Consumed over the Internet
         Communicates and exchanges data
         Application, platform, syntax agnostic
        Understanding Web
            Services
       How do web services work?
         SOAP – Simple Object Access Protocol
         a)   XML based protocol
         b)   Send and receive requests and responses


         WSDL – Web Services Descriptor Language
         a)   Describes arguments accepted
         b)   Describes methods & properties returned
    Creating Web Services
       What is involved?
         Simple ColdFusion Component
         Method access modifier set to “Remote”
         Return data as string
    Creating Web Services
       Building a Web Service - Exercise 1
         Create a new CFC – hello.cfc
         a)   Create new function called “getHello”
         b)   Set access = “remote”
         c)   Return string value

    <cfcomponent output="false">
         <cffunction name="getHello" access="remote" returntype="string">
              <cfreturn "Hello CFUnited" />
         </cffunction>
    </cfcomponent>
    Creating Web Services
       Testing a Web Service - Exercise 2
         Viewing the WSDL
         a) http://dev.cfunited/hello.cfc?wsdl


         Calling Web Service method via URL
         a)   http://dev.cfunited/hello.cfc&method=getHello
            Consuming Web
               Services
       How do I call a web service?
         CFObject tag
         CFInvoke tag
         CreateObject() function
              Consuming Web
                 Services
       Using CFObject – Exercise 3
         Object representing web service
         Referencing Web Service Methods
         a)   Dot notation

    <cfobject webservice="http://dev.cfunited/hello.cfc?wsdl" name="ws">
    <cfoutput>#ws.getHello()#</cfoutput>
            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>
              Consuming Web
                 Services
       Using CreateObject() – Exercise 5
         Object representing web service
         Referencing Web Service Methods
         a)   Dot notation

    <cfset ws = createObject("webservice","http://dev.cfunited/hello.cfc?wsdl") />
    <cfoutput>#ws.getHello()#</cfoutput>
Web Services Revisited
       Creating a more complex web service
         Web services with arguments – Exercise 6
         a)   Create new function called “getPersonalHello”
         b)   Add an argument called “firstName”
         c)   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>
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>
Web Services Revisited
       Consuming Third Party APIs
         SOAP based web services
         a)   Easy to consume
         b)   Type checked
         c)   Simply output returned result
         REST based web services
         a)   Lightweight – not a lot of extra xml markup
         b)   Called using CFHttp
         c)   Process result using XML functions
Web Services Revisited
       Consuming Yahoo Search Service
         http://developer.yahoo.com/search/
         a)   Get an Application ID
         b)   Download the SDK
         c)   Ray Camden's ColdFusion examples
               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
               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()
    Additional Resources
       Adobe Documentation
         ColdFusion MX 8 -- Web services
          http://livedocs.adobe.com/coldfusion/8/
         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
                Summary
   Simple to expose CFCs as web services
   Can consume SOAP and REST services
   Powerful prebuilt APIs from Yahoo, etc.



     Questions: chaz@communitymx.com