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 cfinvoke
syntax, see 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 using the cfinvoke tag.
<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>
You can omit a parameter by setting the cfinvokeargument
omit
attribute to "yes"
. If the WSDL specifies that the argument is nillable, ColdFusion MX sets the associated argument to null. If the WSDL specifies minoccurs=0, ColdFusion MX 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.