{"id":4527,"date":"2023-12-25T17:17:21","date_gmt":"2023-12-25T17:17:21","guid":{"rendered":"https:\/\/truelogic.org\/wordpress\/?p=4527"},"modified":"2023-12-25T17:17:21","modified_gmt":"2023-12-25T17:17:21","slug":"33-youtube-data-api-get-all-playlistitems-for-a-playlist","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2023\/12\/25\/33-youtube-data-api-get-all-playlistitems-for-a-playlist\/","title":{"rendered":"33 \u2013 YouTube Data API \u2013 Get All PlaylistItems For A Playlist"},"content":{"rendered":"\n<p>In continuation of article 32 previous to this, we see how to fetch the playlistItems in a playlist. As per the API a playlistItem can contain different kinds of data, but so far, it only comprises of video elements. Which means that each playlistItem will have the same fields as a video viz.title, description, publishedAt, videoId etc.<\/p>\n\n\n\n<p>The logic to get playlistItems is the same as getting videos from a channel. Each request can get a maximum of 50 items. There is no option to sort the items on any field. And in case there are more playlists available, the nextPageToken will contain the token to do the next fetch request.<\/p>\n\n\n\n<p>The Request URL is<\/p>\n\n\n\n<p>GET <a href=\"https:\/\/www.googleapis.com\/youtube\/v3\/playListItems\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.googleapis.com\/youtube\/v3\/playListItems<\/a><\/p>\n\n\n\n<p>Sample code to fetch the playlistItems of a PlayList.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);\n    set_time_limit(60 * 3);\n    \n    $g_youtubeDataAPIKey = \"xxx\";\n        \n    $output = \"\"; \n    $channelId = \"UC5_j0dmvXE0xs6ra-clGz4A\";\n    $playlistId = \"PLC98ABC853EAEFD7F\";\n\n        \/\/ make api request\n        $url = \"https:\/\/www.googleapis.com\/youtube\/v3\/playlistItems?part=snippet&amp;playlistId=\" .\n            $playlistId. \"&amp;maxResults=50&amp;key=\" . $g_youtubeDataAPIKey;\n\techo($url . \"\\n\");\n\t\/\/$handle = fopen('php:\/\/temp', 'w+');\n\n        $curl = curl_init();\n        curl_setopt_array($curl, array(\n                    CURLOPT_RETURNTRANSFER => 1,\n                    CURLOPT_URL => $url,\n                    CURLOPT_USERAGENT => 'Codular Sample cURL Request',\n                    CURLOPT_SSL_VERIFYPEER => 1,\n                    CURLOPT_SSL_VERIFYHOST=> 0,\n\t\t    CURLOPT_VERBOSE=>false,\n\t\t    CURLOPT_FOLLOWLOCATION => TRUE\n                    ));\n        $resp = curl_exec($curl);\n\n        curl_close($curl);\n    if ($resp) {\n            $json = json_decode($resp);\n            if ($json) {\n                $nextPageToken = $json->nextPageToken;\n                $total = $json->pageInfo->totalResults;\n                $items = $json->items;\n            }\n\n            echo(\"nextpage=\" . $nextPageToken . \"&lt;br>total=\" . $total . \"&lt;br>items=\" . count($items));\n            foreach($items as $item) {\n                $videoId = $item->snippet->resourceId->videoId;\n                $videoTitle = $item->snippet->title;\n                $videoDesc = $item->snippet->description;\n                $thumbnail= $item->snippet->thumbnails->high->url;\n                $rawDate = $item->snippet->publishedAt;\n\n                $output .= $videoId . \", \" . $videoTitle . \",\" . $rawDate . \"\\n\";\n                    \n            }\n\n            \/\/ second level search using nextpage token\n            echo(\"&lt;br>&lt;h2>Second phase&lt;\/h2>&lt;br>\");\n            while ($nextPageToken != null) {\n                $url = \"https:\/\/www.googleapis.com\/youtube\/v3\/search?part=snippet&amp;channelId=\" .\n                    $channelId. \"&amp;maxResults=50&amp;order=date&amp;type=video&amp;\" .\n                    \"&amp;pageToken=\" . $nextPageToken . \"&amp;key=\" . $g_youtubeDataAPIKey;\n                \n                $curl = curl_init();\n                curl_setopt_array($curl, array(\n                            CURLOPT_RETURNTRANSFER => 1,\n                            CURLOPT_URL => $url,\n                            CURLOPT_USERAGENT => 'Codular Sample cURL Request',\n                            CURLOPT_SSL_VERIFYPEER => 1,\n                            CURLOPT_SSL_VERIFYHOST=> 0,\n                            CURLOPT_FOLLOWLOCATION => TRUE\n                            ));\n                $resp = curl_exec($curl);\n\n                if ($resp) {\n                    $json = json_decode($resp);\n                    if ($json) {\n                        $nextPageToken = $json->nextPageToken;\n                        $total = $json->pageInfo->totalResults;\n\n                        $items = $json->items;\n                    }\n\n                    echo(\"nextpage=\" . $nextPageToken . \"&lt;br>total=\" . $total .\n                            \"&lt;br>items=\" . count($items));\n            \n                    foreach($items as $item) {\n\t\t        $videoId = $item->snippet->resourceId->videoId;\n                        $videoTitle = $item->snippet->title;\n                        $videoDesc = $item->snippet->description;\n                        $thumbnail= $item->snippet->thumbnails->high->url;\n                        $rawDate = $item->snippet->publishedAt;\n                        \n\t\t \t$output .= $videoId . \", \" . $videoTitle . \",\" . $rawDate . \"\\n\";\n\n\n                    } \/\/ foreach\n                    \n                } \/\/ if $resp\n                \n                \/\/ sometimes pagetoken is filled but no items are fetched , in such a case stop the code\n                if (count($items) == 0)\n                    $nextPageToken = null;\n                    \n            } \/\/ while $nextPageToken\n\n\t    echo($output);\n           \t   \t \n    } \/\/ if $resp || incremental\n    else {\n\texit(\"Youtube api request did not work\");\n    }\n\n    \n?>\n\n<\/code><\/pre>\n\n\n\n<p>It is assumed that the code will be run on the command so the output uses newline and tab symbols for basic formatting. You can use html tags if the code will be run in a browser. Here is some part of the output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">https:\/\/www.googleapis.com\/youtube\/v3\/playlistItems?part=snippet&amp;playlistId=PLC98ABC853EAEFD7F&amp;maxResults=50&amp;key=AIzaSyAJrlFVIJEP0ZgT9tMCJZp_ILy6MsjGhTA\nnextpage=&lt;br>total=25&lt;br>items=25&lt;br>&lt;h2>Second phase&lt;\/h2>&lt;br>C1ttJju1RKc, Corso Pascal (Turbo\/FPC)  - 01 Struttura del corso,2011-12-31T07:55:09Z\ncblRxEP9wo0, Corso Pascal (Turbo\/FPC)  02 Struttura. Identificatori. Principali errori.,2011-12-31T07:55:09Z\nwgXtF-R1trU, Corso Pascal (Turbo\/FPC) 03 Unit. Crt. Uso dell'Help. Colori. Costanti. Un esercizio.,2011-12-31T14:44:54Z\nPGRj2Cp4Uz4, Corso Pascal (Turbo\/FPC) 04 Ciclo FOR. Assegnamenti. Debugger.,2011-12-31T19:52:34Z\nbpOwOS6-2v4, Corso Pascal (Turbo\/FPC)  05 Cicli annidati. Input da tastiera. Numeri casuali. Delay.,2012-01-01T16:35:50Z\nkA672A0dwKo, Corso Pascal (Turbo\/FPC) 06 Ciclo Repeat. Contatori. If ... then. Break point condizionali.,2012-01-01T20:44:27Z\nrEq7FDuvhTw, Corso Pascal (Turbo\/FPC) 07 Condizioni and\/or, for vs repeat,2012-01-01T20:44:01Z\n7G9Wm8ZZk60, Corso Pascal (Turbo\/FPC)  08 cicli annidati, if in cascata, numeri casuali, while,2012-01-01T21:27:17Z\nLJdHv-BRaLI, Corso Pascal (Turbo\/FPC) 09 Gioco dei dadi, case of,2012-01-01T22:22:17Z\nSCxJPsgPxFo, Corso Pascal (Turbo\/FPC) 10  Esercizi e dispensa,2012-01-02T19:33:09Z\nZU95iarF1EE, Corso Pascal (Turbo\/FPC) 11 Complessit\u00e0 del sw, top down, sottoprogrammi,2012-01-03T16:33:51Z\nJkkGblGkQu0, Corso Pascal (Turbo\/FPC) 12 Procedure e funzioni,2012-01-04T23:51:07Z\nk3lvR2DwDsU, Corso Pascal (Turbo\/FPC) 13 Array, algoritmi classici con gli array,2012-01-05T21:07:49Z\nNiZjoutMgVw, Corso Pascal (Turbo\/FPC) 14 Array bidimensionali (matrici) ed algoritmi classici,2012-01-06T22:25:39Z\nfz_VIyowkVo, Corso Pascal - 15 (Turbo\/FPC) Record,2012-01-08T19:28:01Z\nMN8CDG4B0_w, Corso Pascal 16 (Turbo\/FPC) File di testo,2012-01-11T16:49:51Z\nB1puG7xgyK4, Corso Pascal 17 (Turbo\/FPC) File Tipizzati,2012-01-11T18:45:33Z\nYiQo0JcUIQM, Corso Pascal 18 (Turbo\/FPC) Puntatori, RAM dinamica,2012-01-12T22:22:42Z\nOzuJSQMrFew, Corso Pascal 19 (Turbo\/FPC) Ricorsione,2012-01-14T22:38:03Z\nR40FaRUBxks, Corso Pascal 20 (Turbo\/FPC) Alberi binari con puntatori,2012-01-15T09:56:14Z\nasd6O3NQ_mI, Corso Pascal 21 (Turbo\/FPC) Torre di Hanoi in dettaglio (ricorsione),2012-01-15T10:25:33Z\nPuRFjD2h5Wk, Corso Pascal 22 (Turbo\/FPC) Grafica,2012-01-18T16:06:56Z\nPyQwGhqidm0, Video Corso Pascal - 23 (Turbo e FPC),2012-01-19T21:00:27Z\nlBB3Y0NXxkA, Corso Pascal 25 (Turbo\/FPC) Usare l'editor 'al massimo',2012-01-22T12:08:09Z\nulL__tpzAKk, Corso Pascal - Ed ora??,2012-01-24T19:14:31Z\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In continuation of article 32 previous to this, we see how to fetch the playlistItems in a playlist. As per the API a playlistItem can <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2023\/12\/25\/33-youtube-data-api-get-all-playlistitems-for-a-playlist\/\" title=\"33 \u2013 YouTube Data API \u2013 Get All PlaylistItems For A Playlist\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2727,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[318],"tags":[],"class_list":["post-4527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-youtube-api-with-php"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4527"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4527\/revisions"}],"predecessor-version":[{"id":4530,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4527\/revisions\/4530"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2727"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}