Created
May 13, 2015 10:28
-
-
Save zajca/443b6943c5e217f2acff to your computer and use it in GitHub Desktop.
youtube parse videos from playlist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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&controls=0&showinfo=0&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