2

I am tying to have a link to a youtube video open with colorbox. The link is dynamic - I am pulling the feed from youtube using simplexml.The colorbox shows up on click, but it is blank. Check the URL here: http://revmiller.com/videos-youtube-custom.php for an example. Here is the link's code: <a class='youtube' href="<?php echo $watch; ?>" title="<?php echo $media->group->title; ?>"><img src="<?php echo $thumbnail;?>" /></a>

Thank you very much in advance for any ideas!

JCM
  • 83
  • 1
  • 3
  • 10
  • Any ideas? I still have not been able to pinpoint the problem, let alone figure out a solution. Maybe I should be calling the youtube link a different way for this to work? – JCM Feb 09 '12 at 11:49
  • I think I may have found the problem. I am pulling the "youtube.com/watch" URL, but I think I should be pulling the "youtube.com/embed" URL. If anyone has any insight on how to do this, please share. Otherwise, I'll keep searching. – JCM Feb 13 '12 at 07:44

1 Answers1

1

I was correct that I should have been calling the embed URL. To do this, I had to extract the video id and plug it into the embed URL for each entry. If anyone is seeking to do something similar, here is the working code (the above link will no longer work - it was for testing only):

<?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
}
?>
JCM
  • 83
  • 1
  • 3
  • 10