This post is more than 2 years old.
Yesterday I wrote a quick post about using Yahoo's search API. I wanted to follow it up with a slightly more useful example. On of the commenters on my last post noted that Yahoo seemed to respond pretty quickly, and I have to agree. The results take little to no time to return. Anyway, on to the example.
This example once again uses the search API to return results. However, this time I'm doing something different with the results. Let me show the code first and then I'll explain.
<cfset searchTerm = "coldfusion blog"> <cfset mysite = "camdenfamily.com"> <cfset results = "100"> <cfset appid = "no_you_cant_have_this">
<cfset matches = arrayNew(1)>
<cfhttp url="http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=#appid#&query=#urlEncodedFormat(searchTerm)#&results=#results#" result="result" charset="utf-8" />
<cfif len(result.fileContent) and isXml(result.fileContent)>
<cfset xmlResult = xmlParse(result.fileContent)> <cfloop index="x" from="1" to="#xmlResult.resultSet.xmlAttributes.totalResultsReturned#"> <cfset node = xmlResult.resultSet.xmlChildren[x]> <cfset title = node.title.xmlText> <cfset summary = node.summary.xmlText> <cfset iUrl = node.url.xmlText> <cfset clickurl = node.clickurl.xmlText> <cfif findNoCase(mysite, iURL)> <cfset match = structNew()> <cfset match.title = title> <cfset match.summary = summary> <cfset match.url = iurl> <cfset match.position = x> <cfset arrayAppend(matches, match)> </cfif> </cfloop>
<cfif arrayLen(matches)> <cfmail to="firstname.lastname@example.org" from="email@example.com" subject="Your daily ego check."> Here is your daily ego match for #dateFormat(now(), "long")#
<cfloop index="x" from="1" to="#arrayLen(matches)#"> #matches[x].title# (#matches[x].url#) Position: #matches[x].position# #matches[x].summary# </cfloop> </cfmail>
The code begins pretty much as yesterday's did, except I added a new variable, "mysite". Yahoo lets you filter search results by site, but that is not what I'm doing. (Jeff posted a perfect example of that in this comment.) Instead, scroll down to this line:
<cfif findNoCase(mysite, iURL)>
What I've done here is to say that if my site is found in the url then I consider it a match. I add the relevant data to an array of matches. The end of the code block simply checks to see if any matches were found and if so - mails it to me. Here is an example of the output:
Here is your daily ego match for September 25, 2006
Raymond Camden's ColdFusion Blog: BSG spin off in the works (and a quick note) (http://ray.camdenfamily.com/index.cfm/2006/4/27/BSG-spin-off-in-the-works-and-a-quick-note)
A blog for ColdFusion, Java, and other topics.: BSG spin off in the works (and a ... Subscribe to Raymond Camden's ColdFusion Blog. BSG spin off in the works ...
Raymond Camden's ColdFusion Blog: BlogCFC 5 Beta Announced (http://ray.camdenfamily.com/index.cfm/2006/4/28/BlogCFC-5-Beta-Announced)
A blog for ColdFusion, Java, and other topics.: BlogCFC 5 Beta Announced ... Subscribe to Raymond Camden's ColdFusion Blog. BlogCFC 5 Beta Announced ...
So what's the point of all this? I can set up this script to run daily and check the position of my site on Yahoo's search engine when people are searching for "coldfusion blog". (And geeze - 14th? What am I doing wrong? :) This can let me monitor my marketing efforts and see what is working and what is not working.
I hope this "real" example helps!