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.