Integrated Tech Solutions

How to Fetch & Embed Youtube Channel Videos Using Data API[PHP]

By - Admin October 9, 2022

Get Your API Key

Time needed: 15 minutes

  1. Go to Google Developers Console & Create a New Project

    Visit https://console.cloud.google.com/ & Create a New Project or Select an existing project.

  2. Enable Youtube data API for this Project

    After the project is created, select it and search for Youtube data API in search box.

    Now Enable the Youtube data API as shown below –

  3. Create API key by clicking Create Credentials link




    Copy the API Generated in this Step

  4. Get the Youtube Channel Id

    Go to your Youtube channel & copy the channel id from URL as shown below –

  5. Assemble the data into PHP code

    Now create your PHP file where you want to fetch the youtube videos via API & put the following code –

PHP Code to fetch data from Youtube API –

<?php
/* Fetch Data from Youtube API */
$channelDATA = file_get_contents('https://www.googleapis.com/youtube/v3/search?channelId=<CHANNEL_ID>&key=<API_KEY>&part=snippet&maxResults=10');

/*Convert the Data into PHP Array */
$YtVideosArr = json_decode($channelDATA)->items;

/*Loop through the items in array */
$video_index = 1;
foreach($YtVideosArr as $singleVideo){
$videoId = $singleVideo->id->videoId;
$thumbnail_url = $singleVideo->snippet->thumbnails->high->url;
$video_title = $singleVideo->snippet->title;
$video_description = $singleVideo->snippet->description;
$published_time = $singleVideo->snippet->publishTime;
echo '<iframe width="280" height="150" src="https://www.youtube.com/embed/'.$videoId.'" frameborder="0" allowfullscreen></iframe>';
echo $video_title;
echo $video_description;
echo $published_time;

}

?>

Popular Blogs for Web Developers

Keep Reading

👋 Hi, Find this Helpful? There is More

You Asked,
We made it!

fix japanese keyword hack

Step by Step Video Tutorials on trending topics in software development

Yes I am Curious »

How to create email templates in odoo[XML Code]

Creating email templates in Odoo using XML code is a powerful way to customize email communications directly from your Odoo...

Know More »

5 Tips to Pick the Most Engaging Live Chat for Your Website

In today’s fast-paced digital world, providing excellent customer service is a key differentiator for businesses. One of the most effective...

Know More »