【问题标题】:How can I get all videos for a given channel?如何获取给定频道的所有视频?
【发布时间】:2014-01-19 04:32:06
【问题描述】:

我希望能够从 Youtube 频道检索 Youtube 视频。

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

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

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

所以例如上面我只需要链接 ID --> snRkGatdzm0

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

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

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

请指教。


我不知道我是否足够清楚,我只想能够从一个频道获取所有视频链接,以便我可以在页面中显示它们。我不想硬编码它。就这么简单。

【问题讨论】:

    标签: javascript php youtube youtube-api


    【解决方案1】:

    您可以从以下 URL 获取有关 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 节点,它将保存有关频道的其他信息。您还可以通过将以下参数附加到 URL ?start-index=1&amp;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 并检索其值

    【讨论】:

    • 谢谢兄弟,你明白我的问题。我更像是一个 JSON 人,但我不介意使用 XML。你知道我如何从节点snRkGatdzm0获取信息
    • 我正在尝试调整您的代码以获取每个视频的链接。什么是http://search.yahoo.com/mrss/ 为什么我需要使用雅虎?
    【解决方案2】:

    这是使用 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>";
        }
    

    【讨论】:

      【解决方案3】:

      利用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.
          }
      

      在这样的主题之后..获取您的身份...

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

      输出:

      snRkGatdzm0
      rdd52d9z034
      ...
      

      【讨论】:

      • 也许我不够清楚,我希望能够从频道中获取所有视频链接,以便将它们显示在页面中。
      • 请原谅我缺乏细节。例如,如果我给你一个像this 这样的频道,你如何编写一个页面来显示来自该频道的所有视频。所以你不必硬编码任何东西?
      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2018-03-30
        • 1970-01-01
        • 2015-09-02
        • 2011-09-13
        • 2013-04-16
        • 2016-05-13
        • 2021-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多