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

前端 未结 2 1204
孤独总比滥情好
孤独总比滥情好 2020-12-08 11:05

I\'ve been trying to get the view count on videos that I query through the following method:

      function search() {
        var request = gapi.client.yout         


        
2条回答
  •  佛祖请我去吃肉
    2020-12-08 11:42

    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

提交回复
热议问题