Shaun Mccran

My digital playground

19
F
E
B
2009

Consuming 360 Voices XML data feeds - 360 Voice part 2

www.360voice.com hosts a service where you can interrogate your GamerTag through an XML web feed. They host the service themselves, but also provide an API so that you can remotely call it, and use it however you want. So I thought I'd consume their service using ColdFusion, translate it, and display it here. Firstly I want to try and reduce the service overhead. So I will call the 360 voice service on the first instance of page initialisation, and then write the result to a file. The service only updates once a day, so I can safely assume that caching it daily isn't going to be too out of date. Firstly I've setup some global variables to set the file path location.
view plain print about
1<!--- setup the filename --->
2<cfset variables.filepath = GetBaseTemplatePath()>
3<cfset variables.filepath = replace(variables.filepath, 'include.cfm', '', 'all')>
4<cfset variables.todaysfile = variables.filepath & "tmp\#DateFormat(NOW(), 'dd-mm-yyyy')#.xml">
Then check for the existence of a file with todays date as the name. If the file exists, read it and use it, otherwise make a cfhttp call to the url, passing in any of the filtering url variables that the 360 voice API documents, in this case just my gamer tag.
view plain print about
1<cfif fileExists(variables.todaysfile)>
2    <!--- Read local file --->
3    <cffile action="read" file="#variables.todaysfile#" variable="xmlfile">
4    <cfscript>
5        xmlfile = xmlparse(xmlfile);
6    
</cfscript>    
7<cfelse>
8    <cfhttp url="http://www.360voice.com/api/blog-getentries.asp?tag=ect0z" method="GET" charset="utf-8">
9        <cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
10        <cfhttpparam type="Header" name="TE" value="deflate;q=0">
11    </cfhttp>
12    <cfscript>
13        xmlfile = xmlparse(cfhttp.filecontent);
14    
</cfscript>    
15    <cffile action="write" file="#variables.todaysfile#" output="#xmlfile#">
16</cfif>
In the code above I am also using two cfhttpparams to deflate the return response from the service, as I was having issue with this in a compressed format. (Read more here). Now that we have the content and we've parsed it out into an XML object we need to search through and pick out the elements we want. First we write out the header details from the parent node of the xml document. Create an Array, and map the contents to the child node you want. In this case the "api.info" node. Doing this allows you to treat the previous XML object as a standard Array, so we can loop over it, and pick out the elements we want.
view plain print about
1<cfset arrHeader = xmlfile.api.info>
2<cfoutput>
3    <cfloop index="i" from="1" to="#ArrayLen(arrHeader)#">
4    <img src="#arrHeader[i].tile.XmlText#" alt="Gamer Icon"> - 360 Voice.com Blog
5    <!--- #arrHeader[i].link.XmlText# --->
6    </cfloop>
7</cfoutput>
Now we will do much the same thing with the blog contents, but using a different XML child Node for the Array.
view plain print about
1<cfset arrEntries = xmlfile.api.blog.XmlChildren>
2<cfoutput>
3    <cfloop index="i" from="1" to="#ArrayLen(arrEntries)#">
4        <b>#arrEntries[i].title.XmlText# -#arrEntries[i].date.XmlText#</b><br/>
5        #arrEntries[i].body.XmlText#
6        <p><br/></p>
7    </cfloop>
8</cfoutput>
Again just loop through the Array, picking out the elements you want. Next I will add pagination, and possible look at some of the other data that 360 voice stores in its API, such as gamer badges etc.... but that's another article.
TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top