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)
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)
gdata 不再可用
你可以使用以下内容代替
https://www.googleapis.com/youtube/v3/videos?part=snippet&id=(Video_ID)&key=(API_Key)
| 归档时间: |
|
| 查看次数: |
31191 次 |
| 最近记录: |