Posted on Leave a comment

YouTube Trends PHP Script API V3 Country Wise Trending Video List PHP jQuery


To incorporate YouTube’s trending videos on your webpage, you’ll need to utilize the Google API V3 YouTube Data API library. This video tutorial offers a comprehensive walkthrough on creating an API and developing a PHP-based webpage on YouTube PHP Scripts that seamlessly integrates YouTube Trends. You’ll gain valuable insights into leveraging PHP and jQuery to dynamically generate a curated list of country-specific trending videos by leveraging the power of the YouTube trends PHP script and API v3. This tutorial will equip you with the necessary knowledge and tools to enhance your website with engaging YouTube content.

In this particular case, I have employed the YouTube Data API V3 library to retrieve a compilation of YouTube’s trending videos, showcasing the content that is presently capturing viewers’ attention. Subsequently, I utilized this gathered video list to establish a visually appealing video gallery on my website.

Step 1: Creating an Empty HTML Table to Display Results

I have created an HTML table that showcases the most popular YouTube videos at the moment, taking into account their recent viewership.

<html>
<head><title>YoutTube Trends</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<table>
<thead id="result">
</thead>
</table>

Step 2: Implement a jQuery function to retrieve videos and display them in an HTML table

In the given jQuery script, an API request has been made to access the YouTube Data API V3. The purpose is to retrieve the latest trending and popular videos that are currently captivating viewers in the YouTube trends. The API call is targeted at fetching a collection of YouTube videos associated with a specific keyword, utilizing a PHP script.

Step 3: Set up the YouTube Data API v3 on the Google Console

To create and configure the YouTube Data API v3, follow these steps:

  1. Visit the Google Cloud Console (console.cloud.google.com).
  2. Create a new project or select an existing project.
  3. Navigate to the API Library page.
  4. Search for “YouTube Data API v3” and select it.
  5. Click on the “Enable” button to activate the API for your project.
  6. Go to the Credentials page and create an API key.
  7. Restrict the API key if necessary to ensure secure usage.
  8. Save the generated API key for future use.

By following these steps, you will successfully create and set up the YouTube Data API v3 API key on the Google Console, enabling your application to interact with the YouTube platform. If you encounter any difficulties, you can refer to my YouTube tutorial on my channel: insert YouTube tutorial link here.

Step 4: Add jQuery script

Add the following jQuery script to your website’s footer section. Remember to replace “API_KEY” with your own Google API key that you created.

<script>
$(document).ready(function(){
API_KEY="YOUR_API_KEY";
var url=`https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&videoCategoryId=20&regionCode=US&maxResults=50&key=${API_KEY}`;
$.ajax({
method:"GET",
url:url,
success:function(data){
console.log(data);
displayVideo(data);
}
})
function displayVideo(data){
var videoData="";
data.items.forEach(item=>{
videoData=`<tr>
<td>
<a href="video_play.php?vidid=${item.id.videoId}" target="_blank">${item.snippet.title}</a>
</td>
<td>
<a target="_blank" href=""></a>
</td>
<td>
<img src="${item.snippet.thumbnails.high.url}" width="200" height="200">
</td>
</tr>
`;
$("#result").append(videoData);
})
}
})
</script>

By adding this jQuery script to your website’s footer section and replacing “API_KEY” with your actual Google API key, the script will retrieve YouTube videos related to the “trending” keyword. You can customize the keyword and adjust the number of results as needed. Handle the response to extract the video title, description, and thumbnail, and then display them in your gallery.

Step 5: Play a YouTube video using its unique video ID

Please create a file named video_play.php and insert the following code into it. This code will retrieve the YouTube video ID from the function mentioned in the previous steps and display a YouTube iframe to play the video.

if(isset($_GET['vidid'])){
$vidid=$_GET['vidid'];
echo '<iframe width="552px" height="310px" src="https://www.youtube.com/embed/'.$vidid.'?" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>';
}

Make sure you have the get value “vidid” implemented or available in the same file or from a separate file that you include in video_play.php. The $_GET[‘vidid’] should get the YouTube video ID from URL.

This code will generate an iframe that embeds the YouTube video using the provided video ID. Adjust the width and height attributes of the iframe according to your desired dimensions.

If you’re looking to buy the premium PHP script named “YouTube Trends PHP Script Unlimited Google API Access” in order to seamlessly incorporate auto-generated YouTube trending videos on a country-specific basis into your website’s subdomain or folder, you can make your purchase at https://andamantech.com/product/youtube-trends-php-script-unlimited-google-api-access/. The provided webpage offers comprehensive information regarding the script’s features and specifications.

To gain a comprehensive understanding of the capabilities offered by the premium source code of the YouTube Trends PHP script, you can explore the live server demo available at https://yt-trends.andamantech.com/?geo=IN. This website provides a hands-on demonstration of the script’s functionalities, allowing you to thoroughly evaluate its features and determine whether it aligns with your requirements before making a purchase decision.

I hope you had a great experience utilizing the YouTube Trends PHP Script API V3 to generate trending video lists with play video. Feel free to download and utilize it according to your needs.