PHP code to Generate Video object with Schema Markup for Rich Result using YouTube API
How to add Video object with schema markup to the web page? We can see videos appearing in search result. How to generate structured data so the same can be used as markup for the YouTube published videos.
We will first extract the video id from the URL we copied from YouTube. Here we can use one form so we can easily paste the URL in a textbox and submit the form to same page.
Generating Schema Code in PHP for Rich results while using Video inside Webpage using Youtube API
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
We need the following ( minimum ) data from YouTube to generate our code.
Name : the title of the video embadUrl : the URL of the video uploadDate : Date of upload of video to the YouTube thumbnailURL : URL of the thumbnail used for the video. Description : the Description we submitted while uploading the video or edited afterwards.
We are preparing a variable $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.