Click here to Skip to main content
12,072,809 members (71,497 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5.7K views
1 bookmarked
Posted

Youtube video list with PHP

, 6 Jun 2015 MIT
Rate this:
Please Sign up or sign in to vote.
Load videos automatically from selected Youtube channel with PHP and Youtube Data API v3

Introduction

This article will present how to search and display Youtube videos by channel, keywords and other search terms with PHP and Youtube Data API v3.

Using the code

You can see related documentation and compose own request here: https://developers.google.com/youtube/v3/docs/search/list

The composed request return with json data, this includes the videos information.

Read the json(the part property may be only snippet at current example):

Related documentation: http://php.net/manual/en/function.json-decode.php

//You can see related documentation and compose own request here: https://developers.google.com/youtube/v3/docs/search/list
//You must enable Youtube Data API v3 and get API key on Google Developer Console(https://console.developers.google.com)

$channelId = 'UCVHFbqXqoYvEWM1Ddxl0QDg';
$maxResults = 8;
$API_key = '{YOUR_API_KEY}';


$video_list = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelId.'&maxResults='.$maxResults.'&key='.$API_key.''));

The success response body contains data with following structure(this is one example file):

https://raw.githubusercontent.com/magyarandras/YoutubeVideoList/master/list/example.json

Display videos and playlists:
foreach($video_list->items as $item)
{
        //Embed video
        if(isset($item->id->videoId)){
            
    echo '<li id="'. $item->id->videoId .'" class="col-lg-3 col-sm-6 col-xs-6 youtube-video">
        <a href="#'. $item->id->videoId .'" title="'. $item->snippet->title .'">
            <img src="'. $item->snippet->thumbnails->medium->url .'" alt="'. $item->snippet->title .'" class="img-responsive" height="130px" />
            <h2>'. $item->snippet->title .'</h2>
            <span class="glyphicon glyphicon-play-circle"></span>
        </a>
    </li>
    ';
    
        }
        //Embed playlist
        else if(isset($item->id->playlistId))
        {
            echo '<li id="'. $item->id->playlistId .'" class="col-lg-3 col-sm-6 col-xs-6 youtube-playlist">
        <a href="#'. $item->id->playlistId .'" title="'. $item->snippet->title .'">
            <img src="'. $item->snippet->thumbnails->medium->url .'" alt="'. $item->snippet->title .'" class="img-responsive" height="130px" />
            <h2>'. $item->snippet->title .'</h2>
            <span class="glyphicon glyphicon-play-circle"></span>
        </a>
    </li>
    ';
        }
}

I use Bootstrap UI framework(http://getbootstrap.com) for responsive display, and video list template(http://bootsnipp.com/snippets/featured/video-list-thumbnails).

Show Youtube players with jQuery:
//For video
$(".youtube-video").click(function(e){
    $(this).children('a').html('<div class="vid"><iframe width="420" height="315" src="https://www.youtube.com/embed/'+ $(this).attr('id') +'?autoplay=1" frameborder="0" allowfullscreen></iframe></div>');
    return false;
     e.preventDefault();
    });

    //For playlist
    $(".youtube-playlist").click(function(e){
    $(this).children('a').html('<div class="vid"><iframe width="420" height="315" src="https://www.youtube.com/embed/videoseries?list='+ $(this).attr('id') +'&autoplay=1" frameborder="0" allowfullscreen></iframe></div>');
    return false;
     e.preventDefault();
    });
The result:

Youtube video list

You can view the full code on GitHub: https://github.com/magyarandras/YoutubeVideoList

History

 

Many examples can be found for this purpose, but almost all use the old APIs, therefore i hope useful this solution with the Data API v3.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Magyar András
Student
Hungary Hungary
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionhow to grt the YouTube video list using username or userid using php Pin
Member 1222923128-Dec-15 3:30
memberMember 1222923128-Dec-15 3:30 
AnswerRe: how to grt the YouTube video list using username or userid using php Pin
Magyar András28-Dec-15 9:05
professionalMagyar András28-Dec-15 9:05 
GeneralRe: how to get the YouTube video list using username or userid using php Pin
Member 1222923128-Dec-15 19:48
memberMember 1222923128-Dec-15 19:48 
GeneralRe: how to get the YouTube video list using username or userid using php Pin
Magyar András28-Dec-15 23:56
professionalMagyar András28-Dec-15 23:56 
QuestionRe: how to get the YouTube video list using username or userid using php Pin
Member 122292314-Jan-16 1:36
memberMember 122292314-Jan-16 1:36 
AnswerRe: how to get the YouTube video list using username or userid using php Pin
Magyar András5-Jan-16 10:31
professionalMagyar András5-Jan-16 10:31 
GeneralRe: how to get the YouTube video list using username or userid using php Pin
Member 122292316-Jan-16 0:21
memberMember 122292316-Jan-16 0:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160208.1 | Last Updated 6 Jun 2015
Article Copyright 2015 by Magyar András
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid