Skip to content

Instantly share code, notes, and snippets.

@zajca
Created May 13, 2015 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zajca/443b6943c5e217f2acff to your computer and use it in GitHub Desktop.
Save zajca/443b6943c5e217f2acff to your computer and use it in GitHub Desktop.
youtube parse videos from playlist
<?php
//PLAYLIST ID
$playlistId = 'PLzTOsjrpK9WoMm-MzY6Zquk_ZMigv0lFp';
//ADD HERE YOUTUBE API KEY
$apiKey = '';
$url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=id,status,contentDetails,snippet&maxResults=50&playlistId='.$playlistId.'&key='.$apiKey;
$data = json_decode(file_get_contents($url));
if ( ! empty($data->items) )
{
foreach ($data->items as $item) {
if ( ! empty($item->contentDetails->videoId) ){
$ids[] = [
'id'=>$item->contentDetails->videoId,
'link'=>$item->link,
'title'=>$item->snippet->title,
'pubDate'=>$item->snippet->publishedAt,
'iframe'=> '<iframe class="youtube" width="100%" height="421" src="https://www.youtube.com/embed/'.$item->contentDetails->videoId.'?rel=0&amp;controls=0&amp;showinfo=0&amp;autoplay=1" frameborder="0" allowfullscreen=""></iframe>'
];
}
}
}
$fp = fopen(getcwd().'/results.json', 'w');
fwrite($fp, json_encode($ids));
fclose($fp);
header('Content-Type: application/json');
echo json_encode($ids);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment