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

时间:2015-08-30 11:42:16

标签: php json youtube youtube-api

我在使用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;

1 个答案:

答案 0 :(得分:4)

您需要在代码中进行两项重大更改:

以下是您可以参考的工作代码:

<?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>
相关问题