I'm continuing my journey into the Yahoo API. Today let's take a look at Yahoo Answers. This is a service where people can ask questions and other people (hopefully bright, intelligent reasonable people, which the net is full of) then answer. While you can use the web to search for Yahoo Answers, you can also use a REST based API. The documentation for this API may be found here:
http://developer.yahoo.com/answers/V1/questionSearch.html
It is pretty simple so I won't go over it too deeply. Basically you can do free form searches along with searches on question status (is it already solved?), category, and date ranges. A good "real world" example of this would be a search for unanswered questions in your area of expertise. You could then try to answer the question and increase your rep with Yahoo Answers. (That plus 2.99 will buy you a cup of coffee.)
So let's take a look at a quick example:
<cfset searchTerm = "coldfusion">
<cfset results = "10">
<cfset appid = "dharma_rules_me">
<cfhttp url="http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=#appid#&query=#urlEncodedFormat(searchTerm)#&results=#results#&date_range=7" result="result" charset="utf-8" />
<cfif len(result.fileContent) and isXml(result.fileContent)>
<cfset xmlResult = xmlParse(result.fileContent)>
<cfif structKeyExists(xmlResult, "Error")>
<cfoutput>
An error occured while performing your query. The error was:<br />
#xmlResult.Error.Message#
</cfoutput>
<cfelse>
<cfset numResults = arrayLen(xmlResult.ResultSet.Question)>
<cfoutput>
Your search for #searchTerm# questions returned #numResults# results.
</cfoutput>
<cfloop index="x" from="1" to="#numResults#">
<cfset node = xmlResult.resultSet.Question[x]>
<cfset questiontype = node.xmlAttributes.type>
<cfset subject = node.subject.xmlText>
<cfset question = node.content.xmlText>
<cfset date = node.date.xmlText>
<cfset link = node.link.xmlText>
<cfset category = node.category.xmlText>
<cfset numanswers = node.numanswers.xmlText>
<cfset answer = node.chosenanswer.xmlText>
<cfoutput>
<p>
<b>Question:</b> <a href="#link#">#question#</a><br>
<b>Date:</b> #date#<br>
<b>Number of Answers:</b> #numanswers#<br>
</p>
<p>
<cfif len(answer)>
<b>Answer:</b> #answer#
<cfelse>
This question has NOT been answered yet. Please <a href="#link#">answer</a> it!
</cfif>
</p>
</cfoutput>
<hr />
</cfloop>
</cfif>
</cfif>
As a quick note - don't forget that I've changed the appid. You will need to get your own if you want to test.
The URL is pretty long, so let me focus on the variables and what they mean:
- appid: The application id.
- query: The search term.
- results: The total number of results. The API for Yahoo Answers has a max of 50 results.
- date_range: My value of 7 just means find results in the last 7 days.
There are more options for the API of course. See the documentation for more attributes.
Unlike my last example, I added a bit of error checking this time. I look at the XML packet and if Error is the root node, then I display the error result. You probably don't want to display to this user, but you would definitely want to email it to yourself.
After that it is simply a matter of getting the values out of the XML. These lines get a subset of what is returned:
<cfset questiontype = node.xmlAttributes.type>
<cfset subject = node.subject.xmlText>
<cfset question = node.content.xmlText>
<cfset date = node.date.xmlText>
<cfset link = node.link.xmlText>
<cfset category = node.category.xmlText>
<cfset numanswers = node.numanswers.xmlText>
<cfset answer = node.chosenanswer.xmlText>
Once I have the values I output them to the user. I also prompt them to answer the question if there isn't a chosen answer yet. By "chosenanswer" Yahoo means that the person who wrote the question hasn't picked one answer as the one that best answers their problem. A question can have multiple answers without one being chosen.
p.s. I'd love to share this with Yahoo's Developer Network. But as far as I can see, they have no way of contacting them. Surely someone must have a connection?