Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

How to display YouTube view count on a website

$
0
0

I want to display view count of a youtube video in a website. The below code is written for it. But it does not print the view count, only shows the youtube video.

Here is my code,

`<!DOCTYPE html><html><head><title>How to put PHP in HTML - Simple Example</title><script src="https://code.jquery.com/jquery-3.6.0.min.js"></script><script type="text/javascript">        function videoViews() {            var rex = /[a-zA-Z0-9\-\_]{11}/,            videoUrl = $('input').val() === '' ? alert('Enter a valid Url'):$('input').val(),            videoId = videoUrl.match(rex),            jsonUrl = 'http://gdata.youtube.com/feeds/api/videos/'+ videoId +'?v=2&alt=json',            embedUrl = '//www.youtube.com/embed/'+ videoId,            embedCode = '<iframe width="350" height="197" src="'+ embedUrl +'" frameborder="0" allowfullscreen></iframe>';            //Get Views from JSON            $.getJSON(jsonUrl, function (videoData) {                var videoJson = JSON.stringify(videoData),                vidJson = JSON.parse(videoJson),                views = vidJson.entry.yt$statistics.viewCount;                $('.views').text(views);            });            //Embed Video            $('.videoembed').html(embedCode);        }</script></head><body><input type="url" placeholder="Enter Youtube video URL"> <a href="#" onClick="videoViews()">GO</a><div class="videoembed"></div><div class="views"></div><?php    if(isset($_GET['url'])) {        $video_ID = substr($_GET['url'], -11);        $JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id={$video_ID}&key=AIzaSyAGquK-QPs8yaddEqhL1AEmYbrJKT6xs04");        $JSON_Data = json_decode($JSON);        $views = $JSON_Data->items[0]->statistics->viewCount;        echo "<script>$('.views').text($views);</script>";    }    ?></body></html>`

I want to display Number of Views of a given video in my website.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>