0

I'm trying to search youtube and grab the videos video ID and title.

In the bellow example I am searching for "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}";
    }
    ?>

Here is the json output for the above query:

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
                                )

                        )

                )

        )

)

Im trying to echo out [id] and [title]. Any idea what I am doing wrong?

Thanks!

2 Answers 2

7

Change line 9 to:

foreach ( $json_output->data->items as $data ){
Sign up to request clarification or add additional context in comments.

Comments

4

You should get first items as an Objects Array:

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

So try foreach now:

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

I hope it's clear now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.