Text Link Ads ColdFusion Code
July 18, 2007 3:55 PM
Related Categories: blogs, Web Dev
<!--- COLD FUSION CODE BEGINS HERE --->
<!--- This code is compatible with Cold Fusion 6.1 and higher --->
<!--- You must create a blank file named 'local_67489.xml' in --->
<!--- the same directory as this script. It MUST be writable by --->
<!--- the web server. On unix you would CHMOD 666 the file. On --->
<cfset local_file_name = 'local_67489.xml'>
<cfset inventory_key = 'get this from TLA'>
<cfset local_directory = Getdirectoryfrompath(GetBasetemplatepath())>
<cfset local_xml_filename = local_directory & local_file_name>
<cfif not fileExists(local_xml_filename)>
<cfthrow message="Text Link Ads Error: Unable to find local xml file, #local_xml_filename#">
</cfif>
<cffile action="read" file="#local_xml_filename#" variable="localdoc">
<cfdirectory action="list" directory="#local_directory#" filter="#local_file_name#" name="LocalFileDetails">
<cfif (LEN(localdoc) LTE 20) OR (DateCompare(LocalFileDetails.dateLastModified, DateAdd("h", -1, Now())) EQ -1)>
<cfhttp url="http://www.text-link-ads.com/xml.php?inventory_key=#INVENTORY_KEY#" method="GET" timeout="10"></cfhttp>
<cfset remotedoc = cfhttp.FileContent>
<cfif LEN(remotedoc) LTE 20>
<cfset remotedoc = localdoc>
</cfif>
<cffile action = "write" file = "#local_xml_filename#" output = "#remotedoc#">
<cfset localdoc = remotedoc>
</cfif>
<cftry>
<cfset myDoc = XmlParse(localdoc)>
<cfcatch type="Any">
<cfthrow message="Text Link Ads Error: Error parsing local xml file, #local_xml_filename#">
</cfcatch>
</cftry>
<cfset numItems = ArrayLen(mydoc.links.XmlChildren)>
<cfif numItems GTE 1>
<cfset xmlquery = QueryNew("url, text, beforetext, aftertext") >
<cfset temp = QueryAddRow(xmlquery, numItems)>
<cfloop index= "i" from = "1" to = #numItems#>
<cfset temp = QuerySetCell(xmlquery, "url", mydoc.links.link[i].url.XmlText, i)>
<cfset temp = QuerySetCell(xmlquery, "text", mydoc.links.link[i].text.XmlText, i)>
<cfset temp = QuerySetCell(xmlquery, "beforetext", mydoc.links.link[i].beforetext.XmlText, i)>
<cfset temp = QuerySetCell(xmlquery, "aftertext", mydoc.links.link[i].aftertext.XmlText, i)>
</cfloop>
<cfoutput>
<ul style="overflow: hidden; margin: 0; border: 0px; border-spacing: 0px; padding: 0; list-style: none; width: 100%;">
</cfoutput>
<cfoutput query="xmlquery">
<li style="float: left; display: inline; clear: none; width: 100%; margin: 0; padding: 0;"><span style="color: ##000000; display: block; padding: 3px; width: 100%; margin: 0; font-size: 12px;">#beforetext# <a style="font-size: 12px; color: ##000000;" href="#url#">#text#</a> #aftertext#</span></li>
</cfoutput>
<cfoutput>
</ul>
</cfoutput>
</cfif>
<!--- END OF COLD FUSION CODE --->
I can't remember where I got this code from but I am pretty sure it was directly from TLA. You can't find coldfusion code on the text link ads site any more. I have gotten a couple private requests for it, so here ya go. You should wrap this up in some sort of error check, since sometimes the TLA web site goes down. Also, Brian Rinaldi wrote up a handly pod for blogCFC that does this all for you.
Update: TJ also has a TLA project to check out: http://cftextlinkads.riaforge.org/index.cfm
<!--- COLD FUSION CODE BEGINS HERE --->
<!--- This code is compatible with Cold Fusion 6.1 and higher --->
<!--- You must create a blank file named 'local_67489.xml' in --->
<!--- the same directory as this script. It MUST be writable by --->
<!--- the web server. On unix you would CHMOD 666 the file. On --->
<cfset local_file_name = 'local_67489.xml'>
<cfset inventory_key = 'get this from TLA'>
<cfset local_directory = Getdirectoryfrompath(GetBasetemplatepath())>
<cfset local_xml_filename = local_directory & local_file_name>
<cfif not fileExists(local_xml_filename)>
<cfthrow message="Text Link Ads Error: Unable to find local xml file, #local_xml_filename#">
</cfif>
<cffile action="read" file="#local_xml_filename#" variable="localdoc">
<cfdirectory action="list" directory="#local_directory#" filter="#local_file_name#" name="LocalFileDetails">
<cfif (LEN(localdoc) LTE 20) OR (DateCompare(LocalFileDetails.dateLastModified, DateAdd("h", -1, Now())) EQ -1)>
<cfhttp url="http://www.text-link-ads.com/xml.php?inventory_key=#INVENTORY_KEY#" method="GET" timeout="10"></cfhttp>
<cfset remotedoc = cfhttp.FileContent>
<cfif LEN(remotedoc) LTE 20>
<cfset remotedoc = localdoc>
</cfif>
<cffile action = "write" file = "#local_xml_filename#" output = "#remotedoc#">
<cfset localdoc = remotedoc>
</cfif>
<cftry>
<cfset myDoc = XmlParse(localdoc)>
<cfcatch type="Any">
<cfthrow message="Text Link Ads Error: Error parsing local xml file, #local_xml_filename#">
</cfcatch>
</cftry>
<cfset numItems = ArrayLen(mydoc.links.XmlChildren)>
<cfif numItems GTE 1>
<cfset xmlquery = QueryNew("url, text, beforetext, aftertext") >
<cfset temp = QueryAddRow(xmlquery, numItems)>
<cfloop index= "i" from = "1" to = #numItems#>
<cfset temp = QuerySetCell(xmlquery, "url", mydoc.links.link[i].url.XmlText, i)>
<cfset temp = QuerySetCell(xmlquery, "text", mydoc.links.link[i].text.XmlText, i)>
<cfset temp = QuerySetCell(xmlquery, "beforetext", mydoc.links.link[i].beforetext.XmlText, i)>
<cfset temp = QuerySetCell(xmlquery, "aftertext", mydoc.links.link[i].aftertext.XmlText, i)>
</cfloop>
<cfoutput>
<ul style="overflow: hidden; margin: 0; border: 0px; border-spacing: 0px; padding: 0; list-style: none; width: 100%;">
</cfoutput>
<cfoutput query="xmlquery">
<li style="float: left; display: inline; clear: none; width: 100%; margin: 0; padding: 0;"><span style="color: ##000000; display: block; padding: 3px; width: 100%; margin: 0; font-size: 12px;">#beforetext# <a style="font-size: 12px; color: ##000000;" href="#url#">#text#</a> #aftertext#</span></li>
</cfoutput>
<cfoutput>
</ul>
</cfoutput>
</cfif>
<!--- END OF COLD FUSION CODE --->
Like this entry? Subscribe to my blog.


Comments (moderation on)
That code is the CFTExtLinkAds code from RIAForge (http://cftextlinkads.riaforge.org/index.cfm), and open source project. Please adhere to the licensing (Apache 2.0)of the project and keep crediting comments in the code. Much appreciated!
After reviewing the code, I am curious why you would suggest it is better? The code does not verify that the XML file is writable. Additionally, it provides no caching mechanism if the TLA server is down (not uncommon for TLA). I do rather like how the original author has converted the XML to a query, which i considered doing. I would be interested in some feedback :)