YouTube is the world’s most popular video-sharing platform that hosts many 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 different niches. So you can watch any stuff of your choice on the platform, and it also allows you to create/upload your videos to the platform. According to one survey, the count of global visitors who spend time watching videos on the platform is around 6 billion hours monthly. In addition, talking about its convenience in development, the data YouTube API provides a fast way to access YouTube data, and you’ll see how to connect it with PHP in this article.
Connecting YouTube API Key to PHP
While nowadays, YouTube has also become an excellent channel for digital marketing. As companies do target the platform for advertising their products and services.
Additionally, the YouTube API connect has a wide selection of features and functionality that make it accessible to developers and valuable to businesses. For example, it allows you to recover data on a large scale without accessing individual videos and channels.
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 must create an account on google and get the API key. As that will use in generating API requests and then giving access to the platform.
In this article, I will demonstrate how to get an API key and fetch YouTube videos using Youtube Data API.
Some Benefits of Using the YouTube API Connect:
- You can leverage existing content: With the YouTube API, you can reserve time and aids creating content. Instead, you can leverage published content to increase user engagement with your site.
- YouTube is one of the leading social media platforms, the second-largest search engine, and the second-most popular social platform on the Internet.
- Display content from YouTube can increase your retention time.
- It helps users stay on the page longer while watching the content you’ve curated for them. Increasing dwell time shows search engines that your content is valuable and improves your rankings.
Also, using YouTube API connect can help you attract more website visitors. Using APIs is also beneficial from a conversion and revenue perspective. Potential customers interested in your content and staying on your site may make a purchase.
For many marketers, the YouTube API is easy to use, but only a few people on YouTube can access this valuable tool and its data. To prevent potential abuse, YouTube grants users a unique API key to connect their website or application to the platform. Each API key is up to a project. Therefore, you should create two different API keys by using the API on two websites.
Prerequisites
For the purpose of this tutorial, I assume that you have a PHP application installed on a web server. My setup is as follows:
- PHP 5.4
- Google Account
So I have used a managed cloud server to host my PHP application, allowing 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
After successfully installing the application on a managed Cloud server, the next step is to sign up for a Google account and get an API key from the Google console.
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 connect link.
Now, to access YouTube data, enable the YouTube Data API v3 to access desired data.
In the left navigation menu, click on the Credentials link. Then click Create Credentials button and select the API key to YouTube API to connect.
A dialogue box will appear on the screen containing your API Key, which you will use in the YouTube Data API v3 request.
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>’;
}
}
In addition to the ID and title of the video in the video list, you can show other preferred information that suits your needs when connect YouTube API.
Conclusion
To summarize, this article shows you how to create and connect YouTube API to fetch videos from the platform. From generating the 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.