YouTube is the world’s most popular video sharing platform that hosts tons of videos, movies, shows, documentaries and other related stuff. Developed in 2005 and owned by Google, YouTube holds the repute of the world’s largest database of videos comprising of different niches.  You can watch any stuff of your choice on the platform and it also allows you to create/upload your own videos to the platform. According to one survey, the count of global visitors which spend time watching videos on the platform is around 6 billion hours monthly. Talking about its convenience in development, the data YouTube API provides a fast way to access YouTube data and you’ll see in this article how to connect it with PHP.

Connecting YouTube API Key to PHP

YouTube API PHP Connection Guide
YouTube API PHP Connection Guide

 

While nowadays, YouTube has also become a great channel for digital marketing. As companies do target the platform for advertising their products and services.

Talking about its convenience in development, the data API of YouTube provides a fast way to access YouTube data. It includes videos, playlists, channels and other relevant data. So, to start using YouTube Data API, first, you have to create an account on google and get API key. As that will use in generating API request and then giving access to the platform.

In this article, I will demonstrate how you can get API key and fetch YouTube videos via using Youtube Data API.

Prerequisites

For the purpose of this tutorial, I assume that you have a PHP application installed on a web server. My setup is:

  • PHP 7.x
  • Google Account

I have used a managed cloud server to host my PHP application. As it allows me to concentrate fully on my job without diverting my attention to any physical server issue. Further, it provides optimized hosting for PHP web apps with an advanced stack of tools and features.

Get Youtube API Key

Get Youtube API Key
Get Youtube API Key

 

After successfully installing the application on a managed Cloud server, the next step is to sign up for a Google account and then get API key from the Google console.  

Get Youtube API Key
Get Youtube API Key

 

In the Google API Console, click the Select a project link and create the project.

On the left navigation menu, click on the Library link. Then under the YouTube APIs section, click on the YouTube Data API link.

YouTube Data API link
YouTube Data API link

 

Now to access YouTube data, enable the YouTube Data API v3 to access desired data.  

YouTube Data API v3
YouTube Data API v3

 

From the left navigation menu click on the Credentials link. Then after click on the Create credentials button and select API key.

Select API key
Select API key

 

A dialogue box will appear on the screen containing your API Key, which you will use in the YouTube Data API v3 request.

API Key Created
API Key Created

 

Get Youtube Videos using YouTube Data API v3

YouTube Data API returns JSON data which includes intrinsic information about the video. It includes the title, description, thumbnails etc.  

$API_key    = ‘Insert_Your_API_Key’;

$channelID  = ‘Insert_Channel_ID’;

$maxResults = 10;

$videoList = json_decode(file_get_contents(‘https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=’.$channelID.’&maxResults=’.$maxResults.’&key=’.$API_key.”));

Youtube Video List using PHP

Loop through the $videoList->items to list the videos from a YouTube channel.

foreach($videoList->items as $item){

       if(isset($item->id->videoId)){

       echo ‘<div class=”youtube-video”>

               <iframe width=”280″ height=”150″ src=”https://www.youtube.com/embed/’.$item->id->videoId.'” frameborder=”0″ allowfullscreen></iframe>

               <h2>’. $item->snippet->title .'</h2>

           </div>’;

   }

}

Besides Video ID and title in the Video list, you can show other preferred information that lies with your requirement.

Video ID
Video ID

 

Conclusion

So summarizing up, this article shows you the brief way how to create and use YouTube API to fetch videos from the platform. From generating API key from the Google console to its usage in your PHP application, the article covers detailed steps explaining the overall process. You can further see the whole execution process in this demo. If you have further questions regarding this article, you can ask them in the comments section below.

Author Bio: Sajjad is a Community Manager at Cloudways – A Managed Cloud Hosting Platform. He loves helping out store owners, merchants and marketers in establishing their businesses and startups.




Share This Post