Adobe ColdFusion 8

Using CFScript to consume a web service

The example in this section uses CFScript to consume a web service. In CFScript, you use the CreateObject function to connect to the web service. After connecting, you can make requests to the service. For CreateObject syntax, see the CFML Reference.

After creating the web service object, you can call operations of the web service using dot notation, in the following form:

webServiceName.operationName(inputVal1, inputVal2, ... );

You can handle return values from web services by writing them to a variable, as the following example shows:

resultVar = webServiceName.operationName(inputVal1, inputVal2, ... );

Or, you can pass the return values directly to a function, such as the WriteOutput function, as the following example shows:

writeoutput(webServiceName.operationName(inputVal1, inputVal2, ...) );

Access a web service from CFScript

  1. Create a ColdFusion page with the following content:
    <cfscript>
    ws = CreateObject("webservice", 
    "http://www.xmethods.net/sd/2001/TemperatureService.wsdl");
    xlatstring = ws.getTemp("55987");
    writeoutput(xlatstring);
    </cfscript>
    

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

You can also use named parameters to pass information to a web service. The following example performs the same operation as above, except that it uses named parameters to make the web service request:

<cfscript>
ws = CreateObject("webservice", 
"http://www.xmethods.net/sd/2001/TemperatureService.wsdl");
xlatstring = ws.getTemp(zipcode = "55987");
writeoutput("The temperature at 55987 is " & xlatstring);
</cfscript>