Web Services: Consuming SOAP endpoints
CFML makes it extremely easy to consume and use SOAP within CFML applications. There are two primary ways to consume a remote SOAP endpoint; tag based and function based.
What is common between both methods is that you will need access to the remote WSDL that describes the SOAP endpoint you are attempting to communicate to. The WSDL describes how the client can interact and what methods and types are required.
CFINVOKE
You can use the CFINVOKE tag to remotely execute code against a SOAP server. For example, let us assume we have the WSDL for a remote object (in our case another CFC), that we can invoke commands on. The tag, requires the webservice attribute that describes the URL to the WSDL object. Next, we need to tell the CFINVOKE command which function we wish to execute, using the method attribute. Finally we use the returnvariable to store any result the function will return.
<cfinvoke webservice="http://myhost.com/rpc/mycfc.cfc?wsdl" method="run" returnvariable="result"/> <cfdump var="#result#">
Passing in paramaters is simply done by adding in the attributes for each of the parameters the function requires.
<cfset myStruct = { key1:"data", key2:"sample data" }> <cfinvoke webservice="http://myhost.com/rpc/mycfc.cfc?wsdl" method="run" param1="this is a parameter" param2="#myStruct#" returnvariable="result"/> <cfdump var="#result#">
Alternatively you can use the CFINVOKEARGUMENT embedded tag to pass in arguments to the remote object.
<cfinvoke webservice="http://myhost.com/rpc/mycfc.cfc?wsdl" method="run" returnvariable="result"> <cfinvokeargument name="param1" value="this is a parameter"/> <cfinvokeargument name="param1" value="this is a parameter"/> </cfinvoke> <cfdump var="#result#">
CreateComponent
The alternative to CFINVOKE is to use the function CreateObject which lets you create the remote object and use this a first class object within your CFML application.
remoteObj = CreateObject("webservice", "http://myhost.com/rpc/mycfc.cfc?wsdl"); returnResult = remoteObj.run("param1", "param2");
Once you make the call to CreateObject, the OpenBD engine creates the local object stubs that lets this object exist within the local environment. After that, any call to a method on this function will invoke the function over the wire, remotely to the SOAP endpoint. If you dump this object, you will see all the properties and methods available to this object that you are able to call.
If the network or the server becomes unavailable between the creation and the invocation of a method, then a remote exception will be thrown.