On a site I'm developing I embed videos from YouTube and want to get the video title and its description.
How do I get that information?
To see YouTube analytics on embedded videos, click the Reach Viewers tab at the top of YouTube Studio. From here, scroll down and expand the Traffic source: External box.
Copy the full URL that you want to link to, and then paste it into the YouTube video description box. Make sure to hit "Save" afterwards. 7. YouTube should automatically make the text into a URL on the video's description.
Youtube API V2.0 has been deprecated. It shows some wrong value for title "youtube.com/devicesupport" . pLease switch on to API V3.0
YOu can refer the following PHP code and modify yours in js or jquery as per your needs..
function youtube_title($id) {
$id = 'YOUTUBE_ID';
// returns a single line of JSON that contains the video title. Not a giant request.
$videoTitle = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$id."&key=YOUR_API_KEY&fields=items(id,snippet(title),statistics)&part=snippet,statistics");
// despite @ suppress, it will be false if it fails
if ($videoTitle) {
$json = json_decode($videoTitle, true);
return $json['items'][0]['snippet']['title'];
} else {
return false;
}
}
update:
Jquery code to get the title-
$.getJSON('https://www.googleapis.com/youtube/v3/videos?id={VIDEOID}&key={YOUR API KEY}&part=snippet&callback=?',function(data){
if (typeof(data.items[0]) != "undefined") {
console.log('video exists ' + data.items[0].snippet.title);
} else {
console.log('video not exists');
}
});
You can do it with oembed. Example:
http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&format=json
To get the DESCRIPTION element, you need to access the gdata version of the video's info, and you can return json using alt=json on the path. In this case, oHg5SJYRHA0 is the video ID, found at the end of the url of the video you're working with on YouTube, e.g.
www.youtube.com/watch?v=oHg5SJYRHA0
http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json&prettyprint=true
(the prettyprint is formatting to make that easy to read, you don't need it for what you're doing)
You can grab the JSON, add it into a variable and access it using jQuery:
var youTubeURL = 'http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json';
var json = (function() {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': youTubeURL,
'dataType': "json",
'success': function(data) {
json = data;
}
});
return json;
})();
Then access it using object notation:
alert("Title: " + json.entry.title.$t +"\nDescription:\n " + json.entry.media$group.media$description.$t + "\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With