2014-11-25 1 views
1

PHP에 새로운 오전과 내가 유튜브 ID를 단행 조회하는 수 있는지 궁금 해서요 .. 여기 는셔플 유튜브 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>

내 목표는 새로운 비디오를 내가 방문 자체를 재설정 할 때마다 얻을 수있는 페이지를 새로 고침하는 것입니다 많이 ..

답변

1

배열에 모든 피드 비디오를 저장 한 다음 array_rand를 사용하여 배열의 임의 항목을 가져올 수 있습니다.

기능 참조를 위해 http://php.net/manual/de/function.array-rand.php을 참조하십시오. array_rand는 기본 설정으로 사용될 때 단일 키를 반환하지만 하나 이상의 임의 항목을 선택하도록 선택하면 키 배열을 전달한다는 점에 유의하십시오.

편집 : 추가 된 쿠키 비디오가 REAL 임의 고유 그래서

코드 :

$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 
+0

나는 그것을 시도하고 잘 작동하지만, 브라우저가 종료 된 후 쿠키가 죽지 않는, 더 나은 세션을 사용하고 있습니까? –

+0

일반적으로 세션을 사용하는 것은 완전한 괴상한 구조이며 setcookie 함수의 정의에 따라 다릅니다 (http://php.net/manual/en/function.setcookie.php 참조). 설명 된대로 브라우저가 닫힐 때 쿠키가 삭제됩니다 : "만료 : 쿠키가 만료되는 시간입니다. 이것은 유닉스 타임 스탬프이므로 신기원 이후의 초 단위입니다. [...] 0으로 설정되거나 생략되면 세션이 끝날 때 쿠키가 만료됩니다 브라우저가 닫힐 때). " 만료 매개 변수를 명시 적 0으로 설정하면 시도 할 수 있습니다. –