PHP - Display Videos from YouTube Channel

In this tutorial we will create a Display Videos from YouTube Channel using PHP. This code can be use for storing of list of data. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Get Videos from YouTube Channel</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-primary" id="search"><span class="glyphicon glyphicon-search"></span> Get Video</button>
  18.                 <div id="result"></div>
  19.         </div>
  20. <script src="js/jquery-3.2.1.min.js"></script>
  21. <script type="text/javascript">
  22.         $(document).ready(function(){
  23.                 $('#search').on('click', function(){
  24.                         $('#result').load('get_video.php');
  25.                 });
  26.         });
  27. </script>
  28. </body>
  29. </html>

Getting the Youtube API key

In order to get your api key, first you need to login your google accounts. Then visit this site https://console.developers.google.com/apis/dashboard After that create a project here. tut1 Then, after the project is created go to Credentials and generate your key here. tut2

Setting the Keys

This section hold the api key for the youtube videos. To do this copy and write the codes inside the text editor then save it as youtube_key.php
  1. <?php
  2.         $api_key    = "YOUR API KEY';
  3.         $video_id  = 'UC28FIDM6e_MVxFq4AQXcUxQ';
  4. ?>

Creating the Main Function

This code contains the main function of the application. This code will get the list of the youtube videos channel. To do this just kindly copy and write these block of codes inside the text editor, then save it as get_video.php.
  1. <?php
  2.  
  3. require_once 'youtube_key.php';
  4.  
  5.  
  6. $videos = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$video_id.'&maxResults=50&key='.$api_key.''));
  7.  
  8.  
  9. foreach($videos->items as $item){
  10.    
  11.     if(ISSET($item->id->videoId)){
  12. ?>
  13.                 <div style="float:left; width:250px; margin:20px;">
  14.                         <center><h5><?php echo $item->snippet->title ?></h5></center>
  15.                         <br />
  16.                         <center><iframe width="230" height="180" src="https://www.youtube.com/embed/<?php echo $item->id->videoId ?>" frameborder="0" allowfullscreen></iframe></center>
  17.                 </div>
  18. <?php  
  19.        
  20.     }
  21. }
  22.  
  23. ?>
There you have it we successfully created Display Videos from YouTube Channel using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment