Get Videos from YouTube Channel using Data API v3 and PHP

The YouTube Data API provides an easy way to access YouTube channel data and incorporate into your web application. The various resources can be retrieved from YouTube channel using Data API. If you want to implement YouTube videos gallery on your website, it could be done using YouTube Data API and PHP.

If you have a requirement to retrieve videos from YouTube channel and listing on the website, our example script will help you a lot. This tutorial will show you the simple way to get videos from YouTube channel and display them in the web page using YouTube Data API v3 with PHP. We will use YouTube Data API v3 to retrieve videos from YouTube channel and list them in the website using PHP.

YouTube Data API Key

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

Get Youtube Videos using YouTube Data API v3

YouTube Data API request returns the JSON data that includes the information of the video (title, description, thumbnails, publish date, etc.). You need to specify the API key ($API_key), YouTube Channel ID ($channelID).

//Get videos from channel by YouTube Data API
$API_key    'Insert_Your_API_Key';
$channelID  'Insert_Channel_ID';
$maxResults 10;

$videoList json_decode(file_get_contents(''.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

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.

foreach($videoList->items as $item){
//Embed video
'<div class="youtube-video">
                <iframe width="280" height="150" src="'
.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
$item->snippet->title .'</h2>

In the YouTube video list, Video ID and Title are used, but you can show the other information as per your requirement. The following information is provided by the API.

  • 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

Related Articles

Comments 0