雅虎问答 API + PHP 爬虫

发布于 2024-12-10 06:43:51 字数 6555 浏览 0 评论 0原文

我找到了 php理论上符合我的需求的脚本,但是我无法让它工作,我想知道脚本是否已经过时或者我是否做错了什么。

该脚本看起来像这样:

<?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->appID=$appid;
    }

    function set_numResults($num_results){
        $this->numResults = $num_results;
    }

    /**
     * Search for questions for the given keywords. Returned results can be associative array or XML
     * @param <string> $kewyord
     * @return <string> 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'     =>  '',
            'category_name' =>  '',
            'date_range'    =>  '', //7, 7-30, 30-60, 60-90, more90
            'sort'          =>  'relevance', //relevance, date_desc, date_asc
            'type'          =>  'all',
            'output'        =>  'php',
            'results'       =>  $this->numResults,
            'start'         =>  $this->numStart,
            'region'        =>  'us',
            'appid'         =>  $this->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->array2query_string($params);

        $url = $this->searchQuestionURL.$req_params;
        $results = $this->make_call($url);
        if($params['output']=='php'){
            $results = unserialize($results);
            return $results['Questions'];
        }
        return $results;

    }


    /**
     * Get all answers of a given question ID
     * @param <array> $params keys are: question_id, output, appid
     * @return <string> 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'   =>  '',
            'output'        =>  'php',
            'appid'         =>  $this->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->array2query_string($params);


        $url = $this->getQuestionURL.$req_params;
        $results = $this->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=>$val){
            $params .= "$key=$val&";
        }
        return $params;
    }
}

$appid = 'MYAPPID';
$params = array(
    'query'     =>      'test',   //enter your keyword here. this will be searched on yahoo answer
    'results'   =>       2,         //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
);

$question_id  = 'test'; //without this i get an error "Question ID is not set!"

$yn = new yahooAnswer($appid);
//search questions
try{
    $questions = $yn->search_questions($params);
} catch (Exception $e){
    echo ($e->getMessage());
}

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());
    }

}

?>

但我得到的不是有效的输出:

Array
(
    [id] => 
    [type] => 
    [Subject] => 
    [Content] => 
    [Date] => 
    [Timestamp] => 
    [Link] => http://answers.yahoo.com/question/?qid=
    [Category] => Array
        (
            [id] => 
            [content] => 
        )

    [UserId] => 
    [UserNick] => 
    [UserPhotoURL] => 
    [NumAnswers] => 
    [NumComments] => 
    [ChosenAnswer] => 
    [ChosenAnswererId] => 
    [ChosenAnswererNick] => 
    [ChosenAnswerTimestamp] => 
    [ChosenAnswerAwardTimestamp] => 
)

我已经用其他关键字尝试过,但结果总是相同的。

这部分 $question_id = 'test'; 未包含在官方脚本中,但如果没有它,我就会不断收到 Question ID is not set!

我还尝试更改它,将其添加到脚本中的另一个位置等。我能想到的一切,但结果始终是该数组,除了 [Link] 之外没有任何信息,

因为我几乎为零根本没有 php 经验,我什至不知道从哪里开始寻找错误:/如果 some1 能指出正确的方向,我会很高兴!

问候!

ps 当然“MYAPPID”已更改为我真正的雅虎应用程序 ID。

I have found a php script 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.

The script looks like this:

<?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->appID=$appid;
    }

    function set_numResults($num_results){
        $this->numResults = $num_results;
    }

    /**
     * Search for questions for the given keywords. Returned results can be associative array or XML
     * @param <string> $kewyord
     * @return <string> 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'     =>  '',
            'category_name' =>  '',
            'date_range'    =>  '', //7, 7-30, 30-60, 60-90, more90
            'sort'          =>  'relevance', //relevance, date_desc, date_asc
            'type'          =>  'all',
            'output'        =>  'php',
            'results'       =>  $this->numResults,
            'start'         =>  $this->numStart,
            'region'        =>  'us',
            'appid'         =>  $this->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->array2query_string($params);

        $url = $this->searchQuestionURL.$req_params;
        $results = $this->make_call($url);
        if($params['output']=='php'){
            $results = unserialize($results);
            return $results['Questions'];
        }
        return $results;

    }


    /**
     * Get all answers of a given question ID
     * @param <array> $params keys are: question_id, output, appid
     * @return <string> 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'   =>  '',
            'output'        =>  'php',
            'appid'         =>  $this->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->array2query_string($params);


        $url = $this->getQuestionURL.$req_params;
        $results = $this->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=>$val){
            $params .= "$key=$val&";
        }
        return $params;
    }
}

$appid = 'MYAPPID';
$params = array(
    'query'     =>      'test',   //enter your keyword here. this will be searched on yahoo answer
    'results'   =>       2,         //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
);

$question_id  = 'test'; //without this i get an error "Question ID is not set!"

$yn = new yahooAnswer($appid);
//search questions
try{
    $questions = $yn->search_questions($params);
} catch (Exception $e){
    echo ($e->getMessage());
}

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());
    }

}

?>

But instead of a valid output I just get this:

Array
(
    [id] => 
    [type] => 
    [Subject] => 
    [Content] => 
    [Date] => 
    [Timestamp] => 
    [Link] => http://answers.yahoo.com/question/?qid=
    [Category] => Array
        (
            [id] => 
            [content] => 
        )

    [UserId] => 
    [UserNick] => 
    [UserPhotoURL] => 
    [NumAnswers] => 
    [NumComments] => 
    [ChosenAnswer] => 
    [ChosenAnswererId] => 
    [ChosenAnswererNick] => 
    [ChosenAnswerTimestamp] => 
    [ChosenAnswerAwardTimestamp] => 
)

I have tried it with other keywords, but the result is always the same.

This part $question_id = 'test'; is not included in the official script, but without it I keep getting Question ID is not set!.

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 [Link]

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!

Regards!

p.s. of course "MYAPPID" is changed to my real yahoo app id.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-12-17 06:43:51

为了使该示例正常工作,请将以下行:

$answers = $yn->get_question(array('question_id'=>$question_id));

更改为:

$answers = $yn->get_question(array('question_id'=>$question['id']));

该更改从响应中提取实际问题 ID search_questions(),并在对 get_question() 的调用中使用它。

In order to make this example work, change this line:

$answers = $yn->get_question(array('question_id'=>$question_id));

to:

$answers = $yn->get_question(array('question_id'=>$question['id']));

That change pulls the actual question ID out of the response from search_questions(), and uses it in the call to get_question().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文