Skip to content

All topics  /  PHP

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

PHP ·

Teams applying “Get Videos From YouTube Channel Using Data API V3 And PHP Example” in a production project can use the platform page to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.

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.