Get your Youtube channel playlist

Html+Css
Apr 04 2015
1641 Views

Get your Youtube channel playlist

Download at github

Introduction:

I made a quick little example to read a YouTube user’s video feed that really easy to use.  To request a feed of all videos uploaded by another user, send a GET request to the following URL. This request does not require authentication. https://gdata.youtube.com/feeds/api/users/userId/uploads. In the URL above, you should replace the text userId with the user's YouTube user ID.

Step1:

Use below lines of code to get all youtube channel videos as xml format.

$feedURL = 'https://gdata.youtube.com/feeds/api/users/yahoo/uploads';

$sxml = simplexml_load_file($feedURL);

Step2:

Now parse the XML feed and get the video url as shown

foreach ($sxml->entry as $entry) {

                    $media = $entry->children('http://search.yahoo.com/mrss/');

                    // get video player URL

                    $attrs = $media->group->player->attributes();

                    $watch = $attrs['url'];                   

}

Step3:

To  get the video thumbnail use bellow code

foreach ($sxml->entry as $entry) {

                    $media = $entry->children('http://search.yahoo.com/mrss/');

                    // get video thumbnail

                    $attrs = $media->group->thumbnail[0]->attributes();

                    $thumbnail = $attrs['url'];

}

Step4:

To get the video streaming time use bellow code

foreach ($sxml->entry as $entry) {

                    $media = $entry->children('http://search.yahoo.com/mrss/');

                           // get video length

                    $yt = $media->children('http://gdata.youtube.com/schemas/2007');

                    $attrs = $yt->duration->attributes();

                    $length = $attrs['seconds'];      

}

Step5:

To get the video visit statistics use bellow code

foreach ($sxml->entry as $entry) {

                    $media = $entry->children('http://search.yahoo.com/mrss/');

                        // get viewer statistics  

$yt = $entry->children('http://gdata.youtube.com/schemas/2007');

                    $attrs = $yt->statistics->attributes();

                    $viewCount = $attrs['viewCount'];

}

Step6:

To get the video rating statistics use bellow code

foreach ($sxml->entry as $entry) {

                    $media = $entry->children('http://search.yahoo.com/mrss/');

                        // get video ratings

                    $gd = $entry->children('http://schemas.google.com/g/2005');

                    if ($gd->rating) {

                        $attrs = $gd->rating->attributes();

                        $rating = $attrs['average'];

                    } else {

                        $rating = 0;

                    }

}

Step7:

Coming to the UI part, add simple html in between foreach loop to display feed in your website.

<div class="col-lg-4 col-sm-6 col-xs-12 mygrid">

                        <a href="<?php echo $watch; ?>" target="_blank">

                            <img src="<?php echo $thumbnail; ?>" class="thumbnail img-responsive" alt="<?php echo $media->group->title; ?>">

                        </a>

                        <a href="<?php echo $watch; ?>" target="_blank"><?php echo $media->group->title; ?></a><br/>

                        <span>Views: <?php echo $viewCount; ?></span>

                        <span class="attr" <?php printf('%0.2f', $length / 60); ?> min.</span>

                    </div>

I used bootstrap framework to show your feed beautiful and responsive way. Now combine all the above steps to use show feed in your website. Click on demo link , download source code and use it.

Like my official facebook at https://www.facebook.com/7codesinfo

Leave a Reply