Recently I worked on a project where our client wants to get tags for 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 finding out the relevant tags for their videos.
Below are possible URL formats for 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 the API key under Create credentials.
- Copy this 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
- How to Use YouTube API to Upload Video On YouTube Channel
- YouTube API – How to Get List Of YouTube Videos Of Your Channel
- How to Get YouTube Video List By Keywords Using YouTube Search API
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
Thank you for the valuable information! However, I want a YouTube tags generator API key that functions similarly to keywordtool.io. Could you please guide me on how to get one?
Without API it’s not possible in programming. You may get third-party services like rapidtags.
Good information. Can I update any video tag api ?
How to create website like repidtags Generator?
Nice information but brother I want YouTube tags generator API key.it generate tags like rapidtags.io so please tell me how it possible.
Hi Dear,
it is possible without api using to get video tags?
thanks
https://monetizationway.com
Without API it’s not possible in programming. You may get third-party services like Tubebuddy.
Ok dear thanks
Can you make it one post like rapidtags.io tags generator?
How can i do that
Thsnks
Oops, the page heavier turns out I try to practice it I hope it works, thank you
Thank you so much. Solved my problem. But takes time to echo results even for 10 videos in the list.
http://www.santanu.biz