Getting video tags with the YouTube api in PHP

Getting a YouTube videos tags using the YouTube api in PHP is actually quiet easy, just follow this guide. You will need your YouTube api key!

It easy to just make a function, this also helps with repeat use!

functions.php

function video_tag($tag, $video_id)
{
    $api_key = 'YOU API KEY HERE';
    $data = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" . $video_id . "&key=".$api_key.""), true);
    $tag = $data['items'][0]['snippet']['tags'][$tag];
    return $tag;
}

Here we have a function that will return the tag depending on your tag key. 0 is the first tag… 1 is the next and so forth.

An example for this function being used on this Casey Neistat video

include('functions.php');
echo video_tag(1,'0-7wMD5ISIs');//abandoned mall
echo video_tag(3,'0-7wMD5ISIs');//samsung

His second tag for the video was “abandoned mall and his fourth tag was “samsung”.

You can see the result of the whole api call the function does in this spoiler:

The returned API data
{
  "kind": "youtube#videoListResponse",
  "etag": "\"S8kisgyDEblalhHF9ooXPiFFrkc/YT8xzHl5MVqhbabi8wzgi3wNHXM\"",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#video",
      "etag": "\"S8kisgyDEblalhHF9ooXPiFFrkc/eFOkGEJ6_bWALQT6Ri9euDTlCXI\"",
      "id": "0-7wMD5ISIs",
      "snippet": {
        "publishedAt": "2017-12-18T21:22:24.000Z",
        "channelId": "UCtinbF-Q-fVthA0qrFQTgXQ",
        "title": "ABANDONED MALL TURNED INTO WINTER WONDERLAND",
        "description": "Boys and Girls Clubs; http://www.boysgirlsclubs.org/\n\ncreator collaborators; \nAkilah Hughes https://www.youtube.com/user/smoothiefreak\nAnn Lupo https://www.youtube.com/user/annlupo\nDotan Negrin https://www.youtube.com/user/pianoaround\nElle Mills https://www.youtube.com/user/ElleOfTheMills\nJesse Korgemma https://www.youtube.com/user/PMbasementshow\nJesse Wellens https://www.youtube.com/user/PrankvsPrank\nJohn Hill https://www.youtube.com/user/john2010hillbilly\nKrystal Lora https://www.youtube.com/user/thekrystallora\nMike Boyd https://www.youtube.com/user/microboyd\nMislav Mironovic https://www.youtube.com/channel/UCIcfHSWce_4qQ0MTTx-JJ0g\nMolly Burke https://www.youtube.com/user/MollyBurkeOfficial\nNigel Sylvester https://www.youtube.com/user/nigelsylvesterlive\nPeter McKinnon https://www.youtube.com/user/petermckinnon24\nRachel DeMita https://www.youtube.com/user/rademita\nSam Sheffer https://www.youtube.com/user/sampsheffer\nSara Dietschy https://www.youtube.com/user/saradietschy\nShaun McBride https://www.youtube.com/user/Shonduras\nSuper Saf https://www.youtube.com/user/SuperSafTV\nWill Haynes https://www.youtube.com/user/WilliamHaynesTV",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/0-7wMD5ISIs/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/0-7wMD5ISIs/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/0-7wMD5ISIs/hqdefault.jpg",
            "width": 480,
            "height": 360
          },
          "standard": {
            "url": "https://i.ytimg.com/vi/0-7wMD5ISIs/sddefault.jpg",
            "width": 640,
            "height": 480
          },
          "maxres": {
            "url": "https://i.ytimg.com/vi/0-7wMD5ISIs/maxresdefault.jpg",
            "width": 1280,
            "height": 720
          }
        },
        "channelTitle": "CaseyNeistat",
        "tags": [
          "christmas",
          "abandoned mall",
          "milwaukee",
          "samsung",
          "casey neistat",
          "human flying drone",
          "boys and girls club"
        ],
        "categoryId": "22",
        "liveBroadcastContent": "none",
        "localized": {
          "title": "ABANDONED MALL TURNED INTO WINTER WONDERLAND",
          "description": "Boys and Girls Clubs; http://www.boysgirlsclubs.org/\n\ncreator collaborators; \nAkilah Hughes https://www.youtube.com/user/smoothiefreak\nAnn Lupo https://www.youtube.com/user/annlupo\nDotan Negrin https://www.youtube.com/user/pianoaround\nElle Mills https://www.youtube.com/user/ElleOfTheMills\nJesse Korgemma https://www.youtube.com/user/PMbasementshow\nJesse Wellens https://www.youtube.com/user/PrankvsPrank\nJohn Hill https://www.youtube.com/user/john2010hillbilly\nKrystal Lora https://www.youtube.com/user/thekrystallora\nMike Boyd https://www.youtube.com/user/microboyd\nMislav Mironovic https://www.youtube.com/channel/UCIcfHSWce_4qQ0MTTx-JJ0g\nMolly Burke https://www.youtube.com/user/MollyBurkeOfficial\nNigel Sylvester https://www.youtube.com/user/nigelsylvesterlive\nPeter McKinnon https://www.youtube.com/user/petermckinnon24\nRachel DeMita https://www.youtube.com/user/rademita\nSam Sheffer https://www.youtube.com/user/sampsheffer\nSara Dietschy https://www.youtube.com/user/saradietschy\nShaun McBride https://www.youtube.com/user/Shonduras\nSuper Saf https://www.youtube.com/user/SuperSafTV\nWill Haynes https://www.youtube.com/user/WilliamHaynesTV"
        },
        "defaultAudioLanguage": "en"
      }
    }
  ]
}

And that’s how to easily get a YouTube videos tags using the YouTube api in PHP.