3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $apiKey = 'AIzaSyC7udxUIPhN213ilX9Reyc_ZzXN5_id5eg'; $channelId = 'UC226a2gZas0iS2KUtrF7oFw'; $maxResults = 50; $results = fetchLists($apiKey, $channelId, $maxResults); function fetchLists($apiKey, $channelId, $maxResults, $listsNextPage = null, $playlists = array()) { $ytApiUrl = 'https://www.googleapis.com/youtube/v3/playlists?maxResults=' . $maxResults . '&part=snippet&channelId=' . $channelId . '&key=' . $apiKey ; if($listsNextPage !== null){ $ytApiUrl .= '&pageToken=' . $listsNextPage; } $jsonLists = json_decode(file_get_contents($ytApiUrl)); $listsPageToken = (isset($jsonLists->nextPageToken)) ? $jsonLists->nextPageToken : null ; foreach ($jsonLists->items as $item) { $plId = $item->id; $list = array( "id" => $plId, "title" => $item->snippet->title, "published" => $item->snippet->publishedAt, "url" => 'https://www.youtube.com/playlist?list=' . $plId, "thumbnail" => $item->snippet->thumbnails->default->url, "videos" => [] ); fetchVideos($list, $playlists, $maxResults, $plId, $apiKey); } if($listsPageToken !== null){ return fetchLists($apiKey, $channelId, $maxResults, $listsPageToken, $playlists); } return $playlists; } function fetchVideos(&$list, &$playlists, &$maxResults, &$plId, &$apiKey, $vidsNextPage = null) { $plApiUrl = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults='. $maxResults .'&playlistId=' . $plId . '&key=' . $apiKey; if($vidsNextPage !== null){ $plApiUrl .= '&pageToken=' . $vidsNextPage; } $jsonPlVideos = json_decode(file_get_contents($plApiUrl)); $videosPageToken = (isset($jsonPlVideos->nextPageToken)) ? $jsonPlVideos->nextPageToken : null ; foreach ($jsonPlVideos->items as $vid) { $video = array( "title" => $vid->snippet->title, "url" => 'https://www.youtube.com/watch?v=' . $vid->snippet->resourceId->videoId, "published" => $vid->snippet->publishedAt, "thumbnail" => $vid->snippet->thumbnails->default->url ); $list['videos'][] = $video; } $playlists[] = $list; if($videosPageToken !== null){ return fetchVideos($list, $playlists, $maxResults, $plId, $apiKey, $videosPageToken); } }
Output for 7.1.0
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /in/qe40e on line 14 Warning: file_get_contents(https://www.googleapis.com/youtube/v3/playlists?maxResults=50&part=snippet&channelId=UC226a2gZas0iS2KUtrF7oFw&key=AIzaSyC7udxUIPhN213ilX9Reyc_ZzXN5_id5eg): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /in/qe40e on line 14 Notice: Trying to get property of non-object in /in/qe40e on line 16 Warning: Invalid argument supplied for foreach() in /in/qe40e on line 16