获取嵌入的YouTube视频的标题和说明

Sye*_*rib 14 youtube youtube-api

在我正在开发的网站上,我嵌入了来自YouTube的视频,并希望获得视频标题及其说明.

我如何获得这些信息?

Pra*_*bal 13

Youtube API V2.0已被弃用.它显示标题"youtube.com/devicesupport"的错误值.pLease打开API V3.0

你可以参考下面的PHP代码,并根据你的需要在js或jquery中修改你的代码.

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;
}
}
Run Code Online (Sandbox Code Playgroud)

更新:

获取标题的Jquery代码 -

 $.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');
     }   
    });
Run Code Online (Sandbox Code Playgroud)

  • 我有一个解决方案,我们可以获得没有API密钥的标题...使用以下链接http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=videoid&format= json ...但是应该知道videoid的事情. (3认同)

Wil*_*nni 10

要获取DESCRIPTION元素,您需要访问视频信息的gdata版本,并且可以在路径上使用alt = json返回json.在这种情况下,oHg5SJYRHA0是视频ID,位于您在YouTube上使用的视频的网址末尾,例如 www.youtube.com/watch?v=oHg5SJYRHA0

http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json&prettyprint=true

(漂亮的图纸格式化,使其易于阅读,你不需要它为你正在做的事情)

您可以获取JSON,将其添加到变量中并使用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;
})();
Run Code Online (Sandbox Code Playgroud)

然后使用对象表示法访问它:

alert("Title: " + json.entry.title.$t +"\nDescription:\n " + json.entry.media$group.media$description.$t + "\n");
Run Code Online (Sandbox Code Playgroud)

  • 这已经过时,已经过时,不再有效 (7认同)

Mat*_*aćo 7

你可以用oembed做到这一点.例:

http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&format=json

  • 似乎无法得到描述. (10认同)