Overview
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.
Code:-
// 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.'; }
Youtube Video List using PHP
Loop through the $videoList->items to list the videos from a YouTube channel on the web page using PHP.
Code :-
if(!empty($videoList->items)){ foreach($videoList->items as $item){ // Embed video if(isset($item->id->videoId)){ echo ' ; } } }else{ echo '
'.$apiError.; }
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
In case of Youtube API Support: Get in touch
0 Comments