Extracting Title Description Thumbnail using Youtube Data API

by Vincy. Last modified on June 8th, 2022.

YouTube Data API library can be used to implement API-based access to YouTube videos. Using this library we can extract video data like title, description, video thumbnail, create playlists, channels and more.

You can refer to more code samples available with the YouTube Data API library. For accessing this library from our PHP application we have to create a YouTube Data API project and get the API key.

We have to pass this key from our PHP web application along with the API request for accessing the required YouTube service.

The example uses the Youtube Data API v3 library to extract data from the video URL. It uses the PHP curl post method to read the video information.

In a previous PHP tutorial, we have seen an example to create an image thumbnail of a YouTube video without using Youtube Data API.

The image thumbnail source is formed by using the video id. In this PHP script example, I am sending the video id with the API key while requesting YouTube Data API access.

I parsed the resultant API response to get the video data like title and description. Thumbnails along with the title and description will tell more about the video at the first sight.

This screenshot shows the output after getting the title, and description data using YouTube Data API V3. The video thumbnail is embedded using an iframe.

extracting-title-description-thumbnail-from-youtube-video-output

 

From an SEO point of view text content gives more advantages than media content like images and videos. So, showing videos with informative text data will help your video gallery page to be ranked higher.

How to Get YouTube Video Data API Key

In this section, I have listed the steps required to get the YouTube data API key.

Step1: Login to your Google account and create a new project in Google Developer Console.

create-api-project

Step2: Search the Google API library filter to find the YouTube Data API V3 library to enable it.

enable-youtube-data-api

Step3: Generate API key by selecting the newly created project. A popup will show the API key credential as shown below. This API key will be sent while requesting YouTube data.

generate-api-key

HTML Form to Get Video URL

The following code shows the HTML form to request YouTube video URL for the given input. When the URL is submitted via this form, the video id will be extracted from the URL.

This id will be passed with the API key to get the YouTube data. After getting YouTube data, the video thumbnail, title and description will be displayed below the HTML form. For displaying the video thumbnails, the YouTube video embed URL is formed by using the unique video id. The code shows the iframe with the source of the video embed.

<div class="thumbnail-form-container">
    <form method="post" action="">
        <input class="input-field" type="text" name="url"
            placeholder="Enter URL"><br /> <br /> <input
            class="btn-submit" type="submit" name="submit"
            value="Submit">
    </form>
</div>
<?php
    if (isset($_POST['submit'])) {
        $url = $_POST['url'];
        $value = explode("v=", $url);
        $videoId = $value[1];
?>
<div class="thumbnail">
    <div id="videoDiv">
        <iframe id="iframe" style="width: 100%; height: 100%"
            src="//www.youtube.com/embed/<?php echo $videoId; ?>"
            data-autoplay-src="//www.youtube.com/embed/<?php echo $videoId; ?>?autoplay=1"></iframe>
    </div>
</div>
<div id="titleDiv">
   <h2><?php echo $title; ?></h2>
   <p><u>Description</u>: <?php echo $description; ?></p>
 </div>
<?php 
	} 
?>

Request Google API to Get YouTube Data via the V3 library using PHP

The following CURL request is sent to the Google API to get YouTube video data by using the YouTube Data API V3 library.

In the API request, we have to send the API key and the unique id of the video. The id will be extracted from the video URL posted via the form. After getting YouTube data, I have parsed the response to get the title and the description.

<?php
	$apikey = 'YOU_API_KEY';
    $googleApiUrl = 'https://www.googleapis.com/youtube/v3/videos?id=' . $videoId . '&key=' . $apikey . '&part=snippet';
    
	$ch = curl_init();
    
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $googleApiUrl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
        
    curl_close($ch);
        
    $data = json_decode($response);
        
    $value = json_decode(json_encode($data), true);
        
    $title = $value['items'][0]['snippet']['title'];
    $description = $value['items'][0]['snippet']['description'];
?>

Download

Leave a Reply

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

↑ Back to Top

Share this Article