使用YouTube v3 API的PHP中的YouTube频道视频列表不起作用

时间:2019-02-10 08:24:52

标签: php youtube-api

我正在尝试使用YouTube v3 API提取并显示我的YouTube频道视频。我已经做了一些初步的事情,例如在Google Developer Console中启用YouTube v3 API。我在互联网上的某个地方获取了此代码,并按照指示进行了所有操作,但是我的视频无法显示。

<?php 
//Get videos from channel by YouTube Data API
$API_key    = 'xxxxxxxx-xxxxxxxx'; //my API key
$channelID  = 'UCxxxxxxxx'; //my channel ID
$maxResults = 10;

$video_list = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

foreach ($video_list->items as $item) {

    //Embed video
    if (isset($item->id->videoId)) {
        echo '<div class="">
                <iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
                <h2>'. $item->snippet->title .'</h2>
            </div>';
    } 

} ?>

<?php //echo count($video_list); ?>

$video_list数组返回空。

我在做什么错了?

2 个答案:

答案 0 :(得分:2)

我用密钥和示例频道来运行您的代码,并且视频显示正常。

将完成的网址复制到您的浏览器,并检查结果以验证API是否返回了您期望的内容。

答案 1 :(得分:0)

我认为您使用的是非常旧的示例代码。 Google有一个名为Google API PHP Client的PHP库,它将负责身份验证等。请尝试使用它。

您还将在其文档页面中获得示例PHP代码: