【问题标题】:Stackoverflow API getting answers in array PHPStackoverflow API 在数组 PHP 中获取答案
【发布时间】:2012-02-07 11:20:13
【问题描述】:

我正在尝试使用 stackoverflow API,并且我想在 php 数组中获得问题的答案。到目前为止,这是我的 php 代码:

<?php


//KEY
$string = "key=my_key";

//Call stack API .$string
$stack_url = "compress.zlib://http://api.stackoverflow.com/1.1/questions";

//Get and Store API results into a variable
$result = file_get_contents($stack_url);
$jsonArray = json_decode($result);
print_r($jsonArray);
//echo($jsonArray->questions[0]->question_answers_url);
//var_dump($jsonArray);


?>

我想将问题的答案存储在一个名为 answers 的数组中,以便我可以使用 for 循环访问它们。

我得到的答案是:

stdClass Object
(
    [total] => 2618591
    [page] => 1
    [pagesize] => 30
    [questions] => Array
        (
            [0] => stdClass Object
                (
                    [tags] => Array
                        (
                            [0] => c#
                            [1] => ssh
                            [2] => openssh
                            [3] => rsacryptoserviceprovider
                        )

                    [answer_count] => 1
                    [favorite_count] => 0
                    [question_timeline_url] => /questions/9164203/timeline
                    [question_comments_url] => /questions/9164203/comments
                    [question_answers_url] => /questions/9164203/answers
                    [question_id] => 9164203
                    [owner] => stdClass Object
                        (
                            [user_id] => 311966
                            [user_type] => registered
                            [display_name] => simonc
                            [reputation] => 301
                            [email_hash] => 021f3344004f0c886d715314fa02037d
                        )

                    [creation_date] => 1328548627
                    [last_edit_date] => 1328611688
                    [last_activity_date] => 1328611688
                    [up_vote_count] => 0
                    [down_vote_count] => 0
                    [view_count] => 25
                    [score] => 0
                    [community_owned] => 
                    [title] => Format of PKCS private keys
                )

            [1] => stdClass Object
                (
                    [tags] => Array
                        (
                            [0] => c#
                            [1] => .net
                            [2] => combobox
                        )

                    [answer_count] => 3
                    [favorite_count] => 0
                    [question_timeline_url] => /questions/9174765/timeline
                    [question_comments_url] => /questions/9174765/comments
                    [question_answers_url] => /questions/9174765/answers
                    [question_id] => 9174765
                    [owner] => stdClass Object
                        (
                            [user_id] => 1194399
                            [user_type] => registered
                            [display_name] => Goxy
                            [reputation] => 1
                            [email_hash] => 5fc8c96b6b85c6339cb9ac4ab60cb247
                        )

                    [creation_date] => 1328611202
                    [last_activity_date] => 1328611686
                    [up_vote_count] => 0
                    [down_vote_count] => 0
                    [view_count] => 15
                    [score] => 0
                    [community_owned] => 
                    [title] => WPF: Bind simple List<myClass> to Combobox
                )
....

【问题讨论】:

  • json_decode 有一个可选参数,因此使用$jsonArray = json_decode($result,true); 会将其作为关联数组返回

标签: php arrays json api


【解决方案1】:

不确定您要提取的确切属性,但我认为它是“question_answers_url”。

$answersArray = Array();
for($i=0;$i<count($jsonArray['questions']);$i++){
    //assuming it is the 'question_answers_url' property that you want
    array_push($answersArray,$jsonArray['questions'][$i]['question_answers_url']);
}

应该这样做。

【讨论】:

  • 是的,我的问题是这个命令给了我答案的 url,而不是它自己在数组中的答案。我想在数组中以纯文本格式获得答案。例如 $answersArray=[answer1_in_plain_text,answer2_in_plain_text,....]
  • 检查 API 的这一位是否需要:link
  • 好的,我检查了api命令但不知道如何使用id值
猜你喜欢
  • 2013-06-09
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 2010-09-20
相关资源
最近更新 更多