我在通过 API v3 获取 YouuTbe 中的播放列表信息时遇到问题。
我只需要将 JSON 传递给值。
我在 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>
关于php - 从 YouTube API v3 中的播放列表获取所有视频信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296168/