API YouTube PHP JSON декодировать - PullRequest
0 голосов
/ 05 марта 2012

Я пытаюсь найти на YouTube и получить идентификатор видео и название видео.

В приведенном ниже примере я ищу "youtube":

<code><?php
    $url="http://gdata.youtube.com/feeds/api/videos?q='youtube'&format=5&max-results=2&v=2&alt=jsonc";
    $json = file_get_contents($url,0,null,null);
    $json_output = json_decode($json);
     echo '<pre>';
      print_r("query results:");
      print_r($json_output);
     '
';foreach ($ json_output-> data as $ data) {echo "{$ data-> id}";echo "{$ data-> title}";}?>

Вот вывод json для вышеуказанного запроса:

query results:stdClass Object
(
    [apiVersion] => 2.1
    [data] => stdClass Object
        (
            [updated] => 2012-03-04T20:19:06.314Z
            [totalItems] => 1000000
            [startIndex] => 1
            [itemsPerPage] => 2
            [items] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => IpSYwvzKukI
                            [uploaded] => 2009-03-14T08:17:36.000Z
                            [updated] => 2012-02-29T10:51:35.000Z
                            [uploader] => kenbedict009
                            [category] => Music
                            [title] => one of the funniest kid in youtube!!
                            [description] => he is a 3 years old korean,this kid makes me laugh
                            [tags] => Array
                                (
                                    [0] => rin on the rox
                                    [1] => charice pempengco
                                    [2] => joeydiamond
                                )

                            [thumbnail] => stdClass Object
                                (
                                    [sqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/default.jpg
                                    [hqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/hqdefault.jpg
                                )

                            [player] => stdClass Object
                                (
                                    [default] => http://www.youtube.com/watch?v=IpSYwvzKukI&feature=youtube_gdata_player
                                    [mobile] => http://m.youtube.com/details?v=IpSYwvzKukI
                                )

                            [content] => stdClass Object
                                (
                                    [5] => http://www.youtube.com/v/IpSYwvzKukI?version=3&f=videos&app=youtube_gdata
                                    [1] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
                                    [6] => rtsp://v8.cache2.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
                                )

                            [duration] => 344
                            [rating] => 4.6304307
                            [likeCount] => 23419
                            [ratingCount] => 25803
                            [viewCount] => 9848083
                            [favoriteCount] => 15390
                            [commentCount] => 16074
                            [accessControl] => stdClass Object
                                (
                                    [comment] => allowed
                                    [commentVote] => allowed
                                    [videoRespond] => moderated
                                    [rate] => allowed
                                    [embed] => allowed
                                    [list] => allowed
                                    [autoPlay] => allowed
                                    [syndicate] => allowed
                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => PPNMGYOm1aM
                            [uploaded] => 2011-06-23T16:35:33.000Z
                            [updated] => 2012-02-29T13:15:47.000Z
                            [uploader] => shakeitupvevo
                            [category] => Music
                            [title] => "Watch Me" from Disney Channel's "Shake It Up"
                            [description] => To download Watch Me visit www.smarturl.it Performed by Bella Thorne and Zendaya
                            [tags] => Array
                                (
                                    [0] => Bella
                                    [1] => Thorne
                                    [2] => Zendaya
                                    [3] => Cast
                                    [4] => of
                                    [5] => Shake
                                    [6] => It
                                    [7] => Up:
                                    [8] => Break
                                    [9] => Down
                                    [10] => Watch
                                    [11] => Me
                                    [12] => Walt
                                    [13] => Disney
                                    [14] => Soundtrack
                                )

                            [thumbnail] => stdClass Object
                                (
                                    [sqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/default.jpg
                                    [hqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/hqdefault.jpg
                                )

                            [player] => stdClass Object
                                (
                                    [default] => http://www.youtube.com/watch?v=PPNMGYOm1aM&feature=youtube_gdata_player
                                )

                            [content] => stdClass Object
                                (
                                    [5] => http://www.youtube.com/v/PPNMGYOm1aM?version=3&f=videos&app=youtube_gdata
                                )

                            [duration] => 193
                            [aspectRatio] => widescreen
                            [rating] => 4.7312055
                            [likeCount] => 145059
                            [ratingCount] => 155509
                            [viewCount] => 48201555
                            [favoriteCount] => 69981
                            [commentCount] => 81938
                            [status] => stdClass Object
                                (
                                    [value] => restricted
                                    [reason] => limitedSyndication
                                )

                            [restrictions] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [type] => country
                                            [relationship] => deny
                                            [countries] => DE
                                        )

                                )

                            [accessControl] => stdClass Object
                                (
                                    [comment] => allowed
                                    [commentVote] => allowed
                                    [videoRespond] => allowed
                                    [rate] => allowed
                                    [embed] => allowed
                                    [list] => allowed
                                    [autoPlay] => allowed
                                    [syndicate] => allowed
                                )

                        )

                )

        )

)

Я пытаюсь повторить [id] и [title].Есть идеи, что я делаю не так?

Спасибо!

Ответы [ 2 ]

7 голосов
/ 05 марта 2012

Измените строку 9 на:

foreach ( $json_output->data->items as $data ){
4 голосов
/ 05 марта 2012

Вы должны получить сначала items как Objects Array:

$items = $json -> data -> items;

Так что попробуйте foreach сейчас:

foreach ( $items as $item ){
    echo "{$item->id}";
    echo "{$item->title}";
}

Надеюсь, теперь все ясно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...