Get YouTube Video Thumbnail in PHP: With and Without API

How to Get the Thumbnail of a YouTube Video using PHP

YouTube is one of the most popular video hosting platforms on the internet, and it provides a variety of thumbnails for each video. In this article, we’ll look at how to get the thumbnail of a YouTube video using PHP.

There are two ways to get the thumbnail of a YouTube video using PHP: using the YouTube API and without using the API.

Getting the Thumbnail using the YouTube API

To get the thumbnail of a YouTube video using the API, we’ll need to get an API key from the Google Developers Console. The API key is used to access the YouTube API and retrieve information about a video.

Once we have the API key, we can use PHP’s cURL library to make a request to the API to retrieve the video’s thumbnail. The API response will be in JSON format, and we’ll use the json_decode() function to convert the response into a PHP object.

Here’s the code to get the thumbnail of a YouTube video using the API:

<?php
$video_id = "VIDEO_ID";
$api_key = "API_KEY";
$url = "https://www.googleapis.com/youtube/v3/videos?id=".$video_id."&key=".$api_key."&part=snippet";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response);
$thumbnail_url = $data->items[0]->snippet->thumbnails->default->url;

echo '<img src="'.$thumbnail_url.'"/>';
?>

Replace “VIDEO_ID” with the ID of the YouTube video and “API_KEY” with a valid API key obtained from the Google Developers Console.

Getting the Thumbnail without using the API

If you don’t want to use the API, you can still get the thumbnail of a YouTube video using a direct URL. YouTube provides a URL pattern for its video thumbnails that you can use to get the thumbnail of a video.

Here’s the code to get the thumbnail of a YouTube video without using the API:

<?php
$video_id = "VIDEO_ID";
$thumbnail_url = "https://img.youtube.com/vi/".$video_id."/maxresdefault.jpg";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $thumbnail_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if($response_code == 200) {
    echo '<img src="'.$thumbnail_url.'"/>';
} else {
    $thumbnail_url = "https://img.youtube.com/vi/".$video_id."/hqdefault.jpg";
    echo '<imgsrc="'.$thumbnail_url.'"/>';
}
?>

Replace “VIDEO_ID” with the ID of the YouTube video.

In the code, we use cURL to make a request to the URL of the thumbnail. The URL of the thumbnail is constructed using the pattern

https://img.youtube.com/vi/VIDEO_ID/maxresdefault.jpg

We check the response code of the cURL request, and if it’s 200, it means the thumbnail URL is valid and we display it. If the response code is not 200, it means that the maxresdefault thumbnail is not available, and we use a fallback URL

https://img.youtube.com/vi/VIDEO_ID/hqdefault.jpg

Conclusion

In this article, we looked at how to get the thumbnail of a YouTube video using PHP, either using the API or without using the API. We also discussed how to extract the video ID from a YouTube video URL. With this information, you can easily get the thumbnail of a YouTube video in your PHP applications.


Comments

Leave a Reply

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

Discover more from Worldhook Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading