-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
117 lines (106 loc) · 4.08 KB
/
index.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
if(isset($_POST['submit']))
{
$API_Key='AIzaSyANmcujJeVX0yZ-jxIVq3634uhFSzH_Nac';
$API_url='https://www.googleapis.com/youtube/v3/';
$channelid=$_POST['channelid'];
/*****
To fetch Youtube data :
Channel Logo
Channel Name, Description,
Subscribers count, Views count & Videos count
*****/
$params=array
(
'part' => 'snippet,contentDetails,statistics',
'id' => $channelid,
'key' => $API_Key
);
$url=$API_url.'channels?'.http_build_query($params);
$channelDetails=json_decode(file_get_contents($url),true);
/*****
Fetching data into particular variables
*****/
$logo=$channelDetails['items'][0]['snippet']['thumbnails']['medium']['url'];
$title=$channelDetails['items'][0]['snippet']['title'];
$desc=$channelDetails['items'][0]['snippet']['description'];
$subs=$channelDetails['items'][0]['statistics']['subscriberCount'];
$views=$channelDetails['items'][0]['statistics']['viewCount'];
$videos=$channelDetails['items'][0]['statistics']['videoCount'];
/*****
To fetch videos
*****/
$params=array
(
'part' => 'snippet,contentDetails',
'playlistId' => $channelDetails['items'][0]['contentDetails']['relatedPlaylists']['uploads'],
'key' => $API_Key,
'maxResults' => 50
);
$url=$API_url.'playlistItems?'.http_build_query($params);
$playlistDetails=json_decode(file_get_contents($url),true);
$data=$playlistDetails['items'];
}
else
{
$data=[];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fetch Data | Youtube Data API v3</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="icon" href="assets/logo.png">
<style>
.mb-5{
color:rgb(255,0,0);
}
</style>
</head>
<body>
<section class="container py-5 text-center">
<div class="row mb-5">
<div class="col-6 mx-auto">
<h1 class="mb-5">Fetch Youtube Data by ID</h1>
<form action="" method="POST">
<div class="form-group mb-5">
<input type="text" class="form-control" placeholder="Enter channel ID" name="channelid">
</div>
<input class="btn btn-danger" value="GET DATA" type="submit" name="submit">
</form>
</div>
</div>
<table class="table table-bordered">
<tr>
<th>Channel Logo</th>
<th>Channel Name</th>
<th>Description</th>
<th>Subscribers count</th>
<th>Views Count</th>
<th>Videos Count</th>
</tr>
<tr>
<td><img src="<?php echo isset($logo)?$logo:''?>" alt=""></td>
<td><?php echo isset($title)?$title:''?></td>
<td><?php echo isset($desc)?$desc:''?></td>
<td><?php echo isset($subs)?$subs:''?></td>
<td><?php echo isset($views)?$views:''?></td>
<td><?php echo isset($videos)?$videos:''?></td>
</tr>
</table>
</section>
<section class="container">
<div class="row">
<?php foreach($data as $video){
$id=$video['contentDetails']['videoId'];?>
<div class="col-3 mb-5">
<iframe width="100%" height="200" src="https://www.youtube.com/embed/<?php echo $id?>" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<?php } ?>
</div>
</section>
</body>
</html>