Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cp6
Created August 7, 2019 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cp6/57cd47897d00409697a2115465a5dca7 to your computer and use it in GitHub Desktop.
Save cp6/57cd47897d00409697a2115465a5dca7 to your computer and use it in GitHub Desktop.
Get all videos YouTube api
<?php
$yt_channel_id = '';//YouTube Channel id
$yt_api_key = '';//YouTube API key
function list_videos_w_page($chid, $api_key, $page_token = '')
{
global $yt_channel_id;
global $yt_api_key;
if ($page_token == '') {
$data = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=" . $chid . "&maxResults=50&order=date&type=video&key=$yt_api_key"), true);
} else {
$data = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=" . $chid . "&maxResults=50&order=date&type=video&key=$yt_api_key&pageToken=$page_token"), true);
}
foreach ($data['items'] as $val) {
$vid = $val['id']['videoId'];//returns video id
$title = str_replace("'", "-", $val['snippet']['title']);//returns video title
$desc = str_replace("'", "-", $val['snippet']['description']);//returns video description
$uploaded = date('Y-m-d H:i:s', strtotime($val['snippet']['publishedAt']));//returns video description
echo "[$vid]($uploaded) $title<br>";
}
if (isset($data['nextPageToken'])) {//if exists means there is another page so do:
echo list_videos_w_page($yt_channel_id, $yt_api_key, $data['nextPageToken']);
}
}
echo list_videos_w_page($yt_channel_id, $yt_api_key);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment