如何使用 YouTube API 获取视频观看次数?

2022-01-19 00:00:00 youtube-api javascript

这个问题很简单.如何使用 YouTube API 获取视频观看次数?

The question is very simple. How to get number of video views with YouTube API?

任务很简单,但我想经常对大量视频使用该查询.有什么方法可以调用他们的 Youtube API 并获取它?(类似于 facebook http://api.facebook.com/restserver.php?method=links.getStats&urls=developers.facebook.com)

The task is simple but I would like to use that query on large number of videos very often. Is there any way to call their Youtube API and get it? (something like facebook http://api.facebook.com/restserver.php?method=links.getStats&urls=developers.facebook.com)

推荐答案

我认为,最简单的方法是获取 JSON 格式的视频信息.如果您想使用 JavaScript,请尝试 jQuery.getJSON()... 但我更喜欢 PHP:

I think, the easiest way, is to get video info in JSON format. If you want to use JavaScript, try jQuery.getJSON()... But I prefer PHP:

<?php
$video_ID = 'your-video-ID';
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>

参考:Youtube API - 检索有关单个视频的信息

相关文章