Consuming Web Services


ColdFusion consumes Web Services via the <cfinvoke> tag (the same tag used to invoke ColdFusion Components). When invoking Web Services, the webservice attribute is used (instead of the COMPONENT attributethe two are mutually exclusive).

<cfinvoke> and ColdFusion Components were reviewed in Chapter 31, "ColdFusion Components."


The following complete application demonstrates the consumption of a Web Service. It displays a form prompting for text (in English) and a target language, and then translates that text via the BableFish Web Service and displays the translation:

 <!--- Was a form submitted? ---> <cfif IsDefined("FORM.string")>   <!--- Yes, invoke service --->   <cfinvoke webservice="http://www.xmethods.com/sd/BabelFishService.wsdl"             method="babelFish"             returnvariable="aString">     <cfinvokeargument name="translationmode"                       value="#FORM.lang#"/>     <cfinvokeargument name="sourcedata"                       value="#FORM.string#"/>   </cfinvoke>   <!--- Display results --->   <cfoutput>   <strong>Text:</strong> #FORM.string#<br>   <strong>Translation:</strong> #astring#<P>   </cfoutput> </cfif> <!--- Form ---> <cfoutput> Enter some text and select the language you'd like it translated into.<P> <form action="#CGI.SCRIPT_NAME#"        method="post"> Text: <input type="text" name="string"> <select name="lang">   <option value="en_fr">French</option>   <option value="en_es">Spanish</option>   <option value="en_de">German</option>   <option value="en_it">Italian</option>   <option value="en_pt">Portuguese</option> </select> <br> <input type="submit" value="Translate"> </form> </cfoutput> 

This code is both a form and its action, and so the form submits to itself (using CGI.SCRIPT_NAME as the action). The form submits two fields: string is the string to be translated and lang is the target language. A <cfif> statement checks to see if a form has been submitted (it would not have been on the first call); if it has, <cfinvoke> is used to invoke the translation Web Service. The webservice itself is identified by the URL to its WSDL; the method specifies the Web Service method to be invoked; and returnvariable is the name of the variable that contains the result. The two parameters are passed to <cfinvokeargument> tags (although they could have just as easily been name=value pairs in the <cfinvoke> itself).

If the text Hello, my name is Ben is entered, and the target language is French, the output would be bonjour, mon nom est Ben.

ColdFusion handles any conversion between data types automatically. Web Services may return text, numbers, arrays, structures, and more, and ColdFusion automatically converts the results to native data types that can be used just like any other ColdFusion variables.

NOTE

All SOAP and XML processing is handled internally by ColdFusion, so there is no need to interact with Web Services' SOAP and XML directly.


TIP

As easy as ColdFusion makes using Web Services, Dreamweaver makes it even easier. Dreamweaver can process WSDL content natively so as to construct a tree-control- type menu of available methods. By specifying the URL to the WSDL in the Application panel's Component tab, Dreamweaver will display Web Service details and allow Web Service invocation by simply dragging methods into your code.


Proxy Server Considerations

As should be apparent from the example, ColdFusion connects to remote servers to invoke Web Service methods. If a proxy server is used you must provide relevant information to <cfinvoke> (using the proxyserver, proxyport, proxyuser, and proxypassword attributes).

Working With SOAP Headers

ColdFusion deliberately conceals the inner workings of SOAP so as to simply Web Service invocation. However, when greater control is needed, the following functions may be used:

  • AddSOAPRequestHeader() adds headers to a SOAP request before the request is made.

  • GetSOAPResponseHeader() provides access to SOAP headers after the request is processed.

These functions cannot be used if simple <cfinvoke> invocation is used, and require an object type invocation via <cfobject> or CreateObject().



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net