Saturday, September 13, 2008

Add a newsfeed to your site using coldfusion

you can use the moreover news service to add a news feed to your site using cfhttp and wddx.
To begin, you need to know what type of news feed your are interested in. Moreover has a list of pre-built news feeds that are encoded using the wdd standard available here.
Once you've chosen the specific news feed you are interested in, set the url for that feed into the feed variable. The feed below is for "Tech Latest".

<cfset feed = "">
<!--- use cfhttp to get the feed --->
<cfhttp url="#feed#" method="GET" resolveurl="false"></cfhttp>
<!--- use cfwddx to convert the feed results into a query. The query name is defined in the "output" paramter in the tag below --->
<cfwddx action="WDDX2CFML" input="#CFHTTP.FileContent#" output="newsfeed_data">

if no results were found or any other errors occur, make a fake query -
this allows us to avoid having to do additional error handling when the feed results are displayed.

<CFSET newsfeed_data = QueryNew("headline_text, source, harvest_time, URL")>
<!--- make a row in the query --->
<CFSET newRow = QueryAddRow(newsfeed_data, 1)>
<!--- set the cells in the query --->
<CFSET temp = QuerySetCell(newsfeed_data, "headline_text", "No News Items Available",1)>
<style type="text/css">
th {
font-family: Verdana, Helvitica, Arial;
font-size: 78%;
color: white;
background-color: #336600;
font-weight: bold;
.data {
border: 1px solid #003366;
background-color: #003366;

If you want to see what data is available in our query, dump the whole query
<cfdump var="#newsfeed_data#">
To set how many stories will display at one time, you can set the maxrows property of the output query.
To show all stories, set a default to the recordcount of the query. To decrease the number, just type in the
number of stories you want to display.

<cfparam name="maxrows" default="#newsfeed_data.recordcount#">
<!--- display our results --->
<table cellpadding="4" cellspacing="1" class="data" summary="news feed results">
<th>Current <cfoutput>#newsfeed_data.cluster#</cfoutput> Stories</th>
<cfoutput query="newsfeed_data" maxrows="#maxrows#">
<tr bgcolor="##<cfif currentrow mod 2 is 1>FFFFFF<cfelse>CCCCCC</cfif>">
<a href='#url#' target='_blank'>#headline_text#</a><br><span class='news'>(<strong>#source#</strong>) on #harvest_time#</span><br>

No comments: