How to retrieve YouTube video title and description from PHP script

Simple example on how to retrieve and display video title, description and some other associated data from YouTube video.

First step – get the API key

YouTube API key can be obtained for free from this address: https://console.developers.google.com/home/dashboard

Create new project
Create new project
Click "Enable and manage APIs"
Click “Enable and manage APIs”
Select YouTube Data API
Select YouTube Data API
Enable the API
Enable the API
Select "Credentials" on the left sidebar
Select “Credentials” on the left sidebar
Click "Add Credentials"
Click “Add Credentials”
Choose "API key"
Choose “API key”
Choose "Server key"
Choose “Server key”
Enter your preferred key name, and server IP (optional)
Enter your preferred key name, and server IP (optional)
Done, there's your API key
Done, there’s your API key

A bare-bone code example

<?php

$videoid = 'VIDEO_ID';
$apikey = 'YOUR_API_KEY';

$json = file_get_contents('https://www.googleapis.com/youtube/v3/videos?id='.$videoid.'&key='.$apikey.'&part=snippet');
$ytdata = json_decode($json);

echo '<h1>Title: ' . $ytdata->items[0]->snippet->title . '</h1>';
echo 'Description: ' . $ytdata->items[0]->snippet->description;

Use var_dump($ytdata->items[0]->snippet); to see other available parameters.

10 thoughts on “How to retrieve YouTube video title and description from PHP script

  1. Phil December 11, 2015 / 3:33 pm

    Lots of thanks for this tiny snippet. Read all google docs for hours but haven’t made it running, now it works nearly perfectly!!!

    One question, maybe you ca help me about this: How can i get the title or description in a translated version? I have some translations for a dummy clip in english and german. When viewing on youtube, it works fine, the browser-lang is taken as given in accept-language.

    But I cant gather a translated description on my website. passing the param hl in the iframe-src just toggles ui-language of the player

    Thanks a los and greetings from Spain,
    Phil

    • Phil December 12, 2015 / 3:13 pm

      Ok, found it finally and will share the answer to my own question 😉

      To gather localized translation you set up in youtube, simply use:

      $ytdata->items[0]->snippet->localized->description

  2. Pierre Henrique May 4, 2016 / 7:48 pm

    Hi i’m from Brazil and i loved your tutorial, but please how i can get the views from the videos ?

    • Rosy July 29, 2017 / 5:06 pm

      Hi, Pierre! How did you get the view count using the code?

      Thanks,
      Rosy

  3. Pierre Henrique May 4, 2016 / 8:45 pm

    Hi, i solved the last problem but i want to know how i can get the profile picture from any channel from youtube… if you know please help me !

  4. sam March 1, 2017 / 6:11 pm

    Thanks. I found this very useful

  5. mukesh jakhar July 4, 2017 / 5:38 pm

    From last 2day i am searching code for fetch youtube video title, after 2 days i find out ..thanks man,,, great tuts…

  6. Muhammad Sutiaji April 1, 2018 / 7:46 am

    Thanks alot..
    I have try some methode, but this methode help me so much..
    I also can combine with multiple api key using random array.. 😀

  7. Vaibhav Moradiya December 19, 2018 / 2:01 am

    Thanks

Leave a Reply

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