PHP code to Generate Video object with Schema Markup for Rich Result using YouTube API

Generating Rich Results
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 be using Google YouTube API to connect and fetch data. Read more on Google Developers YouTube API

Developing the schema in PHP

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

After receiving the URL we will break the string by using explode() and get the video id part.
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=""; // 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.

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.
$apikey = 'YOUR_API_KEY_HERE';
//$videoId='DLF2wvDqrwo'; // sample to try 
if(strlen($videoId) >3){
$data = file_get_contents($path);
$json = json_decode($data);
// with these variables start creating the
// string variable with code
<div itemprop=\"video\" 
itemscope itemtype=\"\">
<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\" 
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'>
Rich Results with Video Object
Youtube playlist Videos with all details Check your page for Rich results ( Google ) Generate Thumbnail URL of your Video
All PHP Scripts PHP Home
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer