Searching and retrieving yahoo answers in PHP

I am working on an WordPress autobloggin plugin where I had to search yahoo answers site for any keyword and post the answers in WordPress. After checking their site, I see it is very easy now as they provide API for the questions and answers. I do not know when they introduced API for Answers but I do remember during my last check (quite long time ago), they did not have any API and scrapping was the only way.

Their API for searching and getting answers is very simple and easiest! However, for the better handling of my project, I have mad a class to find questions by any given keyword and then retrieve the answers of any specific question by question ID.

How to use?

First of all you need to download the class file. Click here to download it. Then unzip the file.

Then include the class in your script like (assuming your script and this class file exists in same directory):

require('yahooanswer.class.php');
$params = array(
    'query'     =>      'keyword',   //enter your keyword here. this will be searched on yahoo answer
    'results'   =>      10,         //number of questions it should return
    'type'      =>      'resolved',  //only resolved questiosn will be returned. other values can be all, open, undecided
    'output'    =>      'php',      //result will be PHP array. Other values can be xml, json, rss
);
$query  = 'yoga'; //enter your keyword here to search for
$yn = new yahooAnswer($appid);
//search questions
try{
    $questions = $yn->search_questions($params);
} catch (Exception $e){
    echo ($e->getMessage());
}

There are more options to be set in $params. Please check this URL to know all options. You just need to set any option as key=>value pair in the $params array. It will be conveyed correctly for you :).

So, if everything correct, we will get results like the following:

[0] => Array
        (
            [id] => 20080610153832AAStQwZ
            [type] => Answered
            [subject] => Yoga??????
            [content] => I usually do yoga and relaxation things and stuff like that. and i have a couple questions

1. How long do you think it would take me to lose about 5 pounds while doing yoga?

2. What are the best yoga tips and exersises?

3. What are some good ways to relax?

4. Any advise about yoga?

Those are my questions..hope you can answer them :) thanks!

            [date] => 2008-06-10 15:38:32
            [timestamp] => 1213137512
            [link] => http://answers.yahoo.com/question/?qid=20080610153832AAStQwZ
            [category] => Diet & Fitness
            [userid] => yqaYuvZxaa
            [usernick] => Sjharr S
            [userphotourl] =>
            [numanswers] => 5
            [numcomments] => 2
            [chosenanswer] => I would add walking or aerobic exercise plus meditation and yoga.
            [chosenanswerid] => soZI0hNlaa
            [chosenanswernick] => carpal-tunnel-provider
            [chosenanswertimestamp] => 1213411114
            [chosenanswerawardtimestamp] => 1213681330
        )

Let’s say we need to fetch the answers of all questions. Simply call the get_question() method inside a loop.

foreach ($questions as $question) {
    //now get the answers for the question_id;
    try{
        $answers = $yn->get_question(array('question_id'=>$question_id));
        echo '<pre>';
        print_r($answers);
        echo '<pre>';
    } catch (Exception $e){
        echo($e->getMessage());
    }
}

So, you got answers of all the questions. That’s it. To know the detail option for getting answers you can check this page.  There are other APIs to use with Yahoo Answers however, in my class I implemented only the methods I needed for my project. If you needed any other methods, you can extend this class and implement those. Please ping this blog if you do same so that we all know about your work.