How to Get YouTube Video Tags Using YouTube API

Recently I worked on a project where our client wants to get tags of YouTube videos. They are running their YouTube channel and wanted a simple utility to get tags of any YouTube video using the video URL. In this article, I show you how to get YouTube video tags using the YouTube API.

Using this utility, one can get tags of any YouTube video providing the correct video URL. This can help the YouTubers to view the tags used for their own/competitor videos. It will add benefits to find out the relevant tags for their videos.

Below are possible URL formats of any YouTube video. You must use the URL matching with any of the above formats.

  • https://youtu.be/VIDEO_ID
  • https://www.youtube.com/embed/VIDEO_ID
  • https://www.youtube.com/watch?v=VIDEO_ID

Getting Started

To get started, you first need to get an API key from your Google Console account. This API key is necessary while interacting with the YouTube APIs. Without this key YouTube considers all incoming API calls as unauthorized.

Below are the steps to grab the API key.

  • Head over to the Google Developer Console https://console.developers.google.com.
  • Create a new project. Optionally, you can also select an existing project.
  • Give a name to your project. Google Console will create a unique project ID for you.
  • Your newly created project will appear on top of the left sidebar.
  • Click on Library from the left menu. You will see a list of Google APIs. Enable the YouTube Data API.
  • Next, from the left menu click on the Credentials. Select API key under Create credentials.
  • Copy this API Key.
API key

After this, create a simple form where you can add a YouTube video URL and send it for further processing.

<form method="get">
    <p>
        <input type="text" name="ytvideo" placeholder="Enter YouTube Video URL" value="<?php if (array_key_exists('ytvideo', $_GET)) echo $_GET['ytvideo']; ?>" required />
    </p>
    <p>
        <input type="submit" name="submit" value="Submit">
    </p>
</form>

Get YouTube Video Tags using YouTube API

As mentioned before, YouTube supports different types of URLs. We need to look into the different URL formats and extract the video id from them.

Let’s write the function which extracts the video id using the regular expression from a supported YouTube video URL format.

function extractVideoID($url){
    $regExp = "/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/";
    preg_match($regExp, $url, $video);
    return $video[7];
}

Next, write a method that will give an API call and process the response. This response would contain the tags so return it from the same method.

function getYTTags($api_url = '') {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $arr_result = json_decode($response);
    if (isset($arr_result->items) && isset($arr_result->items[0]->snippet->tags)) {
        return $arr_result->items[0]->snippet->tags;
    } elseif (isset($arr_result->error)) {
        die("No video tags found.");
    }
}

When a user enters a valid YouTube video URL, we need to call getYTTags function and build a variable that contains a list of video tags.

$api_key = 'YOUR_API_KEY';
$arr_tags = array();
if (array_key_exists('ytvideo', $_GET)) {
    extract($_GET);
    $video_id = extractVideoID($ytvideo);
    $api_url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$video_id&type=video&key=$api_key";
    $arr_tags = getYTTags($api_url);
}

Make sure to replace the placeholders with your actual API key. Finally, display the list of tags in the HTML format.

if (!empty($arr_tags)) {
    echo "<ul>";
    foreach ($arr_tags as $tag) {
        echo "<li>$tag</li>";
    }
    echo "</ul>";
}

Final Code

We have written the code in pieces. Below is the final code which you can copy and use in your application.

<?php
$api_key = 'YOUR_API_KEY';
 
function getYTTags($api_url = '') {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $arr_result = json_decode($response);
    if (isset($arr_result->items) && isset($arr_result->items[0]->snippet->tags)) {
        return $arr_result->items[0]->snippet->tags;
    } elseif (isset($arr_result->error)) {
        die("No video tags found.");
    }
}
 
function extractVideoID($url){
    $regExp = "/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/";
    preg_match($regExp, $url, $video);
    return $video[7];
}
 
$arr_tags = array();
if (array_key_exists('ytvideo', $_GET)) {
    extract($_GET);
    $video_id = extractVideoID($ytvideo);
    $api_url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$video_id&type=video&key=$api_key";
    $arr_tags = getYTTags($api_url);
}
?>
 
<form method="get">
    <p>
        <input type="text" name="ytvideo" placeholder="Enter YouTube Video URL" value="<?php if (array_key_exists('ytvideo', $_GET)) echo $_GET['ytvideo']; ?>" required />
    </p>
    <p>
        <input type="submit" name="submit" value="Submit">
    </p>
</form>
 
<?php
if (!empty($arr_tags)) {
    echo "<ul>";
    foreach ($arr_tags as $tag) {
        echo "<li>$tag</li>";
    }
    echo "</ul>";
}

It’s all about getting YouTube video tags using the YouTube API and PHP. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

6 thoughts on “How to Get YouTube Video Tags Using YouTube API

Leave a Reply

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