如何获取指定频道的所有视频?

时间:2014-01-19 04:32:06

标签: javascript php youtube youtube-api

我希望能够从youtube频道中重新播放YouTube视频。

我的目标是在页面上显示它们。目前我通过获取这样的iframe来手动完成:

<iframe width="560" height="315" 
        src="//www.youtube.com/embed/snRkGatdzm0" 
        frameborder="0" allowfullscreen></iframe>

我知道youtube API。我找不到使用它来从频道获取所有视频的方法。

例如,如上所述,我只需要链接ID - &gt; snRkGatdzm0

我更喜欢使用php,所以如果我得到一个视频ID数组。我会像这样循环它们:

foreach ($youtubevideos as $youtubevideo){
    echo '<iframe src="//www.youtube.com/embed/$youtubevideo"></iframe>'
}

注意:循环可能是错误的(我添加了这个假循环因为我知道你们喜欢看代码大声笑)

请指教。


我不知道我是否足够清楚,我只是希望能够从频道获取所有视频链接,以便我可以在页面中显示它们。我不想硬编码。这就是为了缩短它。

4 个答案:

答案 0 :(得分:1)

您可以从以下网址获取有关来自youtube的视频的xml数据,其中channelname是YouTube频道的名称。

https://gdata.youtube.com/feeds/api/users/channelname/uploads

以下示例

https://gdata.youtube.com/feeds/api/users/TheEllenShow/uploads

您可以使用DOmDOcument或SimpleXMl来解析xml并获取数据。

示例代码

   $channelURL = 'https://gdata.youtube.com/feeds/api/users/TheEllenShow/uploads'; 
    $sxml = simplexml_load_file($channelURL);

您可以循环浏览entry节点以获取视频信息。还有一个link节点,它将保存有关该频道的其他信息。您还可以通过将以下参数附加到网址?start-index=1&max-results=25来指定视频的起始索引和限制。 例如:

https://gdata.youtube.com/feeds/api/users/TheEllenShow/uploads?start-index=26&max-results=25

希望这会对你有所帮助。

[为用户更新的部分]

$channelURL = 'http://gdata.youtube.com/feeds/api/videos/snRkGatdzm0';
$xml = simplexml_load_file($channelURL);
$title = $xml->title; //Gets the title
$ChannelAuthor = $xml->author;  //Get the author tag to get the channel name
$channelName =$ChannelAuthor->uri; //Channel name fetched

$videoURL ="https://gdata.youtube.com/feeds/api/users/$channelName/uploads";
$videoXml = simplexml_load_file($videoURL);
$title = $videoXml->title;//Get Title from video url
//Now loop through video information
foreach ($videoXml->entry as $entry) {
  $video = $entry->children('http://search.yahoo.com/mrss/');
  // get video URL
  $URL = $media->group->player->attributes();
  $watchURL = $URL['url'];//video URL
  $thmb = $media->group->thumbnail[0]->attributes();
  $videoThumb = $thmb['url'];//gets the video thumb nail
  ..........
}

您可以检查xml并按上面的方法检索其值

答案 1 :(得分:0)

利用DOMexplode()

<?php
$html='<iframe width="560" height="315"
        src="//www.youtube.com/embed/snRkGatdzm0"
        frameborder="0" allowfullscreen></iframe>';
    $dom = new DOMDocument;
    $dom->loadHTML($html);
foreach ($dom->getElementsByTagName('iframe') as $tag) {
    $ylink=$tag->getAttribute('src');
    }

$ylink = explode('/',$ylink);
echo $ylink = array_pop($ylink);

输出:

snRkGatdzm0

获取一系列链接....

这样做......

foreach ($dom->getElementsByTagName('iframe') as $tag) {
    $ylink[]=$tag->getAttribute('src'); //<---- Just make this $ylink as an array as shown.
    }

在这样的话题之后......来得到你的ids ...

$linkids = array_map('processIds',$ylink);

function processIds($v)
{
 $v = explode('/',$v);
 $v = array_pop($v);
 return $v;
}

print_r($linkids);

输出:

snRkGatdzm0
rdd52d9z034
...

答案 2 :(得分:0)

由于gdata Api很快就会被弃用,最好的办法是使用Data API从该频道的播放列表中获取视频。

这是一个更详细的答案。 How to list all uploaded videos (URL) of a YouTube channel with API v3?

您可以使用Google Javascript client library在您的网站上实现此目的。

答案 3 :(得分:0)

以下是使用YouTube API 2.0实现目标的示例代码。

这只是快速而肮脏的方式。请注意,由于YouTube 3.0已经推出,因此很快就会弃用。

    $channelName ='YourChannelName';
    $channelURL ="http://gdata.youtube.com/feeds/api/users/$channelName/uploads";
    $xml=simplexml_load_string(file_get_contents($channelURL));


    foreach ($xml->entry as $entry) {
        //Get each link
        $link = $entry->id;
        //Retrieve video id from link
        $link_array = explode('/',$link);
        $video_id = array_pop($link_array);
        //Create the iframe
        echo "<iframe src=\"//www.youtube.com/embed/$video_id\"></iframe>";
    }