Shaun Mccran

My digital playground

22
M
A
Y
2008

Weather Web service (cfc) - Part 1

Recently I've been experimenting with consuming public Web Services and interpreting them through a Flex front end.

So I thought a handy remoting service would be a weather forecast feed consumer.

View the application here.

My first discovery was that due to sandbox security restrictions Flex isn't a great at interfacing with remotely hosted services.

This is mainly down to the crossdomain.xml policy.

Adobe has documented this 'feature' extensively here.

So to get around this I'm using a back end proxy service to do the WS calls, and then feeding the results into Flex, so to the CFC backend!

The Data

Before we look at the cfc code I'll mention that the data used in this is an industry formatted uk location code lookup table. Here's an example:

view plain print about
1id Code Location Name
2165    UKXX0011    Basingstoke
3166    UKXX0012    Bath
4167    UKXX0013    Batley

You can download the entire uk codelist and a sql creation script here.

The CFC

I've got into the habit of creating a new component per flex project, so create a new cfc, weather.cfc

In this cfc we have a standard cfc to flex combo population. We query a database, and do a query of queries to return lowercase data of the right name. (read more here).

view plain print about
1<cffunction name="populateCombo" access="remote" hint="Populates the combo for uk locations">
2    
3        <cfquery datasource="#application.dsn#" name="propQuery">
4            SELECT [varCode], [varLocation]
5            FROM [dbo].[ukLocationCodes]
6            order by varlocation
7        </cfquery>
8        
9        <cfquery dbtype="query" name="qGetCombo">
10            Select varCode as data, varLocation as label
11            from propQuery
12            Order by varLocation
13        </cfquery>
14    
15        <cfreturn qGetCombo>
16    </cffunction>

This will populate a combo box in flex.

Next we have the actual http call to the yahoo weather service. Basically you take the url 'http://weather.yahooapis.com/forecastrss?p=' and append two different variables to it. The first is the uk location code. The second is optional, and specifies wether the return should be in Celsius or Fahrenheit. These are case sensitive, and should be a lower case 'c', or 'f'.

view plain print about
1<cffunction name="getForecast" access="remote" returntype="string" hint="Returns weather details for given uk location">
2        <cfargument name="locationCode" type="string" required="yes">
3        <cfargument name="units" type="string" required="no">
4        
5        <cfset var url = 'http://weather.yahooapis.com/forecastrss?p='>
6        <cfset url = url & arguments.locationCode>
7        
8        <cfif arguments.units NEQ "">
9            
10            <cfif arguments.units EQ 'c'>
11                <cfset url = url & '&u=c'>
12            <cfelseif arguments.units EQ 'f'>
13                <cfset url = url & '&u=f'>
14            </cfif>
15            
16        </cfif>
17        
18        <cfhttp url="#url#" method="get" resolveurl="yes">
19        </cfhttp>
20    
21        <cfset returnVar = CFHTTP.FileContent>
22
23        <cfreturn returnVar>
24    </cffunction>

So thats Part 1 complete, now on to the flex that this populates!

Part 2 is here.

Related Blog Entries

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top