#StandWithUkraine

AucT Blog

How to retreive YouTube video information using YouTube API in PHP

How to retreive YouTube video information using YouTube API in PHP

Parse single video

<?php
$json = file_get_contents('http://gdata.youtube.com/feeds/api/videos/eQWG8BVeryU?v=2&alt=json');
$obj = json_decode($json);
$item['title']=$obj->entry->title->{'$t'};
$item['author']=$obj->entry->author[0]->name->{'$t'};
$item['description']=$obj->entry->content->{'$t'};
$item['id']=substr($obj->entry->id->{'$t'},strpos($obj->entry->id->{'$t'},'video:')+6);
$item['url']=$obj->entry->link[0]->href;
$item['published']=$obj->entry->published->{'$t'};
$item['duration']=$obj->entry->{'media$group'}->{'yt$duration'}->seconds;
$item['img']=$obj->entry->{'media$group'}->{'media$thumbnail'}[2]->url;
echo '<b>Title:</b> '.$item['title'];
echo '<br><b>Author:</b> '.$item['author'];
echo '<br><b>Pusblished:</b> '.$item['published'];
echo '<br><b>Duration:</b> '.$item['duration'].' seconds<br>';
echo $item['description'];
echo '<br><a href="'.$item['url'].'" target="_blank"><img src="'.$item['img'].'"></a><br><br><br>';
//Display the video
echo '<iframe width="560" height="315" src="//www.youtube.com/embed/'.$item['id'].'" frameborder="0" allowfullscreen></iframe><br>';
echo '<br><br>';
?>

Parse channel

<?php
$channels = array('ChrisBrownVEVO', 'BritneySpearsVEVO');
foreach ($channels as $channel) {
$json = getSslPage('https://gdata.youtube.com/feeds/api/users/'.$channel.'/uploads?alt=json');
$obj = json_decode($json);
for ($i=0; $i<25; $i++) {
$item['title']=$obj->feed->entry[$i]->title->{'$t'};
$item['author']=$obj->feed->entry[$i]->author[0]->name->{'$t'};
$item['description']=$obj->feed->entry[$i]->content->{'$t'};
$item['url']='http://www.youtube.com/watch?v='.substr($obj->feed->entry[$i]->id->{'$t'},42);
$item['published']=$obj->feed->entry[$i]->published->{'$t'};
$item['duration']=$obj->feed->entry[$i]->{'media$group'}->{'yt$duration'}->seconds;
$item['img']=$obj->feed->entry[$i]->{'media$group'}->{'media$thumbnail'}[0]->url;
echo '<b>Title:</b> '.$item['title'];
echo '<br><b>Author:</b> '.$item['author'];
echo '<br><b>Pusblished:</b> '.$item['published'];
echo '<br><b>Duration:</b> '.$item['duration'].' seconds<br>';
echo $item['description'];
echo '<br><a href="'.$item['url'].'" target="_blank"><img src="'.$item['img'].'"></a><br><br><br>';
}
}
function getSslPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
?>

Share on Social Networks:

Twitter Facebook
comments powered by Disqus