RSS

Tag Archives: youtube API

Get YouTube Video Comments Using PHP

// video id
$videoId='';
// url where feed is located
$url="http://gdata.youtube.com/feeds/api/videos/{$videoId}/comments";
// get the feed
$comments=simplexml_load_file($url);
// parse the feed using a loop
foreach($comments->entry as $comment)
{
echo '<fieldset>'.$comment->content.'</fieldset>';
}
Advertisements
 
Leave a comment

Posted by on October 29, 2012 in PHP

 

Tags:

Retrieve Video Details From Youtube

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<div>
<form id="youtubeDataQuery" action="#">
<div>
<b>Enter YouTube Video ID or URL in the text box below</b><br/>
<input id="youtubeVideoId" type="text" style="width: 70%;" value="" maxlength="200"/>
<input type="submit" style="width: 28%;" value="Fetch Video Information"/>
</div>
<br/>
<div id="youtubeDataOutput">Video information will appear here.</div>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
function youtubeDataCallback(data) {
var s = '';
s += '<img src="' + data.entry.media$group.media$thumbnail[0].url + '" width="' + data.entry.media$group.media$thumbnail[0].width + '" height="' + data.entry.media$group.media$thumbnail[0].height + '" alt="' + data.entry.media$group.media$thumbnail[0].yt$name + '" align="right"/>';
s += '<b>Title:</b> ' + data.entry.title.$t + '<br/>';
s += '<b>Author:</b> ' + data.entry.author[0].name.$t + '<br/>';
s += '<b>Published:</b> ' + new Date(data.entry.published.$t).toLocaleDateString() + '<br/>';
s += '<b>Duration:</b> ' + Math.floor(data.entry.media$group.yt$duration.seconds / 60) + ':' + (data.entry.media$group.yt$duration.seconds % 60) + ' (' + data.entry.media$group.yt$duration.seconds + ' seconds)<br/>';
if (data.entry.gd$rating) {
s += '<b>Rating:</b> ' + data.entry.gd$rating.average.toFixed(1) + ' out of ' + data.entry.gd$rating.max + ' (' + data.entry.gd$rating.numRaters + ' ratings)<br/>';
}
s += '<b>Statistics:</b> ' + data.entry.yt$statistics.favoriteCount + ' favorite(s); ' + data.entry.yt$statistics.viewCount + ' view(s)<br/>';
s += '<br/>' + data.entry.media$group.media$description.$t.replace(/\n/g, '<br/>') + '<br/>';
s += '<br/><a href="' + data.entry.media$group.media$player.url + '" target="_blank">Watch on YouTube</a>';
$('#youtubeDataOutput').html(s);
}
$(document).ready(function() {
$('#youtubeDataQuery').submit(function(e) {
e.preventDefault();
var videoid = $('#youtubeVideoId').val();
var m;
if (m = videoid.match(/^http:\/\/www\.youtube\.com\/.*[?&]v=([^&]+)/i) || videoid.match(/^http:\/\/youtu\.be\/([^?]+)/i)) {
videoid = m[1];
}
if (!videoid.match(/^[a-z0-9_-]{11}$/i)) {
alert('Unable to parse Video ID/URL.');
return;
}
$.getScript('http://gdata.youtube.com/feeds/api/videos/' + encodeURIComponent(videoid) + '?v=2&alt=json-in-script&callback=youtubeDataCallback');
});
});
</script>
</div>
</body>
</html>
 
Leave a comment

Posted by on October 23, 2012 in HTML, JavaScript, jQuery

 

Tags: