READ MORE & ENJOY πŸ€—

Blog Details

Blog Image

Using the YouTube API with PHP: Fetch Channel Data and Videos

Harnessing the power of the YouTube API in your PHP projects allows you to access valuable information about channels and their videos. In this guide, I'll walk you through the process of using the YouTube Data API to fetch channel data and display videos on your website.


Prerequisites

Before diving in, make sure you have the following:

  • - A YouTube Data API key.
  • - The YouTube channel ID you want to fetch data from.
<?php
// Define your YouTube Data API key and channel ID
$you_tube_data_api_key = 'YOUR_API_KEY';
$channel_id = 'YOUR_CHANNEL_ID';

// Function to fetch channel data
function fetchChannelData($api_key, $channel_id) {
    $playlist_id_url = "<https://www.googleapis.com/youtube/v3/channels?key=$api_key&id=$channel_id&part=contentDetails,snippet,statistics>";
    $playlist_id_response = @file_get_contents($playlist_id_url);

    if ($playlist_id_response === false) {
        die('Error fetching channel data.');
    }

    $playlist_id_arr = json_decode($playlist_id_response, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
        die('Error parsing channel data.');
    }

    if (!empty($playlist_id_arr['items'])) {
        $uploads_playlist_id = $playlist_id_arr['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
        $channel_name = $playlist_id_arr['items'][0]['snippet']['title'];
        $channel_logo = $playlist_id_arr['items'][0]['snippet']['thumbnails']['default']['url'];
        $subscriber_count = $playlist_id_arr['items'][0]['statistics']['subscriberCount'];

        return [
            'channel_name' => $channel_name,
            'channel_logo' => $channel_logo,
            'subscriber_count' => $subscriber_count,
            'uploads_playlist_id' => $uploads_playlist_id,
        ];
    } else {
        die('Channel data not found.');
    }
}

// Function to fetch channel videos
function fetchChannelVideos($api_key, $uploads_playlist_id) {
    $api_url = "<https://www.googleapis.com/youtube/v3/playlistItems?key=$api_key&playlistId=$uploads_playlist_id&part=snippet&maxResults=50>";
    $youtube_videos = @file_get_contents($api_url);

    if ($youtube_videos === false) {
        die('Error fetching channel videos.');
    }

    $youtube_videos_arr = json_decode($youtube_videos, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
        die('Error parsing channel videos.');
    }

    if (!empty($youtube_videos_arr['items'])) {
        return $youtube_videos_arr['items'];
    } else {
        die('No videos found in the playlist.');
    }
}

// Fetch channel data
$channelData = fetchChannelData($you_tube_data_api_key, $channel_id);

// Fetch channel videos
$videos = fetchChannelVideos($you_tube_data_api_key, $channelData['uploads_playlist_id']);
?>



Step 1: Define Your API Key and Channel ID

In the PHP script, you need to define your YouTube Data API key and the target channel's ID. Replace 'YOUR_API_KEY' and 'YOUR_CHANNEL_ID' with your actual API key and channel ID.

$you_tube_data_api_key = 'YOUR_API_KEY';
$channel_id = 'YOUR_CHANNEL_ID';


Step 2: Fetch Channel Data

Create a function to fetch channel data using the API key and channel ID. This function retrieves information like the channel name, logo, and subscriber count, and uploads the playlist ID.


Step 3: Fetch Channel Videos

Develop a function to fetch videos from the channel's uploads playlist. The API call returns a list of video items, each containing details such as video title, ID, and thumbnail URL.


Step 4: Display Channel Data

Use HTML and PHP to display the channel's logo, name, and subscriber count on your web page.

<div class="channel-info"> <img class="channel-logo" src="<?= $channelData['channel_logo'] ?>" alt="Channel Logo"> <div class="channel-name"><?= $channelData['channel_name'] ?></div> <div class="subscribers"><?= number_format($channelData['subscriber_count']) ?> subscribers</div> </div>


Step 5: Display Channel Videos

Loop through the fetched videos and display them on your web page. Each video is represented with its title and thumbnail, linked to the YouTube video.

<?php
// Loop through and display videos
foreach ($videos as $ytvideo) {
    $video_title = $ytvideo['snippet']['title'];
    $video_id = $ytvideo['snippet']['resourceId']['videoId'];
    $thumbnail_url = $ytvideo['snippet']['thumbnails']['medium']['url'];
?>
<div class="video-item">
    <img

 class="video-thumbnail" src="<?= $thumbnail_url ?>" alt="Video Thumbnail">
    <div class="video-title"><a href="<https://www.youtube.com/watch?v=><?= $video_id ?>" target="_blank"><?= $video_title ?></a></div>
</div>
<?php
}
?>


Conclusion

With the YouTube API and PHP, you can dynamically fetch and display channel data and videos on your website. This is useful for creating interactive and up-to-date content for your users. Explore more features of the YouTube Data API to enhance your web applications further.

Start integrating the YouTube API into your PHP projects and offer your users an engaging YouTube experience right on your website! Happy coding πŸ˜πŸš€!

Connect with Us

Let’s Talk About how I can help!πŸš€

If you like my work and want to avail my services then drop me a message using the contact form. Feel free to reach out to us.