Skip to content

Instantly share code, notes, and snippets.

@josepanguera
Last active March 18, 2018 02:14
Show Gist options
  • Save josepanguera/7d68148a3697a7f08c648904ec68bb0c to your computer and use it in GitHub Desktop.
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
<?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;
<?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'];
@dynamitemedia
Copy link

so what needs to be added to actually echo out these video ID's?

i want to put into my database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment