Jessica Wallace
Jessica Wallace

Reputation: 75

Get all video informaion from playlist in YouTube API v3

I have problem with getting the information of playlist in YouuTbe by API v3.
I just need to pass JSON to value.
I tried this in v2 but it's not working and also I don't know how I can use v3 code or what is the link that I can get the JSON from it.

$playlist_id = "AD954BCB770DB285";
$url = "https://gdata.youtube.com/feeds/api/playlists/".$playlist_id."?v=2&alt=json";
$data = json_decode(file_get_contents($url),true);
echo  $data;

Upvotes: 3

Views: 6209

Answers (1)

AniV
AniV

Reputation: 4037

You need to make two significant changes in your code:

Here is the working code you can refer to:

<?php

require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';

$client = new Google_Client();
$client->setDeveloperKey('{YOUR-API-KEY}');
$youtube = new Google_Service_YouTube($client);

$nextPageToken = '';
$htmlBody = '<ul>';

do {
    $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
    'playlistId' => '{PLAYLIST-ID-HERE}',
    'maxResults' => 50,
    'pageToken' => $nextPageToken));

    foreach ($playlistItemsResponse['items'] as $playlistItem) {
        $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'], $playlistItem['snippet']['resourceId']['videoId']);
    }

    $nextPageToken = $playlistItemsResponse['nextPageToken'];
} while ($nextPageToken <> '');

$htmlBody .= '</ul>';
?>

<!doctype html>
<html>
  <head>
    <title>Video list</title>
  </head>
  <body>
    <?= $htmlBody ?>
  </body>
</html>

Upvotes: 4

Related Questions