[PHP][Youtube api v3] 检索单个视频的信息

[PHP][Youtube api v3] retrieving the informations for a single video

我在使用 YouTube api 时遇到了一些问题。我想从单个视频中检索信息。

我已经得到的:

我可以从指定的播放列表中获取视频及其所有信息(如标题、描述等)

我从 Url 中获取 ID 播放列表:

$playlist = $_GET['pl'];

然后我从播放列表中获取视频信息:

$response = $youtube->playlistItems->listPlaylistItems('id,snippet,contentDetails', ['playlistId' => $playlist, 'maxResults' => '10']);

然后借助 foreach,我可以轻松地从这些视频中获取和显示信息:

foreach($response['items'] as $video){
echo $video['contentDetails']['videoId']; 
echo $video['snippet']['title'];
echo substr($video['snippet']['description'], 0, 430);}

我想要什么:

现在我只是将 videoId 放入 link :

href="index.php?requ=video&v=<?= $video['contentDetails']['videoId']; ?>&pl=<?= $playlist ?>"

这样我就可以将 videoId 放到我嵌入视频的另一个页面中:

$vid = $_GET['v'];
<iframe height ="432" width="760"  src="https://www.youtube.com/embed/<?= $vid; ?>"></iframe>

但问题来了,我无法从该视频中获取任何信息,当我这样尝试时:

$viewing = $youtube->videos->listVideos("snippet", ['id' => $vid]);
echo $viewing['snippet']['title'];

它returns我NULL

如果有人能帮我一点忙,那就太好了

我终于自己找到了解决方案。

因为我想从视频列表中检索信息(即使在这种情况下该列表由 1 个元素组成),我只需要像这样使用 foreach 来检索:

<?php foreach($viewing['items'] as $view): ?>
                        <p><h3><?= $view['snippet']['title']; ?></h3></p>
                        <p><?= substr($view['snippet']['description'], 0, 430); ?></p>
                        <?php endforeach; ?>

希望能帮到你