Get Videos From YouTube Channel Using Data API V3 And PHP Example

PHP 8 , PHP

Nicesnippets

1828

06-07-2021


Get Videos From YouTube Channel Using Data API V3 And PHP Example

Hi guys,

Today i will explained How To Get YouTube Channel Videos Using Data API V3 in PHP. This example is so easy to use in php. This example to i am perform to get my youtube channel video get to my page.

This example to i am already created to the my youtube api key so you are first create to the your youtube api key and find to your youtube channel id in youtube.

I am add to the Google Developer Console link this link through create a youtube api key Google Developer Console

So let's start to the example.

index.php


<?php

$API_key = 'AIzaSyD181_csxai1WVHPAfAsc1V_-Bqaoy-5Bk';

$channelID = 'UC6LtBEBs1POQVdEP5lBjXdQ';

$maxResult = 10;

$apiError = 'Video not Found';

try{

$apiData = @file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResult.'&key='.$API_key.'');

if($apiData){

$videoList = json_decode($apiData);

}else{

throw new Exception('Invalid API key or channel ID.');

}

}catch(Exception $e){

$apiError = $e->getMessage();

}

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Get Videos from YouTube Channel using Data API v3 and PHP</title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

</head>

<body>

<div class="container">

<h2 class="text-center mt-3">Get Videos from YouTube Channel using Data API v3 and PHP</h2>

<div class="row">

<div class="col-md-12">

<?php

if(!empty($videoList->items)){

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

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

?>

<div class="yvideo-box">

<iframe width="280" height="150" src="https://www.youtube.com/embed/<?php echo $item->id->videoId; ?>" frameborder="0" allowfullscreen></iframe>

<h4><?php echo $item->snippet->title; ?> </h4>

</div>

<?php

}

}

}else{

echo '<p class="error">'.$apiError.'</p>';

}

?>

</div>

</div>

</div>

</body>

</html>

Now you can check your own.

I hope it can help you...


Recommended Posts