title: Yahoo Answers API + php Scraper
body: <p>I have found a <a href="http://hungrycoder.xenexbd.com/scripts/php-scripts-2/searching-and-retrieving-yahoo-answers-in-php.html" rel="nofollow">php script</a> that would in theorie match my needs, however I cant get it to work and I was wondering if maybe the script is outdated or if I am doing something wrong.</p>
  
  <p>The script looks like this:</p>
  
  <pre><code>&lt;?php
  /**
   * @package Yahoo Answer
   * @author The HungryCoder
   * @link http://hungrycoder.xenexbd.com/?p=953
   * @version 1.0
   * @license GPL, This class does not come with any expressed or implied warranties! Use at your own risks!  
   */
  
  class yahooAnswer{
      var $appID;
      var $searchQuestionURL = 'http://answers.yahooapis.com/AnswersService/V1/questionSearch?';
      var $getQuestionURL = 'http://answers.yahooapis.com/AnswersService/V1/getQuestion?';
  
      private $numResults = 10;
      private $numStart = 0;
  
      function  __construct($appid) {
  
          $this-&gt;appID=$appid;
      }
  
      function set_numResults($num_results){
          $this-&gt;numResults = $num_results;
      }
  
      /**
       * Search for questions for the given keywords. Returned results can be associative array or XML
       * @param &lt;string&gt; $kewyord
       * @return &lt;string&gt; Returns the results set either in XML format or associative array. 
       */
  
      function search_questions($params){
          if(!is_array($params)){
              throw new Exception('The parameters must be an array!');
          }
          $defaults = array(
              'search_in'     =&gt;  '',
              'category_name' =&gt;  '',
              'date_range'    =&gt;  '', //7, 7-30, 30-60, 60-90, more90
              'sort'          =&gt;  'relevance', //relevance, date_desc, date_asc
              'type'          =&gt;  'all',
              'output'        =&gt;  'php',
              'results'       =&gt;  $this-&gt;numResults,
              'start'         =&gt;  $this-&gt;numStart,
              'region'        =&gt;  'us',
              'appid'         =&gt;  $this-&gt;appID,
          );
          $params = array_merge($defaults,$params);
  
          if(!$params['appid']){
              throw new Exception('APP ID is empty!', 404);
          }
          if(!$params['query']) {
              throw new Exception('Query is not set!', '404');
          }
  
          $req_params = $this-&gt;array2query_string($params);
  
          $url = $this-&gt;searchQuestionURL.$req_params;
          $results = $this-&gt;make_call($url);
          if($params['output']=='php'){
              $results = unserialize($results);
              return $results['Questions'];
          }
          return $results;
  
      }
  
  
      /**
       * Get all answers of a given question ID
       * @param &lt;array&gt; $params keys are: question_id, output, appid
       * @return &lt;string&gt; Returns all answers in expected format. default format is php array
       */
  
      function get_question($params){
  
           if(!is_array($params)){
              throw new Exception('The parameter must be an array!');
          }
          $defaults = array(
              'question_id'   =&gt;  '',
              'output'        =&gt;  'php',
              'appid'         =&gt;  $this-&gt;appID,
          );
          $params = array_merge($defaults,$params);
  
  
  
          if(!$params['appid']){
              throw new Exception('APP ID is empty!', 404);
          }
          if(!$params['question_id']) {
              throw new Exception('Question ID is not set!', '404');
          }
  
  
          $req_params = $this-&gt;array2query_string($params);
  
  
          $url = $this-&gt;getQuestionURL.$req_params;
          $results = $this-&gt;make_call($url);
          if($params['output']=='php'){
              $results = unserialize($results);
              return $results['Questions'][0];
          }
          return $results;
      }
  
  
      protected function make_call($url){
          if(function_exists('curl_init')){
              $ch = curl_init($url);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
              curl_setopt($ch, CURLOPT_TIMEOUT,60);
  
              $result = curl_exec($ch);
              curl_close($ch);
              return $result;
          } else if(function_exists('file_get_contents')) {
              return file_get_contents($url);
          } else {
              throw new Exception('No method available to contact remote server! We must need cURL or file_get_contents()!', '500');
          }
      }
  
      protected  function array2query_string($array){
          if(!is_array($array)) throw new Exception('Parameter must be an array', '500');
          $params ='';
          foreach($array as $key=&gt;$val){
              $params .= "$key=$val&amp;";
          }
          return $params;
      }
  }
  
  $appid = 'MYAPPID';
  $params = array(
      'query'     =&gt;      'test',   //enter your keyword here. this will be searched on yahoo answer
      'results'   =&gt;       2,         //number of questions it should return
      'type'      =&gt;      'resolved',  //only resolved questiosn will be returned. other values can be all, open, undecided
      'output'    =&gt;      'php',      //result will be PHP array. Other values can be xml, json, rss
  );
  
  $question_id  = 'test'; //without this i get an error "Question ID is not set!"
  
  $yn = new yahooAnswer($appid);
  //search questions
  try{
      $questions = $yn-&gt;search_questions($params);
  } catch (Exception $e){
      echo ($e-&gt;getMessage());
  }
  
  foreach ($questions as $question) {
      //now get the answers for the question_id;
      try{
          $answers = $yn-&gt;get_question(array('question_id'=&gt;$question_id));
          echo '&lt;pre&gt;';
          print_r($answers);
          echo '&lt;pre&gt;';
      } catch (Exception $e){
          echo($e-&gt;getMessage());
      }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>But instead of a valid output I just get this:</p>
  
  <pre><code>Array
  (
      [id] =&gt; 
      [type] =&gt; 
      [Subject] =&gt; 
      [Content] =&gt; 
      [Date] =&gt; 
      [Timestamp] =&gt; 
      [Link] =&gt; http://answers.yahoo.com/question/?qid=
      [Category] =&gt; Array
          (
              [id] =&gt; 
              [content] =&gt; 
          )
  
      [UserId] =&gt; 
      [UserNick] =&gt; 
      [UserPhotoURL] =&gt; 
      [NumAnswers] =&gt; 
      [NumComments] =&gt; 
      [ChosenAnswer] =&gt; 
      [ChosenAnswererId] =&gt; 
      [ChosenAnswererNick] =&gt; 
      [ChosenAnswerTimestamp] =&gt; 
      [ChosenAnswerAwardTimestamp] =&gt; 
  )
  </code></pre>
  
  <p>I have tried it with other keywords, but the result is always the same. </p>
  
  <p>This part <code>$question_id  = 'test';</code> is not included in the official script, but without it I keep getting <code>Question ID is not set!</code>.</p>
  
  <p>I also tried to change it, add it at another place in the script etc. Everything I could think of, but the result was always that array with no information besides the <code>[Link]</code></p>
  
  <p>Since I got almost zero php experience at all, I am not even where start looking for an error :/ Would be glad if some1 could point me in the right direction!</p>
  
  <p>Regards!</p>
  
  <p>p.s. of course "MYAPPID" is changed to my real yahoo app id.</p>