nextPageToken until the API stops returning one.<?php
$apiKey = getenv('YOUTUBE_API_KEY');
$playlistId = 'YOUR_PLAYLIST_ID';
$pageToken = '';
do {
$query = http_build_query([
'part' => 'snippet,contentDetails',
'maxResults' => 50,
'playlistId' => $playlistId,
'key' => $apiKey,
'pageToken' => $pageToken,
]);
$ch = curl_init('https://www.googleapis.com/youtube/v3/playlistItems?' . $query);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30,
]);
$json = curl_exec($ch);
if ($json === false) {
throw new RuntimeException(curl_error($ch));
}
curl_close($ch);
$data = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
foreach ($data['items'] ?? [] as $item) {
$title = $item['snippet']['title'] ?? '';
$videoId = $item['snippet']['resourceId']['videoId'] ?? '';
}
$pageToken = $data['nextPageToken'] ?? '';
} while ($pageToken !== '');
?>
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);
The playlistItems endpoint allows up to 50 results per request. For additional results, pass the returned page token to the next request.
$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 & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.