从YouTube API v3的播放列表中获取所有视频信息


Get all video informaion from playlist in YouTube API v3

通过API v3获取youtube播放列表信息有问题。
我只需要传递JSON给value。
我在v2中尝试过,但它不起作用,我也不知道如何使用v3代码或我可以从中获得JSON的链接。

$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;

您需要在代码中做两个重要的更改:

    使用Google API client Library for PHP
  • 使用Youtube V3调用播放列表中的项目列表。

你可以参考下面的工作代码:

<?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>