我在通过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>