Welcome Guest, Not a member yet? Register   Sign In
Get Youtube Video/Channel as well as Both in combination Data Feed With the use Of Simple Pie
#1

[eluser]Unknown[/eluser]
<?php
// Get Youtube Data Feed With use Of Simple Pie in PHP
// Munir Sunni ( [email protected] )
include('simplepie/simplepie.class.php');

$urlList = array();

$urlList =
(
//List Of Youtube Videos
'http://www.youtube.com/watch?v=D5xxUkLhZe4',
'http://www.youtube.com/watch?v=VBURsTurlH8',
'http://www.youtube.com/watch?v=_qNx7ggPTLc',

//List Of Youtube Channels

'http://www.youtube.com/user/jalil7',
'http://www.youtube.com/user/maddyvora'
);

foreach ($urlList as $videoUrl)
{
// Check that if this Video/channel is actully coming from Youtube or not
if (stristr(strtolower($videoUrl), 'youtube.com'))
{
// Explode all the value through '/' for getting Video/channel ID and stored into array
$linkHref = explode('/', $videoUrl);

// Get the Five Charactor of third value of array to defing that if it is video or channel
$linkHrefText = substr($linkHref[3], 0, 5);

if ($linkHrefText == 'watch')
{
// Get the Video Id of Youtube Video
$urlVars = GetYoutubeUrlVars($videoUrl);
if ($urlVars['v'] != '')
{
$videoAndChannelList[] = 'http://gdata.youtube.com/feeds/videos?vq=' . $urlVars['v'];
}
}
else
{
// Get the Channel Id of Youtube Channel
$channelName = ($linkHref[3] != 'user') ? $linkHref[3] : $linkHref[4];
$videoAndChannelList[] = 'http://gdata.youtube.com/feeds/api/users/'. $channelName .'/uploads';
}
}
}


$feed = new SimplePie();
$feed->set_cache_location('SimplePie/cache');

//Put the List of channel/videos from which you get Videos
$feed->set_feed_url($videoAndChannelList);
$feed->init();
$feed->handle_content_type();

$items = $feed->get_items();
$items = array_slice($items, 0, 50, true);

foreach ($items as $item)
{
$date = $item->get_date('U');
$link = $item->get_permalink();
$enclosure = $item->get_enclosure();
if ($enclosure)
{
$title = $enclosure->get_title();
$duration = $enclosure->duration;
$thumb = $enclosure->get_thumbnail();
$description = $enclosure->get_description();
}

if (empty($link))
$link = $item->feed->feed_url;

$object = null;
$object->Title = $title;
$object->Date = $date;
$object->Link = $link;
$object->Description = $description;
$object->Duration = $duration;
$object->Thumb = $thumb;

$externalVideos[] = $object;
}
echo '<pre>';
print_r($externalVideos);

function GetYoutubeUrlVars($url)
{
$vars = array();
$urlstr = substr($url, strpos($url, '?') + 1, strlen($url));

$urlargs = explode('&',$urlstr);
foreach ($urlargs as $arg)
{
$exploded = explode('=', $arg);

if (count($exploded) > 1)
$vars[$exploded[0]] = $exploded[1];
}

return $vars;
}
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB