[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);}

我想要什么:

现在我只是把视频ID放进一个链接:

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

通过这种方式,我可以将视频ID嵌入到我嵌入视频的另一个页面中:

$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'];

它返回空

如果有人能帮我一点忙,那将是很棒的

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

由于我想从视频列表中检索信息(即使在这种情况下列表由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; ?>

希望它能帮助