Using Cfscript To Consume A Web Service - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

<cfinvoke
webservice = "http://www.xmethods.net/sd/2001/BabelFishService.wsdl"
method
argumentCollection = "#stArgs#"
returnVariable = "varName" >
<cfoutput>#varName#</cfoutput>
In this example, you create the structure in a CFScript block, but you can use any ColdFusion
method to create the structure.

Using CFScript to consume a web service

The example in this section uses CFScript to consume a web service. In CFScript, you use the
function to connect to the web service. After connecting, you can make requests
CreateObject
to the service. The
webServiceName = CreateObject("webservice", "URLtoWSDL")
where URLtoWSDL specifies the URL to the WSDL file for the web service.
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
follows:
(
writeoutput
webServiceName.operationName(inputVal1, inputVal2, ...) );
To access a web service from CFScript:
Create a ColdFusion page with the following content:
1
<cfscript>
ws = CreateObject("webservice",
"http://www.xmethods.net/sd/2001/BabelFishService.wsdl");
xlatstring = ws.BabelFish("en_es", "Hello world, friend");
writeoutput(xlatstring);
</cfscript>
Save the page as wscfscript.cfm in your web root directory.
2
View the page in your browser.
3
The following string appears in your browser:
Hola mundo, amigo
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/BabelFishService.wsdl");
xlatstring = ws.BabelFish(translationmode = "en_es",
sourcedata = "Hello world, friend");
</cfscript>
<cfoutput>#xlatstring#</cfoutput>
716
Chapter 32: Using Web Services
= "BabelFish"
function has the following syntax:
CreateObject
function, as
WriteOutput

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Coldfusion mx

Table of Contents