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


You can use the new YouTube Data API v3

if you retrieve the video, the statistics part contains the viewCount:

from the doc:

https://developers.google.com/youtube/v3/docs/videos#resource

statistics.viewCount / The number of times the video has been viewed.

You can retrieve this info in the client side, or in the server side using some of the client libraries:

https://developers.google.com/youtube/v3/libraries

And you can test the API call from the doc:

https://developers.google.com/youtube/v3/docs/videos/list

Sample:

Request:

GET https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Q5mHPo2yDG8&key={YOUR_API_KEY}

Authorization:  Bearer ya29.AHES6ZSCT9BmIXJmjHlRlKMmVCU22UQzBPRuxzD7Zg_09hsG
X-JavaScript-User-Agent:  Google APIs Explorer

Response:

200 OK

- Show headers -

{
 "kind": "youtube#videoListResponse",
 "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/dZ8K81pnD1mOCFyHQkjZNynHpYo\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {

   "id": "Q5mHPo2yDG8",
   "kind": "youtube#video",
   "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/4NA7C24hM5mprqQ3sBwI5Lo9vZE\"",
   "statistics": {
    "viewCount": "36575966",
    "likeCount": "127569",
    "dislikeCount": "5715",
    "favoriteCount": "0",
    "commentCount": "20317"
   }
  }
 ]
}

Version 2 of the API has been deprecated since March 2014, which some of these other answers are using.

Here is a very simple code snippet to get the views count from a video, using JQuery in the YouTube API v3.

You will need to create an API key via Google Developer Console first.

<script>
  $.getJSON('https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Qq7mpb-hCBY&key={{YOUR-KEY}}', function(data) {
    alert("viewCount: " + data.items[0].statistics.viewCount);
  });
</script>