我想使用新的 youtube API 从特定播放列表中检索所有视频(其 ID 和标题)的列表。
我需要将 id 分开,以便我可以在我的网站上使用 php 制作一个“视频库”,而不是只有一个带有播放列表侧边栏的视频。
这已经在我的网站上运行,但由于新 API 已于 6 月 4 日实施,它不再运行。
有什么解决办法吗?谢谢你。
YouTube API 站点上有一个 PHP 示例,它使用 playlistItems.list 调用来获取视频列表,您可以使用这些列表来获取所需的信息。
http://developers.google.com/youtube/v3/docs/playlistItems/list
如果您使用YouTube PHP 客户端库,那么这些行将获取指定播放列表的所有视频 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>
如果您在实施时遇到任何问题,请将您的代码作为新问题发布,有人将能够提供帮助。