Skip to content

Instantly share code, notes, and snippets.

/youtube_data.php
Created Aug 4, 2017

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<title>Get Youtube Channel Video and Details.- HackerRahul</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form method="GET" action="youtube.php" >
<h1>Enter Channel Id here -</h1>
<input name="id" type="text" />
<input type="submit" name="submit" value="Submit" /><br>
<p>For Ex - <b>UCThD9sEw37dndHMyFCXPFVQ</b></p>
<hr>
</form>
</center>
</body>
</html>
<?php
if(isset($_GET['id'])){
$baseUrl = 'https://www.googleapis.com/youtube/v3/';
// https://developers.google.com/youtube/v3/getting-started
$apiKey = 'YOUR_API_KEY'; // REPLACE with your API KEY here.
// get the channel id from url through get request.
$channelId = $_GET['id'];
$params = [
'id'=> $channelId,
'part'=> 'contentDetails',
'key'=> $apiKey
];
$url = $baseUrl . 'channels?' . http_build_query($params);
$json = json_decode(file_get_contents($url), true);
$playlist = $json['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
$params = [
'part'=> 'snippet',
'playlistId' => $playlist,
'maxResults'=> '50',
'key'=> $apiKey
];
$url = $baseUrl . 'playlistItems?' . http_build_query($params);
$json = json_decode(file_get_contents($url), true);
$videos = [];
foreach($json['items'] as $video)
$videos[] = $video['snippet']['resourceId']['videoId'];
while(isset($json['nextPageToken'])){
$nextUrl = $url . '&pageToken=' . $json['nextPageToken'];
$json = json_decode(file_get_contents($nextUrl), true);
foreach($json['items'] as $video)
$videos[] = $video['snippet']['resourceId']['videoId'];
}
foreach($videos as $key=> $id){
$url = "https://www.googleapis.com/youtube/v3/videos?id=".$id."&key=".$apiKey."&part=snippet,contentDetails,statistics";
$json = json_decode(file_get_contents($url), true);
echo "Thumbnail - <br><img src='".$json['items'][0]['snippet']['thumbnails']['medium']['url']."'></a><br>";
echo "Title - ".$json['items'][0]['snippet']['title']."<br>";
echo "Description - <br>".$json['items'][0]['snippet']['description']."<br>";
echo "Id - ".$json['items'][0]['id']."<br>";
echo "Views - ".$json['items'][0]['statistics']['viewCount']."<br>";
echo "Likes - ".$json['items'][0]['statistics']['likeCount']."<br>";
echo "Dislikes - ".$json['items'][0]['statistics']['dislikeCount']."<br>";
echo "<br><br><hr>";
}
}else{
echo "No channel Id Specified";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.