Adobe ColdFusion 8

Using cfinvoke to consume a web service

This section describes how to consume a web service using the cfinvoke tag. With the cfinvoke tag, you reference the WSDL file and invoke an operation on the web service with a single tag.

The cfinvoke tag includes attributes that specify the URL to the WSDL file, the method to invoke, the return variable, and input parameters. For complete syntax, see the CFML Reference.

Note: You can pass parameters to a web service using the cfinvokeargument tag or by specifying parameter names in the cfinvoke tag itself. For more information, see Passing parameters to methods by using the cfinvoke tag.

Access a web service using cfinvoke

  1. Create a ColdFusion page with the following content:
    <cfinvoke 
    webservice="http://www.xmethods.net/sd/2001/TemperatureService.wsdl"
    method="getTemp"
    returnvariable="aTemp">
    <cfinvokeargument name="zipcode" value="55987"/>
    </cfinvoke>
    <cfoutput>The temperature at zip code 55987 is #aTemp#</cfoutput>
    

  2. Save the page as wscfc.cfm in your web root directory.
  3. View the page in your browser.

You can omit a parameter by setting the omit attribute to "yes". If the WSDL specifies that the argument is nillable, ColdFusion sets the associated argument to null. If the WSDL specifies minoccurs=0, ColdFusion omits the argument from the WSDL. However, CFC web services must still specify required="true" for all arguments.

You can also use an attribute collection to pass parameters. An attribute collections is a structure where each structure key corresponds to a parameter name and each structure value is the parameter value passed for the corresponding key. The following example shows an invocation of a web service using an attribute collection:

<cfscript>
    stArgs = structNew();
    stArgs.zipcode = "55987";
</cfscript>

<cfinvoke 
webservice="http://www.xmethods.net/sd/2001/TemperatureService.wsdl"
method="getTemp"
argumentcollection="#stArgs#"
returnvariable="aTemp">
<cfoutput>The temperature at zip code 55987 is #aTemp#</cfoutput>

In this example, you create the structure in a CFScript block, but you can use any ColdFusion method to create the structure.