echo "<form method=post action=''>
<input type=text name=t1 class='Form-control'>
<input type=submit value='Go'></form>";
$url=$_REQUEST['t1']; // Collect the user submitted youtube URL
//$url="https://youtu.be/8myaNdeRKwk"; // sample to try
$ar=explode("/",$url); // breaking the string
$videoId=$ar[3]; // get the last part , the id only
After getting the $vidioId
we will use Google Developers YouTube API to get data from YouTube.Name
: the title of the video embadUrl
: the URL of the video uploadDate
: Date of upload of video to the YouTubethumbnailURL
: URL of the thumbnail used for the video. Description
: the Description we submitted while uploading the video or edited afterwards.
$str
with the code and finally we will display the value of the string variable $str
inside one Textbox for the user to select and copy the code.
$apikey = 'YOUR_API_KEY_HERE';
//$videoId='DLF2wvDqrwo'; // sample to try
if(strlen($videoId) >3){
$path="https://www.googleapis.com/youtube/v3/videos?key=";
$path=$path.$apikey."&part=snippet&id=".$videoId;
$data = file_get_contents($path);
$json = json_decode($data);
$title_4983=$json->items[0]->snippet->title;
$uploadDate_4983=$json->items[0]->snippet->publishedAt;
$embedUrl_4983='https://www.youtube.com/embed/'.$videoId;
$thumbnailUrl_4983=$json->items[0]->snippet->thumbnails->standard->url;
$description_4983=$title=$json->items[0]->snippet->description;
// with these variables start creating the
// string variable with code
$str="
<div itemprop=\"video\"
itemscope itemtype=\"http://schema.org/VideoObject\">
<span itemprop=\"name\">$title_4983</span><br>
<meta itemprop=\"embedURL\" content=\"$embedUrl_4983\"/>
<meta itemprop=\"uploadDate\" content=\"$uploadDate_4983\" />
<meta itemprop=\"thumbnailUrl\" content=\"$thumbnailUrl_4983\"/>
<meta itemprop=\"description\" content=\"$description_4983\"/>
<iframe width=\"560\" height=\"315\" src=\"$embedUrl_4983\"
title=\"$title_4983\" frameborder=\"0\"
allow=\"accelerometer; autoplay; clipboard-write;
encrypted-media; gyroscope; picture-in-picture\"
allowfullscreen>
</iframe></div>";
}else{
$str='';
}
Now our $str
is ready with the code, let us display it inside one textarea.
echo "<textarea name='code_dump' rows=10 id='code_dump'>
$str
</textarea>";
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.