Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of video views with YouTube API?

The question is very simple. How to get number of video views with YouTube API?

enter image description here

The task is simple but I would like to use that query on large number of videos very often. Is there any way to call their Youtube API and get it? (something like facebook http://api.facebook.com/restserver.php?method=links.getStats&urls=developers.facebook.com)

like image 382
Ante Avatar asked Jul 25 '10 21:07

Ante


People also ask

How can I count my YouTube video?

YouTube wants to make sure that video views are coming from real people. That's why a YouTube view is only counted when the following two criteria apply: A user intentionally initiates the watching of a video. The user watches it on the platform for at least 30 seconds.

What is YouTube Analytics API?

The YouTube Reporting and YouTube Analytics APIs let you retrieve YouTube Analytics data to automate complex reporting tasks, build custom dashboards, and much more. The Reporting API supports applications that can retrieve and store bulk reports, then provide tools to filter, sort, and mine the data.


2 Answers

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

like image 104
Victor Avatar answered Sep 29 '22 13:09

Victor


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"    }   }  ] } 
like image 23
Matias Molinas Avatar answered Sep 29 '22 11:09

Matias Molinas