How to Get all Videos of a channel using youtube API

Authors: CodeToday | PHP Code | Views: 625 | Posted: 04 AM: 09/14/2017

If you have a youtube channel and you want to get all of that video on your website then this is the solution for you.

Next article: How to get Video from Youtube and display directly on your website, I will guide you to get the video by Youtube Channel (How to get youtue api key like previous post).

First you must have is youtube channel ID. Ex: https://www.youtube.com/channel/UCGIH12itZniDIIRsLvJmoKQ then youtube channel ID: UCGIH12itZniDIIRsLvJmoKQ

The URL to get the video from youtube channel is: https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCGIH12itZniDIIRsLvJmoKQ&maxResults=48&key=[Your API Key]

The way to get data through json is the same as before

PHP Full Source Code

<?
$url  = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCGIH12itZniDIIRsLvJmoKQ&maxResults=48&key=[Your API Key]';
$data = curlGet($url);
$array_json =  json_decode($data);

echo '<div class = "row">';
foreach($array_json -> items as $item) { 

	$videoId      = $item -> id -> videoId;
	$datePosted   = $item -> snippet -> publishedAt;
	$title        = $item -> snippet -> title;
	$channelId    = $item -> snippet -> channelId;
	$channelTitle = $item -> snippet -> channelTitle;
	echo '<div class="col-md-4 col-xs-12">';
	echo '<p>'.$title .'</p>';
	echo '<p><img src="http://i3.ytimg.com/vi/'.$videoId.'/mqdefault.jpg"</p>';
	echo '</div>';
	
}
echo '</div>';

function curlGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	curl_setopt($ch, CURLOPT_HTTPGET, 1 );
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_URL, $url );
	curl_setopt($ch, CURLOPT_REFERER, $ref );
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
?>
The image below is a result of fetch all videos on a Channel using Youtube API

How to Get all Videos of a channel using youtube API


If you have any questions, please leave a message below

Tyree cruz

Kansas sign up

Posted: 09/15/2017