Yahoo Answers API Example

This post is more than 2 years old.

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:

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="" result="result" charset="utf-8" />

<cfif len(result.fileContent) and isXml(result.fileContent)>

&lt;cfset xmlResult = xmlParse(result.fileContent)&gt;

&lt;cfif structKeyExists(xmlResult, "Error")&gt;

	An error occured while performing your query. The error was:&lt;br /&gt;

	&lt;cfset numResults = arrayLen(xmlResult.ResultSet.Question)&gt;
	Your search for #searchTerm# questions returned #numResults# results.

	&lt;cfloop index="x" from="1" to="#numResults#"&gt;

		&lt;cfset node = xmlResult.resultSet.Question[x]&gt;
		&lt;cfset questiontype = node.xmlAttributes.type&gt;
		&lt;cfset subject = node.subject.xmlText&gt;
		&lt;cfset question = node.content.xmlText&gt;
		&lt;cfset date =;
		&lt;cfset link =;
		&lt;cfset category = node.category.xmlText&gt;
		&lt;cfset numanswers = node.numanswers.xmlText&gt;
		&lt;cfset answer = node.chosenanswer.xmlText&gt;
		&lt;b&gt;Question:&lt;/b&gt; &lt;a href="#link#"&gt;#question#&lt;/a&gt;&lt;br&gt;
		&lt;b&gt;Date:&lt;/b&gt; #date#&lt;br&gt;
		&lt;b&gt;Number of Answers:&lt;/b&gt; #numanswers#&lt;br&gt;
		&lt;cfif len(answer)&gt;
		&lt;b&gt;Answer:&lt;/b&gt; #answer#
		This question has NOT been answered yet. Please &lt;a href="#link#"&gt;answer&lt;/a&gt; it!
		&lt;hr /&gt;



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 => <cfset link => <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?

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA