· 5 years ago · Nov 26, 2020, 05:24 PM
1<?php
2//Define a constant so that the API KEY can be used globally across the application
3define("YOUTUBE_DATA_API_KEY", 'AIzaSyAzDLhLFqywTZyE0iz353M0xzyC1XWlStw');
4
5function requestStatistics($url) {
6 // Open curl connection and set headers
7 $connection = curl_init();
8 curl_setopt($connection, CURLOPT_URL, $url);
9 curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
10
11 // Execute request and close
12 $response = curl_exec($connection);
13 curl_close($connection);
14
15 // Extract the data structure response
16 return json_decode($response);
17
18}
19
20// Get a single video Views by ID
21function youtube_video_statistics($video_id) {
22 $url = "https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $video_id . "&key=" . YOUTUBE_DATA_API_KEY;
23
24 $jsonData = requestStatistics($url);
25 $views = $jsonData->items[0]->statistics->viewCount;
26 return $views;
27}
28
29// Retrieve all views
30function youtube_playlist_all_views($playlistId) {
31
32 $maxResults = 8;
33 $url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=' . $playlistId . '&maxResults=' . $maxResults . '&key=' . YOUTUBE_DATA_API_KEY;
34
35 // Extract the data structure response
36 $jsonData = requestStatistics($url);
37
38 foreach($string->items as $item) {
39 $total += youtube_video_statistics($item->snippet->resourceId->videoId);
40 }
41 return $total;
42}
43?>
44
45<!---------------- YOUTUBE TABLE -------------------------------------------------------------------------->
46<div class="table-container youtube-container">
47<div class="table-title">The Savior 8 clips on YouTube - Number of views per language</div>
48<table id="Table-Youtube" class="table-list table-list-2">
49<thead>
50<tr>
51<td width="50%"><b>Language</b></td>
52<td width="50%"><b>Views</b></td>
53</tr>
54</thead>
55<tbody>
56
57<?php
58
59$countries = array (
60 0 => 'Classical Arabic (Original Language)',
61 1 => 'Amharic',
62 2 => 'Bahasa (Indonesia)',
63 3 => 'Bangla (Bengali)',
64 4 => 'Bulgarian',
65 5 => 'Chittagonian',
66 6 => 'Dari',
67 7 => 'Egyptian (Arabic)',
68 8 => 'English',
69 9 => 'Farsi',
70 10 => 'French',
71 11 => 'German',
72 12 => 'Hausa',
73 13 => 'Hebrew',
74 14 => 'Hindi',
75 15 => 'Italian subs (on Arabic)',
76 16 => 'Italian subs (on English dub)',
77 17 => 'Javanese',
78 18 => 'Korean',
79 19 => 'Kuwaiti (Arabic)',
80 20 => 'Malay',
81 21 => 'Mandarin',
82 22 => 'Nepali',
83 23 => 'Pashto',
84 24 => 'Portuguese',
85 25 => 'Punjabi',
86 26 => 'Central Asian Russian',
87 27 => 'Russian',
88 28 => 'Somali',
89 29 => 'Spanish (Latin America)',
90 30 => 'Swahili',
91 31 => 'Tagalog',
92 32 => 'Tamil',
93 33 => 'Tashelhit',
94 34 => 'Thai',
95 35 => 'Turkish',
96 36 => 'Urdu Hindi',
97 37 => 'Urdu Pakistan',
98);
99
100$allviews = array (
101 0 => '2869',
102 1 => '1313140',
103 2 => '682471',
104 3 => '538068',
105 4 => '5597',
106 5 => '15928',
107 6 => '2507',
108 7 => '2758',
109 8 => '15681',
110 9 => '3643',
111 10 => '27754',
112 11 => '15238',
113 12 => '1384',
114 13 => '5528',
115 14 => '1494695',
116 15 => '3800',
117 16 => '11048',
118 17 => '1249',
119 18 => '36586',
120 19 => '3511',
121 20 => '58507',
122 21 => '13605',
123 22 => '320',
124 23 => '452',
125 24 => '12430',
126 25 => '998',
127 26 => '7326',
128 27 => '20649',
129 28 => '217',
130 29 => '8337',
131 30 => '32981',
132 31 => '270980',
133 32 => '7069',
134 33 => '2053',
135 34 => '3992123',
136 35 => '155289',
137 36 => '35381',
138 37 => '1132',
139);
140
141$playlist_ids = array (
142 0 => 'PLjb2NNhAinSkHKsl-RrFeZ37uSx5N_iX4',
143 1 => 'PLjb2NNhAinSl9CJ86MzYhtHyybVEuUIXi',
144 2 => 'PLjb2NNhAinSmvyPR7xe4uYKX14UiQMwZE',
145 3 => 'PLjb2NNhAinSkTJs-HoOMMygJtVstOZYxq',
146 4 => 'PLjb2NNhAinSmNC-HZLMT_BbRexCm4HFMj',
147 5 => 'PLjb2NNhAinSlawqle3k-zwP6Sv3m15ofR',
148 6 => 'PLjb2NNhAinSlnR8ZOmur_W10711gawsIk',
149 7 => 'PLjb2NNhAinSmnI-Bs6OxgCSaUTUKstq6s',
150 8 => 'PLjb2NNhAinSni3l53ME2LP-8GlAcNsPgG',
151 9 => 'PLjb2NNhAinSk7KU2QqRbXXGTNEgke-Fl2',
152 10 => 'PLjb2NNhAinSkwO0qC-TPY0E9oEJPgNAkc',
153 11 => 'PLjb2NNhAinSldXYlmyj4zwopUx_fmjE3x',
154 12 => 'PLjb2NNhAinSk2x3btK_fMLPyaV4fU_5bz',
155 13 => 'PLjb2NNhAinSn4k6AsEakNyMV6VkP3T9_4',
156 14 => 'PLjb2NNhAinSkYlaQHP54O6HW0to4GEEcB',
157 15 => 'PLjb2NNhAinSmjnY50C6fsh88Xs_7yWCEm',
158 16 => 'PLjb2NNhAinSm9Tewmks0CZxagcYruQavz',
159 17 => 'PLjb2NNhAinSmPeH7TGC_y7vHXYNiQTreS',
160 18 => 'PLjb2NNhAinSmnj3RrGHCIURUAlP1GYGOp',
161 19 => 'PLjb2NNhAinSkSu-Ecd8hFOPQbFZ7dCt9x',
162 20 => 'PLjb2NNhAinSkSJRA_zzWhvXX4R60SaKFq',
163 21 => 'PLjb2NNhAinSlW0icp-i_Q-jp8wCM_Hlur',
164 22 => 'PLjb2NNhAinSlaUZRLHr97_Kyz2pRHrA4k',
165 23 => 'PLjb2NNhAinSl8Q5aAUO315tPBWhzRFEYk',
166 24 => 'PLjb2NNhAinSnkAW6Lt26PbJx3xALXTsN4',
167 25 => 'PLjb2NNhAinSnP4J0H2emoJMo08EXq_Kof',
168 26 => 'PLjb2NNhAinSnHcDG9YY7nD8-iUneWmBb_',
169 27 => 'PLjb2NNhAinSnghhdhhrl9Excv4pk6zhr5',
170 28 => 'PLjb2NNhAinSmKIT1_w4Ue3KWIw8iqduu9',
171 29 => 'PLjb2NNhAinSmOroQ1dwnGvvLeP3wLj0aC',
172 30 => 'PLjb2NNhAinSlwLFRnu1QjZDENAg65Z_9w',
173 31 => 'PLjb2NNhAinSlwBaOnUhKft1aSuY7_Osr3',
174 32 => 'PLjb2NNhAinSltjzmI-hEF63pzxZwOTi4t',
175 33 => 'PLjb2NNhAinSkz0hIM_ozN73CqnK-XFOQG',
176 34 => 'PLjb2NNhAinSkitiyHZDbrQzb_MUab8Dgm',
177 35 => 'PLjb2NNhAinSmLLArLwLzcbBYvLasAX_69',
178 36 => 'PLjb2NNhAinSmgNu4hICJGTloeO1-j4fsN',
179 37 => 'PLjb2NNhAinSkC0NfNpVAXCjShsbRGKvto',
180);
181
182$youtube_table = array_combine($countries, $playlist_ids);
183
184$i = 0;
185$total_views = 0;
186
187foreach ($youtube_table as $country => $playlist_ids) {
188 echo '<tr>';
189 echo ' <td><a href="https://www.youtube.com/playlist?list='. $playlist_ids .'" target="_blank" rel="noopener noreferrer">'. $country .'</a></td>';
190 echo ' <td>'. youtube_playlist_all_views($playlist_ids) .'</td>';
191 echo '</tr>';
192}
193
194?>
195
196<tr>
197<td><strong>TOTAL Views</strong></td>
198<td><strong><?php echo $total_views; ?></strong></td>
199</tr>
200</tbody>
201</table>
202</div>