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.
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.
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.
file_get_contents()
function is used to load the YouTube Data API response data using PHP.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.
Loop through the $videoList->items
to list the videos from a YouTube channel on the web page using PHP.
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.
$item->id->videoId
$item->snippet->publishedAt
$item->snippet->channelId
$item->snippet->title
$item->snippet->description
$item->snippet->thumbnails->default->url
$item->snippet->thumbnails->medium->url
$item->snippet->thumbnails->high->url
$item->snippet->channelTitle
Download YouTube Video using PHP
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
i need more than 50 video and i also want to work it with pagination.. please help me.
how to access video monetization details through youtube data v3 API key.
Hello in this tutorial only get 10 videos i want all videos from channel so how to implement it can you give me solution please
In the maxResults parameter, you can specify the maximum number of items that should be returned in the result set. It accepts the value from 0 to 50. So, you can get a maximum of 50 videos on each request.
Thank you so much sir, is is a great tutorial, its done my work trouble from few hours. thanks alot
Great tutorial. Was wondering how to show view counts per video. Any suggestions?
Thank you so much!
Thanks so much for this great way of doing it. Super simple to implement and do myself. No need to even look further.