Extracting Title Description Thumbnail using Youtube Data API

Last modified on May 3rd, 2018 by Vincy.

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 more code samples available with the YouTube Data API library. For accessing this library from our PHP application we have to create 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.

video-thumbnail

I used Youtube Data API v3 library to extract data from the video URL. In a previous PHP tutorial, I have presented you a PHP script to create 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. Thumbnail along with the title and description will tell more about the video at the first sight. In SEO point of view text content gives more advantages than the media contents like images, 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 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 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 the description will be displayed below the HTML form. For displaying video thumbnail, 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 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'];
?>

Extracting Title Description Thumbnail using YouTube Data API- Output

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

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

Download

This PHP code tutorial was published on May 3, 2018.

↑ Back to Top

Share this Article