2012-02-06 3 views
2

colorbox로 열리는 YouTube 비디오에 대한 링크가 필요합니다. 링크가 동적입니다. YouTube에서 simplexml을 사용하여 피드를 가져오고 있습니다. 클릭하면 컬러 박스가 표시되지만 비어 있습니다. 예 : URL : http://revmiller.com/videos-youtube-custom.php을 확인하십시오. 링크 코드는 다음과 같습니다. <a class='youtube' href="<?php echo $watch; ?>" title="<?php echo $media->group->title; ?>"><img src="<?php echo $thumbnail;?>" /></a>Dynamic Youtube Colorbox로 열기 링크

미리 읽어 주셔서 감사합니다.

+0

아이디어가 있으십니까? 솔루션을 찾아 낼 수는 없지만 문제를 정확히 찾아 낼 수 없었습니다. 어쩌면 나는 이것을 위해 다른 방식으로 유튜브 링크를 부름해야 할까? – JCM

+0

문제를 발견했을 수도 있습니다. "youtube.com/watch"URL을 가져 오는 중이지만 "youtube.com/embed"URL을 가져와야한다고 생각합니다. 누구라도이를 수행하는 방법에 대한 통찰력이 있다면 공유하십시오. 그렇지 않으면 계속 검색 할 것입니다. – JCM

답변

1

내가 포함 URL을 호출해야한다는 것이 맞습니다. 이렇게하려면 비디오 ID를 추출하여 각 항목의 포함 URL에 연결해야했습니다. 누구나 비슷한 작업을 수행하려는 경우 다음은 작동하는 코드입니다 (위의 링크는 더 이상 작동하지 않으며 테스트 용이었습니다).

<?php 

//Credits: Mixed some code from Vikram Vaswani (http://www.ibm.com/developerworks/xml/library/x-youtubeapi/), Matt (http://stackoverflow.com/questions/7221485/get-youtube-video-id-from-url-w-php), & Tim (http://groups.google.com/group/youtube-api-gdata/browse_thread/thread/fc1efc399f9cc4c/d1a48cf5d4389cf8?lnk=gst&q=colorbox#d1a48cf5d4389cf8), and then messed around with it to fit my needs. 

function getYoutubeId($ytURL) 
    { 
     $urlData = parse_url($ytURL); 
     //echo '<br>'.$urlData["host"].'<br>'; 
     if($urlData["host"] == 'www.youtube.com') // Check for valid youtube url 
     { 
      $query_str = parse_url($ytURL , PHP_URL_QUERY); 
      parse_str($query_str, $args); 
      $ytvID = $args['v']; 

      return $ytvID; 
     } 
     else 
     { 
      //echo 'This is not a valid youtube video url. Please, give a valid url...'; 
      return 0; 
     } 

    } 

// set feed URL 
$feedURL = 'your feed url here'; 

// read feed into SimpleXML object 
$sxml = simplexml_load_file($feedURL); 
?> 
    <h1 class="page-title">Video Gallery</h1> 
<?php 
// iterate over entries in feed 
foreach ($sxml->entry as $entry) { 
    // get nodes in media: namespace for media information 
    $media = $entry->children('http://search.yahoo.com/mrss/'); 

    // get video player URL 
    $attrs = $media->group->player->attributes(); 
    $watch = $attrs['url']; 

    // get video thumbnail 
    $attrs = $media->group->thumbnail[0]->attributes(); 
    $thumbnail = $attrs['url']; 

    //get video id 
    $videoid = $yt->videoid[0]; 

    // get <yt:duration> node for video length 
    $yt = $media->children('http://gdata.youtube.com/schemas/2007'); 
    $attrs = $yt->duration->attributes(); 
    $length = $attrs['seconds']; 

    // get <yt:stats> node for viewer statistics 
    $yt = $entry->children('http://gdata.youtube.com/schemas/2007'); 
    $attrs = $yt->statistics->attributes(); 
    $viewCount = $attrs['viewCount']; 

    // get <gd:rating> node for video ratings 
    $gd = $entry->children('http://schemas.google.com/g/2005'); 
    if ($gd->rating) { 
    $attrs = $gd->rating->attributes(); 
    $rating = $attrs['average']; 
    } else { 
    $rating = 0; 
    } 

    $videoId = getYoutubeId($watch); 
    ?> 
    <div class="item"> 
    <h1 class="video-title"> 
     <a class="youtube" href="http://www.youtube.com/embed/<?php echo $videoId ?>?rel=0&amp;wmode=transparent"><?php echo $media->group->title; ?></a> 
    </h1> 
    <p> 
     <span class="video-thumbnail"> 
     <a class="youtube" href="http://www.youtube.com/embed/<?php echo $videoId ?>?rel=0&amp;wmode=transparent" title="<?php echo $media->group->title; ?>"><img src="<?php echo $thumbnail;?>" /></a> 
     <br/>click to view 
     </span> 
    </p> 
    </div>  
<?php 
} 
?>