Youtube API PHP json decode


Youtube API PHP json decode

我正在尝试搜索youtube并获取视频ID和标题。

在下面的例子中,我正在搜索"youtube":

<?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);
     '</pre>';
    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
                                )
                        )
                )
        )
)

我想把[身份证]和[头衔]呼应一下。知道我做错了什么吗?

谢谢!

将第9行更改为:

foreach ( $json_output->data->items as $data ){

您应该将第一个items作为Objects Array:

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

所以现在就试试foreach

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

我希望现在一切都清楚了。