do{
$x="https://www.googleapis.com/youtube/v3/playlistItems?
part=snippet%2CcontentDetails&maxResults=20&
playlistId=PLzz7R3Slbh5SBgyevCsjVXXygbWZCAPTa&
key=YOUR_API_KEY&pageToken=$p_token";
$json = file_get_contents($x);
$playlist = json_decode($json);
//echo $obj->items;
//echo var_dump($playlist);
echo "<hr>";
$i=0;
foreach($playlist->items as $item) {
//print($item->snippet->publishedAt); // working
//print($item->snippet->resourceId->videoId); // working
//print($item->snippet->thumbnails->default->url); // working
print"<hr>";
$title=$item->snippet->title;
$v_id= $item->snippet->resourceId->videoId;
$thumb=$item->snippet->thumbnails->medium->url;
$i=$i+1;
} // end of foreach loop
echo $playlist->pageInfo->totalResults;
echo "<br>";
echo $playlist->pageInfo->resultsPerPage;
echo "<br>";
echo $playlist->nextPageToken;
echo "<br>";
echo "Total ".$i;
$next=$playlist->nextPageToken;
}while(strlen($next)>10);
Youtube will return maximum 50 results, so to add paging to the results we will use this script.
$p_token=$_GET['p_token'];
$x="https://www.googleapis.com/youtube/v3/playlistItems?
part=snippet%2CcontentDetails&maxResults=20&
playlistId=PLzz7R3Slbh5SBgyevCsjVXXygbWZCAPTa&
key=YOUR_API_KEY&pageToken=$p_token";
$json = file_get_contents($x);
$playlist = json_decode($json);
//$i=0;
//echo $playlist->pageInfo->totalResults;
//echo $playlist->pageInfo->resultsPerPage;
$next=$playlist->nextPageToken;
$prev=$playlist->prevPageToken;
$n_links='';
if(strlen($prev)>10){
$n_links.= "<a href=tkinter-video-paging.php?p_token=$prev
class='btn btn-outline-secondary'><span aria-hidden='true'>«</span> Previous</a> ";
}
if(strlen($next)>10){
$n_links.= " <a href=tkinter-video-paging.php?p_token=$next
class='btn btn-outline-secondary' > Next <span aria-hidden='true'>»</span></a>";
}
echo $n_links;
echo "<ul class='list-group'>";
foreach($playlist->items as $item) {
$title=$item->snippet->title;
$v_id= $item->snippet->resourceId->videoId;
$thumb=$item->snippet->thumbnails->medium->url;
echo "<li class='list-group-item d-flex justify-content-between align-items-center'>$title
<a href=https://youtu.be/$v_id target=new>
<img src=https://i.ytimg.com/vi/$v_id/mqdefault.jpg class='img-fluid' alt='quixote'></a>
</li>";
$i=$i+1;
}
echo "</ul>";
echo "Total ".$i."<br>";
echo $n_links;
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.