How to get YouTube Keywords using PHP via URL? | YouTube API v3
Required Knowledge
Let’s start if you have knowledge of PHP with the given requirements. As we know the youtube keywords create a big role in driving traffic to your youtube video. To get the Youtube Keywords or phrase through the youtube video URL, you need to access it via an API Key, you will get API Key from google credential as shown on this page get-API-google.
Table of Contents
Create Google API Key For YouTube API v3
In order to use YouTube API v3, the user needs to get the API key. You will get API Key credential from google credential as shown on this page get-API-google in steps.
key=AIzaSyDPT6MUbePIaeyX8f6EC8avpD446AYHOtk
Prepare URL
To get the data from YouTube API v3 here requires preparing a URL which helps in what types of data is needed and what types. Create a $var variable that contains the ID of Youtube Video e.g, mBJMkFNRVek . The following URL extracting title, description, and tags. It require the &fields and &part attribute to extract the snippet as &fields=items(snippet(title,description,tags)) and &part=snippet respectively.
Don’t forget to replace your Google API key in the following URL as shown &key=AIzaSyAIfYZNM5wRKRr3Z qyentZ_KFBDUapnl84
$site='https://www.googleapis.com/youtube/v3/videos?id='.$var.'&key=AIzaSyAIfYZNM5wRKRr3ZqyentZ_KFBDUapnl84&fields=items(snippet(title,description,tags))&part=snippet';
Create Form To Extract YouTube Keywords
Create a required Input Field in the form as shown below, the form contains URL input field and TextArea for input/output respectively.
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Youtube Keyword Extract Tool: Increase Views & Subscribers</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<h1 > DEMO of Youtube API v3 , reterive or extract keywords from Youtube Videos </h1>
<div class=" w-100 d-flex align-items-center d-flex justify-content-center">
<form action="" method="post" name="form">
<h5>Enter a Youtube Url to get Keyword or phrase :</h5>
<div class="input-field" >
<div class='inside-form'>
<input type="text" class="form-control form-input" name="youtubeData" id="youtubecaption" placeholder="Enter youtube url here" ></input>
</div>
<div class='inside-form2'>
<input type="submit" href="#captionD" class="btn btn-primary codeNaiveBTN" name="getButton" id="test" value="Get !"> </input><br/>
</div>
</div>
<div class="box-vertical">
<label ><?php echo $word_count?> </label>
</div>
<div class="box-vertical ">
<textarea id="captionD" class="captionData w-100 " height="400px"><?php echo $extracted_data;?></textarea>
</div>
</form>
Create PHP Code To Perform The Back-End Operations
Create a PHP code that helps to extract the keywords, the following code has two variables $extracted_data and $word_count that store the information regarding keywords and word counts respectively. Create getKeywords() function which executes the prepared URL and returns the JSON file. After the successful execution of the CURL code, it decodes the JSON format file and extracts the required data e.g tags (youtube keywords) then saves it into the $extracted_data variable.
<?php
$extracted_data= '';
$word_count='';
if (isset($_POST['getButton'])) {
$var1=htmlentities($_POST['youtubeData']);
$var2=str_replace("https://youtu.be/","",$var1);
if($var2==$var1){
$var2=str_replace("https://www.youtube.com/watch?v=","",$var1);
}
//echo $var1;
$extracted_data=getKeywords($var2);
if(str_word_count($extracted_data)==0){
$word_count='<h2 style="color:red;!important" >Total Keyword Found : '. str_word_count($extracted_data)."</h2>";
}
else{
$word_count='<h2 style="color:green;!important" >Total Keyword Found: '. str_word_count($extracted_data)."</h2>";
}
}
function getKeywords($var){
try{
$data='';
$site='https://www.googleapis.com/youtube/v3/videos?id='.$var.'&key=AIzaSyAIfYZNM5wRKRr3ZqyentZ_KFBDUapnl84&fields=items(snippet(title,description,tags))&part=snippet';
$ch = curl_init();
$extracted_data = '';
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $site);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$value = json_decode(json_encode($data), true);
if(empty($value['items']))
{
$extracted_data='';
throw new Exception();
exit;
}
else{
$title = $value['items'][0]['snippet']['title'];
$data= $value['items'][0]['snippet']['tags'];
for($i=0; $i<count($data);$i++)
{
$extracted_data =($extracted_data.$data[$i].', ');
}
}
}
catch(Exception $e){
echo '<script>alert("unable to get from this url")</script>';
}
return $extracted_data;
}
?>
Output Of YouTube API v3 Keyword Extract
The output of created form above which takes the YouTube Video URL as an input and show the output into the TextArea :

The output of extracted youtube-keywords from the given YouTube Video URL and shows the output into the Textarea :

Leave a Reply