PHP code get Videos from YouTube Channel using Data API v3

youtube logo

PHP code get Videos from YouTube Channel using Data API v3. 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.

The YouTube Data API provides the easiest way to access YouTube data, such as channels, playlists, and videos from the web application. To get started with YouTube Data API, you need to create API key and use it in the API request. In this tutorial, we will provide a step-by-step guide to generate an API key on Google Developer Console and get API key for YouTube Data API v3.

Go to the Google Developer Console and login to your Google account.

In Google API Console, click the Select a project link.

google api console project
google api console project

A dialog box will appear, click the plus button (+) to create a project.

google api console create project
google api console create project

Enter the Project name and select other options » Click the Create button.

Create YouTube Data API Key
google api console project name details

It takes some seconds to setup your new project, please be patient. Once done, select your project.

google api console select project
google api console select project

Click the Library link on the left navigation menu. Under the YouTube APIs section and click the YouTube Data API link.

google developer console youtube data api
google developer console youtube data api

ENABLE the YouTube Data API v3 API to access the YouTube data.

google developer console enable youtube data api
google developer console enable youtube data api

Click the Credentials link on the left navigation menu. Click the Create credentials button and select API key.

google developer console create api key
google developer console create api key

A dialog box will appear with your newly created API Key. Use this API key in YouTube Data API v3 API request.

generate youtube data api v3 key
generate youtube data api v3 key

PHP code get Videos from YouTube Channel using 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('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$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
    if(isset($item->id->videoId)){
        echo '<div class="youtube-video">
                <iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
                <h2>'. $item->snippet->title .'</h2>
            </div>';
    }
}

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

Conclusion

Using our YouTube video list script, you can get all the videos from a channel without authentication (OAuth). Only an API Key need to be created on Google Developer Console and specified it in the script. That’s all! you can specify any YouTube channel from where the videos will be retrieved.

avatar
  Subscribe  
Notify of