Retrieving individual videos view count - Youtube API V3.0 - JavaScript

强颜欢笑 提交于 2019-11-28 06:27:22

The link you gave https://developers.google.com/youtube/v3/docs/videos/list#try-it is working for me. To get duration and viewCount: Fill in for part: contentDetails,statistics and for id: a comma-separated-list of video-id's like: TruIq5IxuiU,-VoFbH8jTzE,RPNDXrAvAMg,gmQmYc9-zcg

This will create a request as:

GET https://www.googleapis.com/youtube/v3/videos?part=contentDetails,statistics&id=TruIq5IxuiU,-VoFbH8jTzE,RPNDXrAvAMg,gmQmYc9-zcg&key={YOUR_API_KEY}

Agree with the answer provided by @Als.

But I found a code snippet which might be more convenient for some of you:

function youtube_view_count_shortcode($params)
{
 $videoID = $params['id']; // view id here 
 $json = file_get_contents("https://www.googleapis.com/youtube/v3/videos? 
 part=statistics&id=" . $videoID . "&key=xxxxxxxxxxxxxxxxxxxxxxxx");
 $jsonData = json_decode($json);
 $views = $jsonData->items[0]->statistics->viewCount;
 return number_format($views);
}

Replace the key value with the google api key for youtube data API and the video id with the youtube video id and Voila you get the total number of views for the youtube video.

Source: https://www.codementor.io/rajharajesuwari/how-to-get-youtube-views-count-aftojpxhj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!