从播放列表中获取所有视频(Youtube API v3)

时间:2015-06-05 21:07:24

标签: xml api video youtube playlists

我想使用新的youtube API从特定的播放列表中检索所有视频(其ID和标题)的列表。

我需要将id分开,以便我可以在我的网站上使用php创建一个“视频库”,而不是只有一个带有播放列表侧边栏的视频。

这已经在我的网站上运行了,但是由于新的API已于6月4日实施,它已不再适用了。

任何解决方案? 谢谢。

1 个答案:

答案 0 :(得分:0)

YouTube API网站上有一个PHP示例,它使用playlistItems.list调用来获取可用于获取所需信息的视频列表。

http://developers.google.com/youtube/v3/docs/playlistItems/list

如果您使用YouTube PHP client library,那么沿着这些行的内容将获得指定播放列表的所有视频ID和标题:

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

如果您在实施时遇到任何问题,请将您的代码发布为新问题,然后有人可以提供协助。