Data from Youtube Playlist

We will be using Google YouTube API to connect and fetch data. Read more on Google Developers YouTube API

Use your own YouTube API while using this script. Check the demo of the script showing Tkinter video list here.

Developing the script in PHP

Looping through all the videos of the playlist is done by reading and passing the nextPageToken value in each successive loops. Once the last page is reached, no value is returned by nextPageToken.
At the end of each loop we will check the length of the value of nextPageToken and then terminate the loop of it is not available.
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;

Generate Video schema for web page
Check your page for Rich results ( Google ) Generate Thumbnail URL of your Video
All PHP Scripts PHP Home
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer