Last active
March 18, 2018 02:14
-
-
Save josepanguera/7d68148a3697a7f08c648904ec68bb0c to your computer and use it in GitHub Desktop.
PHP: Get the uploaded videos of a channel using the YouTube API v3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$baseUrl = 'https://www.googleapis.com/youtube/v3/'; | |
// https://developers.google.com/youtube/v3/getting-started | |
$apiKey = 'API_KEY'; | |
// If you don't know the channel ID see below | |
$channelId = 'CHANNEL_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']; | |
} | |
return $videos; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$username = 'username'; | |
$params = ['forUsername'=> $username, 'part'=> 'contentDetails', 'key'=> $apiKey]; | |
$url = $baseUrl . 'channels?' . http_build_query($params); | |
$json = json_decode(file_get_contents($url), true); | |
$channelId = $json['items'][0]['id']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so what needs to be added to actually echo out these video ID's?
i want to put into my database