· 5 years ago · Nov 26, 2020, 05:10 PM
1<?php
2 //Define a constant so that the API KEY can be used globally across the application
3 define("YOUTUBE_DATA_API_KEY", 'AIzaSyAzDLhLFqywTZyE0iz353M0xzyC1XWlStw');
4 //Get a single video Views by ID
5 function youtube_video_statistics($video_id) {
6 $json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $video_id . "&key=". YOUTUBE_DATA_API_KEY );
7 $jsonData = json_decode($json);
8 $views = $jsonData->items[0]->statistics->viewCount;
9 return $views;
10 }
11 function youtube_playlist_all_views($playlistId) {
12 $apiKey = 'AIzaSyAzDLhLFqywTZyE0iz353M0xzyC1XWlStw';
13 $maxResults = 8;
14 $string = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.$playlistId.'&maxResults='.$maxResults.'&key='.$apiKey.''));
15 foreach($string->items as $item) {
16 $total += youtube_video_statistics($item->snippet->resourceId->videoId);
17 }
18 return $total;
19 }
20?>