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.

  • Ralph Thompson

    great post thanks

  • Chris

    I would really appreciate if you could post the code (or send it to me privately if desired) which combines this with automatically posting the data into the wordpress database.

    I am specifically looking to use an rss feed for a site as the “target”, then scrape the data of the target url based on xpath and then take the extracted data and post it into my wordpress blog.

    I am also looking for a way to post data for specific custom fields if you happen to know how to do that and set the post status to “draft”.

  • The HungryCoder

    Hello,
    I understand your requirements but I am sorry, this is out of my blogging scope as your requirements are very custom. I blog on generic issues. I believe it is better that you go to any freelancing site and hire an WordPress specialist who can help you.

    Thanks for your inputs!

  • http://www.maistro.com juust

    nice class, I wanted to build a simple:press forum filler and this is exactly what i was looking for. I ran into one issue with $question_id in stead of $question[‘id’], the rest of it works fine. Thnx.

  • Chris

    Thanks for your reply! I am attempting to build this myself so if possible could you at least briefly explain how (and with what code) you can get wordpress to automatically create posts based off search results? I am assuming you have some script running on a page then and call the file through a cron job… I am just confused on how this should be done so any info will be very helpful to me and surely others because no one has discussed this really.

  • Pingback: Yahoo Answers API + php Scraper | Gravity Layouts()