Stackoverflow API получает ответы в массиве PHP - PullRequest
0 голосов
/ 07 февраля 2012

Я пытаюсь использовать API-интерфейс stackoverflow и хочу получить ответы на вопрос в массиве 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);


?>

Я хочу сохранить ответы на вопрос в массиве, называемом ответы, чтобы я мог получить к ним доступ с помощью цикла 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
                )
....

1 Ответ

0 голосов
/ 07 февраля 2012

Не знаю точно, какое именно свойство вы хотите извлечь, но я предполагаю, что это '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']);
}

Это нужно сделать.

...