How to receive data from YouTube videos through its API and PHP

PHP is an extremely versatile technology. In my case, beyond using it to handle business logic or persist data, I have used it many times to connect to external services and consume APIs, and one of the most useful is the YouTube Data API.

In this guide, I'm going to show you how to receive data from YouTube videos through its API using PHP, process them, and display them on your website. It is a very common approach when you need to list videos from a channel, show thumbnails, titles, or even play them embedded—something I have implemented especially in music projects where videos play hidden, leaving only the background audio.

Why use the official YouTube API and not scraping?

Before diving into the code, it's worth clarifying something important.

YouTube offers a public, legal, and documented API to access its video information. Using it has several clear advantages:

  • You comply with Google and YouTube policies
  • You get structured and updated data
  • You avoid blocks, captchas, or changes in the HTML
  • You have control over results, filters, and pagination

Although you could technically do scraping, in my experience it is not recommended when an official API exists, especially for long-term projects.

Prerequisites

To follow this tutorial you need:

  • PHP 7.x or higher
  • Curl extension enabled (optional, but recommended)
  • A local server or hosting
  • A Google account

It is not necessary to use frameworks; the example works perfectly in pure PHP, although you can later easily adapt it to Laravel, CodeIgniter, or any other framework.

How to get your YouTube API Key

This is a key step that many tutorials take for granted.

  • Create a project in Google API Console
    • Access Google Cloud Console
    • Create a new project
    • Assign an identifying name
  • Enable YouTube Data API v3
    • Within the project:
    • Go to “APIs & Services”
    • Search for YouTube Data API v3
    • Click Enable
  • Create credentials (API Key)
    • Go to “Credentials”
    • Create credentials → API Key
    • Copy the generated key (you will use it in PHP)

Structure of a request to YouTube Data API, key for performing CURL requests

First of all, we must understand how the YouTube URL is composed that we will use from PHP to make the requests, and this is the heart of our experiment or example that we will perform a little later; the YouTube URL to perform queries for video details of a channel is as follows:

https://www.googleapis.com/youtube/v3/search
https://www.googleapis.com/youtube/v3/search?key=$key&channelId=$channel&part=snippet,id&order=date&maxResults=50

Making a side note, we see that we use the Google Apis website, also known as Google Developers, and not the YouTube one; the Google Apis website provides information about other Google APIs that you can consult.

Main Parameters

Returning to the explanation of the URL shown above, we see that it is composed of several parameters that by nature are passed via HTTP GET:

  • key: your API Key
  • channelId: To indicate the ID of the channel containing the video or group of videos to display in a list.
  • part: what data you want to obtain (snippet, id, etc.)
  • order: The fetch order of the videos, which can be date, rating, alphabetically, etc.; you can get more information about this at the following link. (date, rating, relevance…)
  • maxResults: To indicate the maximum expected video results, with a limit of 50.

Making the API request from PHP

With the above explained, what remains for us to do is actually very little. Now we just have to generate the request and process it through PHP; for that, we use the following function that internally employs a CURL request:

function get_videos() {
    $key = 'TU_API_KEY';
    $channel = 'ID_DEL_CANAL';

    $url = "https://www.googleapis.com/youtube/v3/search?key=$key&channelId=$channel&part=snippet,id&order=date&maxResults=50";

    $json = file_get_contents($url);
    $data = json_decode($json, true);

    return $data;
}

Explaining the code for performing the CURL query to the YouTube API

We see that in addition to the rest of the parameters specified above, there is a key parameter which is nothing more than the one generated in the Google Developers Console when we created the project and enabled the YouTube API;

The file_get_contents method allows obtaining the source code (a string) of a web page through a URL; in our case, the web page is plain text in JSON format, which we decode with the json_decode method and then we can process and iterate perfectly.

Understanding the YouTube JSON response

The API returns a structured JSON object. The most important fields are in:

$data['items']

Each element contains:

  • id.videoId → Video ID
  • snippet.title → title
  • snippet.description → description
  • snippet.thumbnails → thumbnails
  • snippet.publishedAt → publication date

Understanding this structure allows you to fully customize how you display the videos.

Displaying YouTube video details with PHP and HTML

If we want to render the videos in our HTML, which would be the basic thing we want to do—show a list of the obtained videos—we can do it with the following PHP code:

<?php foreach ($videos['items'] as $video): ?>
    <?php if (isset($video['id']['videoId'])): ?>
        <iframe 
            width="560" 
            height="315"
            src="https://www.youtube.com/embed/<?php echo $video['id']['videoId']; ?>"
            frameborder="0"
            allowfullscreen>
        </iframe>
    <?php endif; ?>
<?php endforeach; ?>

We obtain a list of videos like the one in the following image:

youtube video list

Error handling and quota limits

The YouTube API works with a quota system. Some common errors are:

  • 403: quota exceeded
  • 400: incorrect parameter
  • 404: channel does not exist

Practical recommendations:

  • Implement caching (file or database)
  • Do not call the API on every page load
  • Log errors for debugging

Best practices when using YouTube Data API with PHP

Over time, these are the practices that have given me the best results:

  • Separate API logic into a function or class
  • Cache results for at least 10–30 minutes
  • Always validate videoId
  • Do not assume all items are videos (there are playlists)

This type of approach makes it easy to port the code to PHP frameworks without issues.

 

FAQs – Frequently Asked Questions

  • Can I get more than 50 videos?
    • Yes, using the pageToken parameter for pagination.
  • Do I need OAuth2 for public data?
    • No. For public videos, an API Key is enough.
  • Can I get only titles and thumbnails?
    • Yes, by reading only snippet.title and snippet.thumbnails.

Conclusions

And that's all; with two simple steps, we can obtain YouTube videos, which we do by performing a CURL query to the YouTube API, which is perfectly public and legal.

YouTube offers us this tool to enhance our sites and make them dynamic, showing content in real-time and perfectly updated. The next and last step is that since we have our video data, we simply return and render them in a list.

This is a script that we can easily take to PHP frameworks via a helper or a library to bring in videos from a YouTube channel.

Receiving data from YouTube videos through its API using PHP is a simple, powerful, and totally legal process. With just an API Key and a well-constructed request, you can list videos, show detailed information, and keep your site always updated.

It is a script that you can reuse, extend, and adapt easily, both in small projects and larger applications.

I agree to receive announcements of interest about this Blog.

We will see how to obtain the details of the videos of a channel with PHP using PHP's CURL requests and then process them and perform some action such as displaying them in a list.

| 👤 Andrés Cruz

🇪🇸 En español