-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_videos.php
More file actions
54 lines (45 loc) · 1.34 KB
/
get_videos.php
File metadata and controls
54 lines (45 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
header('Content-Type: application/json');
// Channels
$channels = [
'eyaduddin' => 'UCHrjyKuoG7nQSRE-amT8I8w',
'canadiangamer' => 'UC4cXh5_kRY7xcZIgglTz9sg'
];
$channel = $_GET['channel'] ?? 'canadiangamer';
$channelId = $channels[$channel] ?? $channels['canadiangamer'];
// Load API key manually from .env
$envFile = __DIR__ . '/.env';
$apiKey = '';
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (str_starts_with($line, 'YOUTUBE_API=')) {
$apiKey = trim(substr($line, strlen('YOUTUBE_API=')));
break;
}
}
}
// If no API key, return empty
if (!$apiKey) {
echo json_encode([]);
exit;
}
// Fetch videos
$url = "https://www.googleapis.com/youtube/v3/search?key={$apiKey}&channelId={$channelId}&part=snippet,id&order=date&maxResults=6&type=video";
$response = file_get_contents($url);
if (!$response) {
echo json_encode([]);
exit;
}
$data = json_decode($response, true);
$videos = [];
if (!empty($data['items'])) {
foreach ($data['items'] as $item) {
$videos[] = [
'title' => $item['snippet']['title'],
'thumbnail' => $item['snippet']['thumbnails']['medium']['url'] ?? '',
'videoId' => $item['id']['videoId']
];
}
}
echo json_encode($videos);