Getting the YouTube Variable
Posted: Thu Aug 05, 2010 10:43 am
I am creating an RSS feed from a YouTube channel to display on my site; but instead of linking them to the YouTube page, I want them to change the URL of my web address.
For example, the youtube video http://www.youtube.com/watch?v=[b]KnEwbit0P94[/b] would be displayed on the site as http://domain.com/?v=[b]KnEwbit0P94[/b] and the variable would change the video in the embedded player accordingly.
Here is the code I currently have:
so the URL to each video is something like http://www.youtube.com/watch?v=[b]KnEwbit0P94[/b]&feature=youtube_gdata
Is there a way I can grab just the variable from each video, so I would direct the links to domain.com/v=KnEwbit0P94
Not sure if this is possible, but I figure it is worth a shot asking. Thanks!
For example, the youtube video http://www.youtube.com/watch?v=[b]KnEwbit0P94[/b] would be displayed on the site as http://domain.com/?v=[b]KnEwbit0P94[/b] and the variable would change the video in the embedded player accordingly.
Here is the code I currently have:
Code: Select all
<?php
//
// ScarySoftware RSS parser
// Copyright (c) 2006 Scary Software
// ( Generates HTML from a RSS feeds )
//
// Licensed Under the Gnu Public License
//
// Here are the feeds - you can add to them or change them
$RSSFEEDS = array(
0 => "http://www.youtube.com/rss/user/higgensvideo/videos.rss",
);
//
// Makes a pretty HTML page bit from the title,
// description and link
//
function FormatRow($title, $description, $link) {
return <<<HTML
<!-- RSS FEED ENTRY -->
<a href="$link">$title</a>
<hr size=1>
<!-- END OF RSS FEED ENTRY -->
HTML;
}
// we'll buffer the output
ob_start();
// Now we make sure that we have a feed selected to work with
if (!isset($feedid)) $feedid = 0;
$rss_url = $RSSFEEDS[$feedid];
// Now we read the feed
$rss_feed = file_get_contents($rss_url);
// Now we replace a few things that may cause problems later
$rss_feed = str_replace("<![CDATA[", "", $rss_feed);
$rss_feed = str_replace("]]>", "", $rss_feed);
$rss_feed = str_replace("\n", "", $rss_feed);
// If there is an image node remove it, we aren't going to use
// it anyway and it often contains a <title> and <link>
// that we don't want to match on later.
$rss_feed = preg_replace('#<image>(.*?)</image>#', '', $rss_feed, 1 );
// Now we get the nodes that we're interested in
preg_match_all('#<title>(.*?)</title>#', $rss_feed, $title, PREG_SET_ORDER);
preg_match_all('#<link>(.*?)</link>#', $rss_feed, $link, PREG_SET_ORDER);
preg_match_all('#<description>(.*?)</description>#', $rss_feed, $description, PREG_SET_ORDER);
//
// Now that the RSS/XML is parsed.. Lets Make HTML !
//
// If there is not at least one title, then the feed was empty
// it happens sometimes, so lets be prepared and do something
// reasonable
if(count($title) <= 1)
{
echo "No news at present, please check back later.<br><br>";
}
else
{
// OK Here we go, this is the fun part
// Well do up the top 3 entries from the feed
for ($counter = 1; $counter <= 3; $counter++ )
{
// We do a reality check to make sure there is something we can show
if(!empty($title[$counter][1]))
{
// Then we'll make a good faith effort to make the title
// valid HTML
$title[$counter][1] = str_replace("&", "&", $title[$counter][1]);
$title[$counter][1] = str_replace("'", "'", $title[$counter][1]);
// The description often has encoded HTML entities in it, and
// we probably don't want these, so we'll decode them
$description[$counter][1] = html_entity_decode( $description[$counter][1]);
// Now we make a pretty page bit from the data we retrieved from
// the RSS feed. Remember the function FormatRow from the
// beginning of the program ? Here we put it to use.
$row = FormatRow($title[$counter][1],$description[$counter][1],$link[$counter][1]);
// And now we'll output the new page bit!
echo $row;
}
}
}
// All Finished!
ob_end_flush(); // Send the output to the browser
?>
Is there a way I can grab just the variable from each video, so I would direct the links to domain.com/v=KnEwbit0P94
Not sure if this is possible, but I figure it is worth a shot asking. Thanks!