Menu

Get playlists and playlist videos belong to a YouTube channel using youtube API with php

I have been playing around YouTube API for a while, and was bit difficult for a while to get my head around it. But it all starting to make sense in my tiny brain. I was able to get few example working with successfully. Below is an example of how to get all the playlist from a channel.

Download the Google PHP client library from GitHub. and include it in your script.

require_once '../../../Google/autoload.php';
require_once '../../../Google/Client.php';
require_once '../../../Google/Service/YouTube.php';

Below is the code to access the playlists.

$client = new Google_Client();
$client->setDeveloperKey('AIzXXXXXXXXXXXXXXXXXXXXXXXX');
$youtube = new Google_Service_YouTube($client);

$playlists = $youtube->playlists->listPlaylists("snippet,localizations,status",array('channelId' => 'UCFtEEv80fQVKkD4h1PF-Xqw', "maxResults" =>50));

foreach ($playlists['items'] as $playlist) 
{
   echo $playlist["id"],
   echo $playlist["snippet"]["thumbnails"]["medium"]["url"],
   echo $playlist["snippet"]["localized"]["title"]
}

It’s that easy.

Now we will see the code to get the videos from a playlist.

$playlistItems = $youtube->playlistItems->listPlaylistItems('id,snippet,contentDetails,status',  array('playlistId' => 'playlistId here','maxResults' => 50));	
foreach ($playlistItems['items'] as $playlistItem) 
{
  echo $playlistItem["snippet"]["resourceId"]["videoId"];
  echo $playlistItem["snippet"]["thumbnails"]["medium"]["url"];
  echo $playlistItem["snippet"]["title"];
  echo $playlistId;
}

 

Leave a comment