Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

ColdFusion MX Overview – Functionality as a Web Service

First, we’ll need to go back into our .cfc file and let ColdFusion know that we plan on exposing the component as a Web service. We only need to specify that the <CFFUNCTION>s can be accessed remotely as a Web service and that the type of information returned by each <CFFUNCTION> is a “string”. We’ll set the ACCESS attribute of each <CFFUNCTION> to “remote” (meaning Web service) and the RETURNTYPE attribute to “string”. If we wanted to be really spiffy, we would also add the OUTPUT=”FALSE” attribute to the <CFFUNCTION> tags. It’s good practice, because it causes the body of the function to execute as though it were in a <CFSILENT> tag.

Here’s what our .cfc file looks like now:

dateExample.cfc

<CFCOMPONENT HINT=”This component is used to perform very
simple date manipulation. It can tell you the name of the current
day, what yesterday was, or what tomorrow will be. It can also
tell you the day of any valid date that you pass to it.”>
 <CFFUNCTION NAME=”getToday”
  HINT=”Returns the name of the day today.”
  ACCESS=”remote”
  returntype=”string”>
  <CFRETURN  dayofweekAsString(dayofweek(now()))>
 </CFFUNCTION>
 <CFFUNCTION NAME=”getYesterday”
  HINT=”Returns the name of the day yesterday.”
  ACCESS=”remote”
  returntype=”string”>
   <CFRETURN dayofweekAsString(dayofweek(dateadd(“d”, -1, now())))>
 </CFFUNCTION>
 <CFFUNCTION NAME=”getTomorrow”
  HINT=”Returns the name of the day tomorrow.”
  ACCESS=”remote”
  returntype=”string”>
   <CFRETURN dayofweekAsString(dayofweek(dateadd(“d”, 1, now())))>
 </CFFUNCTION>
 <CFFUNCTION NAME=”getThisDayName”
  HINT=”Allows you to pass an arbitrary date. The function
  returns the name of the day that this date falls on.
  Be sure to pass a valid date. For example: ’12/1/2002′.”
  ACCESS=”remote”
  returntype=”string”>
   <CFARGUMENT name=”day”>
   <CFRETURN dayofweekAsString(dayofweek(day))>
 </CFFUNCTION>
</CFCOMPONENT>

 

Now that we’re finished writing the code for our Web service, we’ll now need to understand how to expose it. A Web service has to be accompanied by a Web Services Description Language (WSDL) file that describes the Web service so other systems can make sense of it. ColdFusion components generate WSDL files automatically for us! To access the WSDL file for our Web service, just request the .cfc file directly in your browser followed by “?WSDL” at the end. For example, on my computer, when I request:

http://localhost:8500/dateExample.cfc?WSDL

The browser returns this file seen below.

Macromedia ColdFusion date Example WSDL

You can use 127.0.0.1 or “localhost” to refer to your local machine. The CFMX development Web server installs on port 8500 by default, so that’s why my examples reference port 8500. Web services, in general, will run on the same port as your regular ColdFusion pages unless you intentionally set your server up for some other arrangement.

When you see all of the XML that gets generated, don’t panic. We don’t have to deal with any of it. We are only interested in the WSDL link itself.

Believe it or not, we’re done. The person interested in using the awesome new Web service will need to know the path to the appropriate WSDL file in order to be able to use it, so we’ll need to make it available on our CFMX-enabled Web server. Other systems — whatever the programming language might be — can now use the <CFFUNCTION>s that we’ve defined in our “dateExample” component. This is cool.

Next, we’ll take a look at how to use Web services hosted by somebody else’s system.

Comments