<?php
$apiKey = 'AIzaSyC7udxUIPhN213ilX9Reyc_ZzXN5_id5eg';
$channelId = 'UC226a2gZas0iS2KUtrF7oFw';
$maxResults = 50;
$results = fetchLists($apiKey, $channelId, $maxResults);
function fetchLists($apiKey, $channelId, $maxResults, $listsNextPage = null, $playlists = array())
{
$ytApiUrl = 'https://www.googleapis.com/youtube/v3/playlists?maxResults=' . $maxResults . '&part=snippet&channelId=' . $channelId . '&key=' . $apiKey ;
if($listsNextPage !== null){
$ytApiUrl .= '&pageToken=' . $listsNextPage;
}
$jsonLists = json_decode(file_get_contents($ytApiUrl));
$listsPageToken = (isset($jsonLists->nextPageToken)) ? $jsonLists->nextPageToken : null ;
foreach ($jsonLists->items as $item) {
$plId = $item->id;
$list = array(
"id" => $plId,
"title" => $item->snippet->title,
"published" => $item->snippet->publishedAt,
"url" => 'https://www.youtube.com/playlist?list=' . $plId,
"thumbnail" => $item->snippet->thumbnails->default->url,
"videos" => []
);
fetchVideos($list, $playlists, $maxResults, $plId, $apiKey);
}
if($listsPageToken !== null){
return fetchLists($apiKey, $channelId, $maxResults, $listsPageToken, $playlists);
}
return $playlists;
}
function fetchVideos(&$list, &$playlists, &$maxResults, &$plId, &$apiKey, $vidsNextPage = null) {
$plApiUrl = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults='. $maxResults .'&playlistId=' . $plId . '&key=' . $apiKey;
if($vidsNextPage !== null){
$plApiUrl .= '&pageToken=' . $vidsNextPage;
}
$jsonPlVideos = json_decode(file_get_contents($plApiUrl));
$videosPageToken = (isset($jsonPlVideos->nextPageToken)) ? $jsonPlVideos->nextPageToken : null ;
foreach ($jsonPlVideos->items as $vid) {
$video = array(
"title" => $vid->snippet->title,
"url" => 'https://www.youtube.com/watch?v=' . $vid->snippet->resourceId->videoId,
"published" => $vid->snippet->publishedAt,
"thumbnail" => $vid->snippet->thumbnails->default->url
);
$list['videos'][] = $video;
}
$playlists[] = $list;
if($videosPageToken !== null){
return fetchVideos($list, $playlists, $maxResults, $plId, $apiKey, $videosPageToken);
}
}
preferences:
109.59 ms | 406 KiB | 5 Q