diff --git a/src/Madcoda/Youtube.php b/src/Madcoda/Youtube.php index 5cd4e49..e0b1d82 100644 --- a/src/Madcoda/Youtube.php +++ b/src/Madcoda/Youtube.php @@ -405,7 +405,43 @@ public function getPlaylistItemsByPlaylistIdAdvanced($params, $pageInfo = false) } } + /** + * @param $playlistId + * @return array + * @throws \Exception + */ + public function getAllPlaylistItemsByPlaylistId($playlistId) { + $playlistItems = []; + $params = array( + 'playlistId' => $playlistId, + 'part' => 'id, snippet, contentDetails, status', + 'maxResults' => $maxResults + ); + + $raw = $this->getPlaylistItemsByPlaylistIdAdvanced($params, true); + + if ($raw['results'] !== false) { + foreach ($raw['results'] as $result) { + $playlistItems[] = $result; + } + + if ($raw['info']['nextPageToken'] !== null) { + do { + $params['pageToken'] = $raw['info']['nextPageToken']; + $raw = $this->getPlaylistItemsByPlaylistIdAdvanced($params, true); + + if ($raw['results'] !== false) { + foreach ($raw['results'] as $result) { + $playlistItems[] = $result; + } + } + } while ($raw['info']['nextPageToken'] !== null); + } + } + return $playlistItems; + } + /** * @param $channelId * @return array diff --git a/test/Madcoda/Tests/YoutubeTest.php b/test/Madcoda/Tests/YoutubeTest.php index 456a308..1f4f3de 100644 --- a/test/Madcoda/Tests/YoutubeTest.php +++ b/test/Madcoda/Tests/YoutubeTest.php @@ -186,7 +186,15 @@ public function testGetPlaylistItemsByPlaylistId() $this->assertTrue(count($response) > 0); $this->assertEquals('youtube#playlistItem', $response[0]->kind); } - + + public function testGetAllPlaylistItemsByPlaylistId() + { + $GOOGLE_ZEITGEIST_PLAYLIST = 'PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs'; + $response = $this->youtube->getAllPlaylistItemsByPlaylistId($GOOGLE_ZEITGEIST_PLAYLIST); + $this->assertTrue(count($response) > 0); + //$this->assertEquals('youtube#playlistItem', $response[0]->kind); + } + public function testParseVIdFromURLFull() { $vId = $this->youtube->parseVIdFromURL('http://www.youtube.com/watch?v=1FJHYqE0RDg'); @@ -262,4 +270,4 @@ public function testNotFoundAPICall2() //$response = $this->youtube->getPlaylistsByChannelId($channelId); //$this->assertFalse($response); } -} \ No newline at end of file +}