Pulling videos From Youtube Playlist





This time, I am going to read the youtube videos from a youtube playlist. The code is pretty simple and straight forward. The following code will pull all the videos with the playlist id 5Hc74bvAC8V6XQnPFDEYOHFhMlEPg8q1. You need to change the playlist id to the desired ones. The example not only pulls the videos but embeds them using iframe. So youtube playlist can be embeded in your website this way.

<?php
$playlist_id = "5Hc74bvAC8V6XQnPFDEYOHFhMlEPg8q1";
$url = "https://gdata.youtube.com/feeds/api/playlists/".$playlist_id."?v=2&alt=json";
$data = json_decode(file_get_contents($url),true);
$info = $data["feed"];
               
$video = $info["entry"];
$nVideo = count($video); //number of videos
                   

foreach($video as $v){          
        $title =  $v['title']['$t'];
        $link = $v['link'][0]['href'];
        $description =$v['media$group']['media$description']['$t'];
        $image = $v['media$group']['media$thumbnail'][1]['url'];
        $em = $v['media$group']['yt$videoid']['$t'];//
?>
<iframe width="560" height="315" src="http://www.youtube.com/embed/<?php echo $em; ?>" frameborder="0" allowfullscreen></iframe>        
<?php
}
?>



Comments

Popular posts from this blog

Automate file upload in Selenium IDE

How To Install and Configure Nextcloud