使用Colorbox打开动态Youtube链接

时间:2012-02-06 09:51:12

标签: youtube simplexml colorbox

我想要一个用colorbox打开的YouTube视频的链接。链接是动态的 - 我使用simplexml从youtube中提取Feed。单击时会显示颜色框,但它是空白的。请查看此处的网址: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>

非常感谢您提出任何想法!

1 个答案:

答案 0 :(得分:1)

我说我应该调用嵌入网址是对的。为此,我必须提取视频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
}
?>