Create Youtube Video Gallery in PHP

Gallery | PHP | Xml | Youtube | Youtube Api

Youtube Video Gallery in PHP


Youtube Video Gallery: Youtube is a most popular website for video sharing created by google. Youtube provides a way for users to create their own channel with google account. Users can subscribe to channel they like. In this tutorial, we are going to display our youtube channel’s videos on our own website with thumbnails. So that users on our website can quickly watch video. Though you can do similar code for any programming language you like. But i will use PHP (an open source scripting language) for this tutorial.

Pre-requisites for Youtube Video Gallery in PHP

  • A Youtube Channel with few videos uploaded
  • Familiarity with PHP code
  • Basic idea about HTML & CSS
  • Knowledge of what is XML
  • Manipulate XML in PHP code

Youtube Video Gallery in PHP: Let’s Do it

Step 1: Create a PHP file with following code. Or you can use existing one of your website to add following code.


In above code, We used youtube api to get data from youtube. simplexml_load_file is a PHP function allows us to fetch xml data from URL, load in memory and returns an xml object. we stored xml object in $xml variable. In URL after users we have to put long string that youtube generates for channel instead of YOUR_CHANNEL_NAME. For this example, I have used CodeRiddles’ youtube channel.

Step 2: add following highlighted code in PHP file created in step 1. So your code for php file will look similar to below.

Next we have created a empty array and loop through xml entries, retrieve details that we need from xml object and store details in $return array. then we simply output $return using print_r function. Output will be similar to what we have below.

Youtube Video Gallery in PHP

Step 3: Lets add some CSS to our PHP file for better visual result. I have written style in php file but it is recommended that you write css in separate file.

Step 4: Add following code in same PHP file after style code we added  in step 3.

As you can see I have added few div tags for better result. I sed foreach loop on $result array and output details of video in proper HTML tags. Also note that use of id in link’s href attribute, which redirects user on exact youtube’s video page. Now its time to see what we have in browser window. Navigate to your php file and you will get result similar to what we get below. Our own youtube video gallery.

YOUTUBE Video Gallery

Remember we created full page youtube video gallery. But you can also create small youtube video gallery for placing in sidebar by reducing width of img tag as required.  Have fun with youtube gallery on your own website.

Youtube Video Gallery in PHP Complete Code

If you like Youtube Video Gallery in PHP website, please like our Facebook Fan page for regular updates. Thanks to everyone.

Bookmark Link using: bookmark at folkd