How to get Video from Youtube and display directly on your website

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

Youtube is a popular video sharing site of google. Here I instruct you to get video from youtube and display on your website via Youtube API.

The first thing you need is the Youtube API Key

Go to https://console.cloud.google.com -> Enabled APIs and services -> Library -> YouTube Data API and Active it



Then you go to Credentials to create an API key for it


Now save this API key to use for getting video data through the youtube API

API paths will be used through the tutorial from youtube: https://developers.google.com/youtube/v3/docs/search/list

Below is a link to get youtube videos with category = 1 (Film & Animation). 

https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=48&type=video&videoCategoryId=1&key=[Your API Key]

The list of groups is divided as below:

1:Film & Animation, 2:Autos & Vehicles, 10:Music, 15:Pets & Animals

17:Sports, 19:Travel & Events, 20:Gaming, 22:People & Blogs

23:Comedy, 24:Entertainment, 25:News & Politics, 26:Howto & Style

27:Education, 28:Science & Technology, 29:Nonprofits & Activism, 30:Movies

34:Comedy, 35:Documentary, 36:Drama, 43:Shows, 44:Trailers

The results are returned as json shown below:


PHP Full Source Code

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

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;
}

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;
}
?>

Here we will have $videoId, $datePosted, $title, $channelId, $channelTitle, you can present your website on your own

Link of image you can use: http://i3.ytimg.com/vi/[$videoId]/mqdefault.jpg

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>';

The result is as below when you complete the above steps:

How to get Video from Youtube and display directly on your website

Also see: How to Get all Videos of a channel using youtube API


If you have any questions, please leave a message below