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
).Note that: The YouTube Data API Key will get from Google Developer Console which you created earlier.
//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.''));
Youtube Video List using PHP
Loop through the$videoList->items
to list the videos from a YouTube channel.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.
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>';
}
}
- 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
Comments