【问题标题】:Shuffle youtube IDs随机播放 youtube ID
【发布时间】:2015-01-22 23:51:36
【问题描述】:

我是 php 新手,我想知道如何检索已洗牌的 YouTube ID。 这就是我的意思..

$playlist_id = "PLB9DAD6B9EDAEE7BC";

$cont = json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/playlists/'.$playlist_id.'/?v=2&alt=json&feature=plcp'));
$feed = $cont->feed->entry;

if(count($feed)) {
    foreach($feed as $item) {
        $title = $item->title->{'$t'};
        $desc = $item->{'media$group'}->{'media$description'}->{'$t'};  
        $id = $item->{'media$group'}->{'yt$videoid'}->{'$t'};
    }
}

这基本上是从播放列表中获取 id、标题和描述,我怎样才能洗牌 $id 给我唯一的不重复值,以便我以后可以在这里使用它?

<iframe ... src="http://www.youtube.com/embed/<?= $id ?>" allowfullscreen></iframe>

我的目标是每次访问时刷新页面以获取新视频,并在结束时自行重置(或继续选择唯一值)

提前谢谢你,非常感谢..

【问题讨论】:

    标签: php json youtube-api shuffle


    【解决方案1】:

    您可以将所有 feed 视频存储在一个数组中,然后使用 array_rand 获取该数组的随机条目。

    函数参考见http://php.net/manual/de/function.array-rand.php。 请注意,当与默认设置一起使用时,array_rand 返回一个键,但如果您选择选择多个而不是单个随机条目,它将提供一个键数组。

    编辑:添加了 cookie,因此视频是真正的随机唯一

    代码sn-p:

    $playlist_id = "PLB9DAD6B9EDAEE7BC";
    
    $cont = json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/playlists/'.$playlist_id.'/?v=2&alt=json&feature=plcp'));
    $feed = $cont->feed->entry;
    
    $youtubeVideos = array();
    
    if(count($feed))
    {
      foreach($feed as $item)
      {
        // build video array
        $video = array();
        $video['title'] = $item->title->{'$t'};
        $video['desc'] = $item->{'media$group'}->{'media$description'}->{'$t'};
        $video['id'] = $item->{'media$group'}->{'yt$videoid'}->{'$t'};
    
        // push it into collection
        $youtubeVideos[$video['id']] = $video;
      }
    }
    
    $seenVideos=array();
    $lastSeenVideo='';
    
    // only get diff array if the cookies are set (= not first page view)
    if(isset($_COOKIE['seen_youtube_videos']) && isset($_COOKIE['last_youtube_video']))
    {
      $lastSeenVideo=$_COOKIE['last_youtube_video'];
    
      $seenVideos=unserialize($_COOKIE['seen_youtube_videos']);
      $diffArr=$youtubeVideos;
    
      foreach($seenVideos as $vidId)
        unset($diffArr[$vidId]);
    
      if(count($diffArr)>0)
      {
        // set difference for searching only
        $youtubeVideos=$diffArr;
      }
      else
      {
        // if we did show all videos, reset everything
        setcookie('seen_youtube_videos', '');
        setcookie('last_youtube_video', '');
        $seenVideos = array();
      }
    }
    
    $randomizedKey = array_rand($youtubeVideos);
    $randomVideo = $youtubeVideos[$randomizedKey];
    
    do
    {
      $randomizedKey = array_rand($youtubeVideos);
      $randomVideo = $youtubeVideos[$randomizedKey];
    }
    while($randomVideo['id'] == $lastSeenVideo);
    
    
    $seenVideos[] = $randomVideo['id'];
    setcookie('seen_youtube_videos', serialize($seenVideos));
    setcookie('last_youtube_video', $randomVideo['id']);
    
    // do stuff with $randomVideo
    

    【讨论】:

    • 我试过了,效果很好,但是关闭浏览器后cookies不会死,使用会话更好吗?
    • 一般来说,使用会话是一个完全奇怪的构造,并且根据 setcookie 函数的定义(参见此处:php.net/manual/en/function.setcookie.php),当浏览器关闭时,cookie 将被删除,如下所述:“过期:时间“如果将 expire 参数设置为显式 0 有帮助,您可以尝试
    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多