How to get number of video views with YouTube API?

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;
?>

Ref: Youtube API – Retrieving information about a single video

Leave a Comment