Posted on Leave a comment

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

If you want to list YouTube Trending Videos on your web page, you need to access the Google API V3 YouTube Data API library. In this video tutorial, you can watch hot to create API and also create php YouTube Trends web page with simple ways YouTube trends php script API v3 country wise trending video list use php jQuery.

In this example, I have created YouTube Trending videos what people look watching now this video list shows on my website. YouTube via the YouTube Data API V3 library and get the list of videos. Then, I formed a video gallery with the resultant video list.

HTML Blank Table for show results

I have created a HTML table to show trending YouTube videos by recent by people watch right now.

<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/bootstrap@4.3.1/dist/css/bootstrap.min.css">
</head>
<table>
<thead id="result">
</thead>
</table>

Create jQuery function get video to show on html table

In the following jQuery script, I have requested the YouTube Data API V3 access to get the popular videos by the just now trending and what video people love to watch right now. I am sending the request throuth API call to request a YouTube video list related to the keyword.

After fetching the data, the jQuery response will return the video data. The jQuery function response will be parsed to get the video title and the description in the gallery. The video thumbnail is embedded.

<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>

YouTube Play Video through video id

Create video_play.php file and paste this code.

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>';
}

I Hope you liked YouTube Trends PHP Script API V3 Country Wise Trending Video List which I create feel Free to Download and use it, Don’t forget to Subscribe my YouTube Channel TECHboto.
I hope you enjoyed this tutorial, and maybe got some inspiration for your next project.

Leave a Reply

Your email address will not be published. Required fields are marked *