Get Videos from YouTube Channel using Data API v3 and PHP

List YouTube videos on the web application helps the user to find relevant video content easily. The user can access YouTube videos directly from your website. If you want to list videos from your YouTube channel or create a YouTube video gallery on the web application, it can be easily implemented with YouTube Data API and PHP.

YouTube Data API provides an easy way to access YouTube channel data and incorporate it into the web application. You can fetch various information from YouTube channels using Data API. This tutorial will show you how to retrieve videos from the YouTube channel and list them on the website using PHP.

In this example code, we will use YouTube Data API v3 to retrieve videos from the YouTube channel. You can get videos from YouTube channel and display them on the web page using YouTube Data API v3 and PHP.

YouTube Data API Key

In order to use YouTube Data API, you must enable YouTube Data API v3 and create an API key on Google Developer Console. The API key needs to be provided in the YouTube Data API request. To create a YouTube Data API key, see the following step-by-step guide.

Get Youtube Videos using YouTube Data API v3

YouTube Data API request returns the data in JSON format that contains the information about the videos (title, description, thumbnails, publish date, etc.) of the specified YouTube channel.

  • API_key – Google API Key (YouTube Data API must be enabled).
  • Channel_ID – YouTube Channel ID from where the videos will be fetched.
  • Max_Results – Number of videos to be fetched.
  • file_get_contents – The file_get_contents() function is used to load the YouTube Data API response data using PHP.
  • json_decode – The json_decode() function converts API JSON response to array.
// API config 
$API_Key    'Your_YouTube_Data_API_Key';
$Channel_ID 'YouTube_Channel_ID';
$Max_Results 10;

// Get videos from channel by YouTube Data API
$apiData = @file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$Channel_ID.'&maxResults='.$Max_Results.'&key='.$API_Key.'');
if(
$apiData){
    
$videoList json_decode($apiData);
}else{
    echo 
'Invalid API key or channel ID.';
}

Note that: The YouTube Data API Key will get from Google Developer Console which you created earlier.

Youtube Video List using PHP

Loop through the $videoList->items to list the videos from a YouTube channel on the web page using PHP.

  • Use HTML <iframe> to embed YouTube video on the web page.
if(!empty($videoList->items)){ 
    foreach(
$videoList->items as $item){
        
// Embed video
        
if(isset($item->id->videoId)){
            echo 
'
            <div class="yvideo-box">
                <iframe width="280" height="150" src="https://www.youtube.com/embed/'
.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
                <h4>'
$item->snippet->title .'</h4>
            </div>'
;
        }
    }
}else{
    echo 
'<p class="error">'.$apiError.'</p>';
}

In the YouTube video list, ID and Title are used from the video information. But, you can use various information of video as per your requirement. The YouTube Data API provides the following information.

  • YouTube Video ID – $item->id->videoId
  • YouTube Video Publish Date – $item->snippet->publishedAt
  • YouTube Channel ID – $item->snippet->channelId
  • YouTube Video Title – $item->snippet->title
  • YouTube Video Description – $item->snippet->description
  • YouTube Video Thumbnail URL (default size) – $item->snippet->thumbnails->default->url
  • YouTube Video Thumbnail URL (medium size) – $item->snippet->thumbnails->medium->url
  • YouTube Video Thumbnail URL (large size) – $item->snippet->thumbnails->high->url
  • YouTube Channel Title – $item->snippet->channelTitle

Download YouTube Video using PHP

Conclusion

Using our YouTube video list script, you can get all the videos from a channel without authentication (OAuth) and display them on the web page using PHP. Only an API Key is required to be specified in the script (it can be created from Google Developer Console). Also, there are various options (order, limit, etc.) are available to customize the YouTube data result set as per your needs.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

7 Comments

  1. Ashu Said...
  2. Vinay Babu Said...
  3. Jaydev Said...
    • CodexWorld Said...
  4. Mukesh Jakhar Said...
  5. Hibana Said...
  6. Anieka Said...

Leave a reply

keyboard_double_arrow_up