How to load/list YouTube channel Videos?
Here we will see how we can load YouTube channel video in our website by using JSON.
Now we will start this from scratch
STEP 1 (Get API Key):
We will get ‘API Key’ from Google Developer Console, you can find steps here : https://developers.google.com/youtube/v3/getting-started
STEP 2 (Get Channel ID):
Go to your YouTube Channel and copy your channel URL e.g. https://www.youtube.com/channel/XXXThs2cnzpDD-dT
and your channel id is ‘XXXThs2cnzpDD-dT‘.
Now see the complete code below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php $api_key = 'XXXXXXXXC9eEmCnF4qXXXXX'; $chanel_id = 'XXXnzpXXXXXXXXXXXc-dT'; // Youtube Channel ID, copy from url of channel 'https://www.youtube.com/channel/XXXnzpXXXXXXXXXXXc-dT' $maxResults = '20'; $url = 'https://www.googleapis.com/youtube/v3/search?key='.$api_key.'&channelId='.$chanel_id.'&part=snippet,id&order=date&maxResults=20'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_TIMEOUT, 60); $result = curl_exec($ch); if(curl_errno($ch) !== 0) { error_log('cURL error when connecting to ' . $url . ': ' . curl_error($ch)); } curl_close($ch); $videoLists = json_decode($result); //echo'<pre>'; //print_r($videoLists); $videoList = $videoLists->items; ?> <?php foreach($videoList as $videoListVal){ ?> <div style="width:20%; float:left;"> <?php $thumb_url = $videoListVal->snippet->thumbnails->medium->url; //mediaum,high,default $title = $videoListVal->snippet->title; $description = $videoListVal->snippet->description; $videoId = $videoListVal->id->videoId; $publishTime = $videoListVal->snippet->publishTime; ?> <iframe width="100%" height="auto" src="https://www.youtube.com/embed/<?php echo $videoId; ?>?si=ICtFe87ElbGLgdvm" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> </div> <?php } //echo'<pre>'; //print_r($videoList); ?> |