Text Link Ads ColdFusion Code

July 18, 2007 3:55 PM
Related Categories: blogs, Web Dev

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)

Whut?
# Posted By Sci Fi Vixen | 7/18/07 4:12 PM
Hi Joshua,

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!
# Posted By TJ Downes | 7/18/07 4:49 PM
Interesting. I have been using and not from riaforge I swear... in fact in thinking about it I think I got it from TLA. Is it possible they got it from ria forge and later stopped publishing the code? At any rate I will update the post to properly link to the source and author. :-)
# Posted By Joshua Cyr | 7/18/07 4:52 PM
Hi Joshua, its all good. I don't think TLA had it on their site. I offered them to use it but never saw it on there. If you happen to remember where you got it please let me know so I can remind the site author to include the licensing info as well :) Thanks!
# Posted By TJ Downes | 7/18/07 4:54 PM
TJ. In looking at your code it is completely different (and better). Is the code I am using an old version of your project? I haven't modified the comments at all but copied them in full from where ever it was I got it, which I am pretty sure was TLA.
# Posted By Joshua Cyr | 7/18/07 5:00 PM
Ok, the earliest I can find the code in use is October of last year so far. Not sure if that helps track anything down or not.
# Posted By Joshua Cyr | 7/18/07 5:04 PM
Joshua, my apologies. You are correct in that this is different code. I guess thats what I get for not comparing my codebase to your post. Ok, i sufficiently feel like an ass now ;)

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 :)
# Posted By TJ Downes | 7/18/07 7:29 PM
Ahh I do see he has a caching method...
# Posted By TJ Downes | 7/18/07 7:31 PM
Just to add an additional comment, the cftextlinkads function runs about 5x faster in testing than the code chunk you have posted averaging 31ms as compared to 150ms using the same xml datasource. I am assuming this is because the other code is utilizing cfdirectory and a cffile read on every request, whereas the cftextlinkads function only uses cffile and only when the file is not returned by TLA or the local xml file is older than 3600 seconds.
# Posted By TJ Downes | 7/18/07 7:41 PM

Sponsors


Savvy Content Manager