Making a JavaScript script that gets out info from Youtube
so i continue from this question YouTube: get youtube title+ image+description like facebook` i got this answer:
If you were given the video link http://www.youtube.com/watch?v=NWHfY_lvKIQ, you 开发者_如何学Gocould get all the info about the video by using this link, http://gdata.youtube.com/feeds/api/videos/NWHfY_lvKIQ. The data returned contains all the information about the video, including title, description, and a thumbnail.
Now how can i get out the info about the video, with a script? I mean, how to do a script that displays description,thumbnail and title from http://gdata.youtube.com/feeds/api/videos/NWHfY_lvKIQ , do i need to download this first and then take out the information by opening in notepad, but thats not how i want it, i want it to show / echo through a script, the description+thumbnail+title, if you understand me correctly, just like what you do when you enter a link in facebook "what are you doing". Now i only want to show you for this video: http://gdata.youtube.com/feeds/api/videos/NWHfY_lvKIQ, just so i can learn to do the rest
thank you
If you can use jquery, this is what I use to get the title, description, and url. If you can't use jquery, you can use some other ajax call, or the callback recommended by digitalFresh
$.get('http://gdata.youtube.com/feeds/api/videos/NWHfY_lvKIQ?v=2&alt=json', function(data) {
var title = data.entry.title.$t;
var description = data.entry.media$group.media$description.$t;
var thumbnail = data.entry.media$group.media$thumbnail[0].url; // URL of the image
// Use these variables somewhere
});
You need a callback JSON. If you only want to get a video by the code (ie. NWHfY_lvKIQ) Use this:
http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1&q=NWHfY_lvKIQ&callback=cbk
Important parts:
q=... - the query(the video code)
callback=... - the function that you want to call after the string is loaded.
Then put it inside a script tag
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1&q=NWHfY_lvKIQ&callback=cbk"></script>
When this loads, it calls the function cbk
and transfers its data.
精彩评论