Open In App

How to get YouTube video data by using YouTube data API and PHP ?

Last Updated : 15 Oct, 2020
Like Article

Thumbnails are used for a preview of a single or group of media entities in a single explorer window. 
It saves time and gives a better focus to the user’s attention and help us grab attention and educate viewer enhancing web design project.

The description is a few words more than the title. The more words in the YouTube video description, the possibility of more exact keywords to represent on, which is very important to get found in search engines. 
Video channels should know data about the video to rank better and get more public views because no one can see what exactly in the video content, so it needs data.

The main aim of the title is to represent and describe each result in the best way and which can relate to the user’s search query. The title is the foremost piece of information which is used to decide which link to click on, so it becomes important to retrieve well-understood titles on your web pages.

The titles help the search engine to crawl for the relevant data. 

Title and Description plays a key role in making the video important and for search engine ranking factors.

Purpose: The article shows the title, description, and thumbnail for a particular YouTube URL link. HTML form requests a YouTube video URL for the given input. 

When the user pastes the URL into the input field and submits it, this URL variable will be sent to the PHP file. After getting data from the URL, it will be previewed on the browser. 

YouTube Video Data API key: 

  • YouTube Data API is available free for usage. 
  • We need the YouTube data API key for the implementation of the following example program. 
  • Each YouTube video will have a specific ID associated with it and this ID is passed to the API call for data retrieval. Let us see step by step how to create a YouTube data API key. 


  • Go to the Google Developer Console link and login to your own Google account. 
  • After logging in, go to the link and click on the blue-colored CREATE PROJECT button. 
  • We have to wait for some time until Google prepares the developers’ project.
create project
  • Fill in the project name of your own choice. 

write project

  • In Google API Console, click the ‘Select a project link’.
  • Then click the plus button (+) to create a project. 

select project

  • Enter the project name of your own choice and select other options >> Click the create button.
  • It takes a few seconds to set up a developer’s new project. Once it’s done, select your project.
  • Click the library link on the left navigation menu. Under the YouTube APIs section, click the YouTube Data API link.
  • Enable the YouTube Data API v3 to access the YouTube data by clicking the uppercased ENABLE button. 

youtube data API

  • Click the Credentials link on the left navigation menu. Press the “Create credentials button” selecting the API key.
  • A dialog box will appear with your newly created API Key. Use this API key in the YouTube Data API v3 API request.

HTML code: The following code shows the HTML form to request YouTube video URL for the given input. When the user pasting the URL into the input field, this URL variable will be sent to the “showDetails.php” file. After getting data from the URL, it will be previewed on the browser.


<!DOCTYPE html>
    <link rel="stylesheet" href="style.css">
    <br />
        Please enter the youtube 
        URL in the input
    <div id="divID" class="container-class">
        <form method="post" action="showDetails.php">
            <input class="input-class" 
                type="text" name="url" 
                placeholder="Enter your URL">
            <br />
            <br />
            <input class="submit-class" 
                type="submit" name="submit" 
                value="Submit URL">

PHP code: The following code demonstrates the “showDetails.php” file used in the above HTML web page.


    error_reporting (E_ALL ^ E_NOTICE); 
    /*Just for your server-side code*/
    header('Content-Type: text/html; charset=ISO-8859-1');
<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1">
        .thumbnail-class {
            width: 50%;
            margin: 10px;
            padding: 5px;
            border-radius: 1px;
        #titleDescID {
            width: 50%;
            margin: 10px;
            padding: 10px;
<br />
    <div id="thumbnailID" class="thumbnail-class">
        if (isset($_POST['submit'])){
            $url = $_POST['url'];
            /* Extracting the v element from the link*/
            $vString = explode("v=", $url);
            $youtubeId = $vString[1];
        <div id="videoDivID" 
            <iframe id="iframe" 
"<?php echo $youtubeId; ?>"
                data-autoplay-src="<?php echo $youtubeId; ?>?autoplay=1">
        //Its different for all users
        $myApiKey = 'ENTER YOUR API KEY'
            . $youtubeId . '&key=' . $myApiKey . '&part=snippet';
        /* Create new resource */
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        /* Set the URL and options  */
        curl_setopt($ch, CURLOPT_URL, $googleApi);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        /* Grab the URL */
        $curlResource = curl_exec($ch);
       /* Close the resource */
        $youtubeData = json_decode($curlResource);
        $youtubeVals = json_decode(
            json_encode($youtubeData), true);
        $urlTitle = $youtubeVals
        $description = $youtubeVals
    <div id="titleDescID">
            echo '<b>Title: ' . $urlTitle . '</b>';
            echo '<b>Description: </b>' . $description;

CSS code: The following code demonstrates the “style.css” file used in the above HTML code.


    font-family: Sans-serif,Arial;
    width: 600px;
    background: #e9e9e9;
    border: #B3B2B2 1px solid;
    border-radius: 2px;
    margin: 20px;
    padding: 40px;
    width: 100%;
    border-radius: 2px;
    padding: 20px;
    border: #e9e9e9 1px solid;
    padding: 10px 20px;
    background: #000;        
    color: #fff;
    font-size: 0.8em;
    width: 110px;
    border-radius: 4px;
    border: black 2px solid;


The following image shows the screen before the user clicks the “Submit URL” button:

The following image is shown after the YouTube link is entered by the user and submitted:

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads