| home / programming / coldfusion / 1 | [previous] [next] |
|
|
If we apply these definitions to the currency exchange web service,
we can easily determine that the method we want to invoke is called getRate(
).[2] It accepts two input parameters of type string:
country1 (currency converting from) and country2
(currency converting to), respectively. By looking at the WSDL, we also know
that we can expect the getRate( ) method to return
a parameter of type float (the exchange rate).
Based on this information, it's simple to write the ColdFusion code to call
the web service and invoke the getRate( ) method,
passing in the names of the country whose currency we want to convert from and
the country whose currency we want to convert to.
If you are using Dreamweaver MX, you can have it write most of
the code for you. Simply expand the CurrencyExchangeService web service under
the Components tab, then grab the float getRate
label and drag and drop it onto the main code editing area. Dreamweaver MX automatically
creates the ColdFusion code necessary to call the web service and invoke the
getRate( ) method. The code should look like this:
<cfinvokewebservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"method="getRate"returnvariable="aRate"><cfinvokeargument name="country1" value="enter_value_here"/><cfinvokeargument name="country2" value="enter_value_here"/></cfinvoke>
At this point, all you need to do is replace the enter_value_here
placeholders for the country1 and country2
parameters with the names of the countries you wish to use in the currency conversion
(for a list of available countries, see http://www.xmethods.net/ve2/ViewListing.po?key=uuid:D784C184-99B2-DA25-ED45-3665D11A12E5).
If you aren't using Dreamweaver MX as your development environment, you can just as easily write the CFML code to call the web service yourself. Example 24-1 shows the code necessary to call the currency exchange web service and convert 1 USD (U.S. dollar) to euros.
Example 24-1: Calling the currency exchange web service
<cfinvokewebservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"method="getRate"returnvariable="aRate"timeout="30"><cfinvokeargument name="country1" value="united states"/><cfinvokeargument name="country2" value="euro"/></cfinvoke><cfoutput>1 USD = #aRate# Euros</cfoutput>
This code should already look somewhat familiar to you. It's basically
the same code used to instantiate a CFC, but instead of using the component
attribute of the cfinvoke tag as we did for instantiating
a CFC, we use the webservice attribute to specify
an absolute URL to the web service's WSDL file or the name of the web service
if it is already registered in the ColdFusion Administrator (we'll talk about
this shortly). The method attribute is also required
and specifies the method in the web service we want to invoke. In this case,
it's the getRate( ) method. The returnvariable
attribute is optional. It specifies a variable name to hold the results of the
method call to the web service invocation. In our example, we assign the results
of the call to getRate( ) to a variable called
aRate. The timeout
attribute is new in ColdFusion MX 6.1. It is an optional attribute that lets
you specify the amount of time in seconds ColdFusion should wait for a web service
request to complete before timing out.
If your ColdFusion server sits behind a firewall and you must go through a proxy server to connect to external web services, you can use the following optional attributes, all new in ColdFusion MX 6.1:
proxyserver="proxy_server"
proxyserver attribute.
Just as when passing arguments to a CFC, you can pass arguments
to a web service's invoked method using the argumentcollection
attribute, cfinvokeargument tag, or as an attribute
of the cfinvoke tag. If more than one passed argument
has the same name, ColdFusion uses the aforementioned order of precedence. In
this example, we use cfinvokeargument tags to pass
arguments to the web service. The cfinvokeargument
tags are child tags of cfinvoke. Since we know
that the currency exchange web service expects two input parameters, country1
and country2, we use the cfinvokeargument
tags to pass these to the web service.
Besides the cfinvoke tag, you can
also call web services using the cfobject tag or
the createObject( ) function. This gives you tremendous
flexibility in how you integrate web services into your ColdFusion applications.
To call the currency exchange web service using the cfobject
tag, your code would look like this:
<cfobjectwebservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"name="ce"><cfset aRate = ce.getRate("united states", "euro")><cfoutput>1 USD = #aRate# Euros</cfoutput>
Here, the cfobject tag calls the
currency exchange web service. The webservice attribute
specifies the absolute URL to the web service's WSDL file or the name of the
web service as registered in the ColdFusion Administrator. The name
attribute specifies the name of a variable to store the instance of the web
service. Methods of the web service are invoked using dot notation. In our example,
we call the getRate( ) method, passing in values
for country1 and country2.
The results of the getRate( ) operation are assigned
to aRate.
If you are using CFScript, you can call a web service using the
createObject( ) function:
<cfscript>ce = createObject("webservice","http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl");aRate = ce.getRate("united states", "euro");</cfscript><cfoutput>1 USD = #aRate# Euros</cfoutput>
The first parameter of the createObject(
) function tells ColdFusion to call a web service. The second parameter
specifies the absolute URL to the web service's WSDL file or the name of the
web service as registered in the ColdFusion Administrator. The call to the instance
of the web service call is assigned to a variable called ce.
Dot notation is used to invoke the getRate( ) method
of the web service. Arguments are passed the same way as with the cfobject
tag.
No matter which method you choose to call the currency exchange web service, the output, shown in Figure 24-3, remains the same.
|
|
| home / programming / coldfusion / 1 | [previous] [next] |
Created: March 27, 2003
Revised: Sept. 1, 2003
URL: http://webreference.com/programming/coldfusion/1