how to use oembed api using php for youtube - php

I was using WordPress 3.8 and seen oembed API that in WP we can just simply put a link and it automatically embedded that video in WP. So, my question is that:
How can i use oembed feature? in PHP:
give me some practical example, so i can easily use that in my web
and i have seen this in Google
used the code stated in above link:
<?php
$manager = ProviderManager::getInstance();
$obj=$manager->provide("http://www.youtube.com/watch?v=QsROH9YfOZk","object");
$html=$obj->renderClass();
?>
I used that code locally. So,
I appreciate if you tell me that,
Can i implement this API locally means to test ??
Thanks in advance.

First, you need to pass the id of the YouTube video in a special url :
http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3DID_VIDEO&format=xml
Example with the video : https://www.youtube.com/watch?v=l-gQLqv9f4o
We use this url :
http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3Dl-gQLqv9f4o&format=xml
The output is the XML file generated :
<oembed>
<html>
<iframe width="480" height="270" src="http://www.youtube.com/embed/l-gQLqv9f4o?feature=oembed" frameborder="0" allowfullscreen></iframe>
</html>
<author_name>SoulPancake</author_name>
<height>270</height>
<width>480</width>
<thumbnail_url>http://i.ytimg.com/vi/l-gQLqv9f4o/hqdefault.jpg</thumbnail_url>
<author_url>http://www.youtube.com/user/soulpancake</author_url>
<provider_name>YouTube</provider_name>
<type>video</type>
<thumbnail_width>480</thumbnail_width>
<provider_url>http://www.youtube.com/</provider_url>
<thumbnail_height>360</thumbnail_height>
<version>1.0</version>
<title>A Pep Talk from Kid President to You</title>
</oembed>
So, all you have to do, it's pass this XML file in function simplexml_load_file of PHP. Basically you need to read a XML file to get the iframe of the video.
<?php
$xml = simplexml_load_file("http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3Dl-gQLqv9f4o&format=xml");
echo $xml->title . "<br />";
echo $xml->html;
echo $xml->author_name;
?>
You will have the title of the video, the iframe code and the author of the video.

Related

One page to handle different URL with parameters

The solution is probably very simple, but still, I am struggling to understand how to make it work.
We are preparing for the new school year, and of course, we're required to make things work remotely. We will have a bunch of educational videos posted on Youtube, and an external Windows app will make sure everything is well organized and that teachers can easily upload new videos (via Youtube API), and that teachers can quickly share material to students.
And, when a teacher wants to send a video to students, he will obviously give them a link. They decided that they want videos to be played outside Youtube (for a number of reasons).
We have a WordPress website that is already functional, and which we can use to display Youtube videos that we can embed.
A WordPress page would have embed code for Youtube player, which is provided by Youtube.
But, the issue is that I don't understand how to make video ID changeable (b_DcQHbJIfE) so that one page can play any video which the Windows app sends.
Example:
External Windows app sends: https://www.externalvideoplayer.com/player/b_DcQHbJIfE
A website gets "b_DcQHbJIfE" and actually plays it on the embedded player
App then sends another video, like this: https://www.externalvideoplayer.com/player/c_gcOWSJISe
A website gets and plays "c_gcOWSJISe"
I know a little bit of PHP, and stumbled upon this code:
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $actual_link;
But I am not sure if that would work when the page is loading, since the Windows app probably opens a URL that does not exist.
Any help would be greatly appreciated! Thanks!
I figured this out, here's the code:
<!DOCTYPE HTML>
<HTML>
<?php
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" :
"http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url_ID = array_pop(explode('/', $actual_link));
?>
<iframe width="800" height="800" src="https://www.youtube.com/embed/<?php echo $url_ID?>" frameborder="0" gesture="media" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</html>
So now, when you load this PHP file, and if you add "/VIDEO ID" (https://www.example.com/test.php/c_gcOWSJISe)
The page will load any video that you enter into the URL. Note that this probably does not work on the localhost server, so you need actual server and domain to make this work.
Thanks!

unable to show doc file using google docs api with iframe

I want to show doc file on a browser. I am using Google Docs API with iframe. But it shows me error that google docs refused to connect. I want to get rid of this error kindly help me out..
Here is my code in php, $file is a variable where I am getting the file name and I have to concat it with my iframe..
echo '<iframe src="http://docs.google.com/gview?url=http://192.168.0.56/orangehrm/admin/images/"'.$file.'&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>';
Based from this blogpost - Embedding a Google Doc:
Google provides an iFrame to embed your Google Docs. By default, WordPress will strip out iFrame code for security reasons. The iFrame code will disappear when switching between Visual and Text mode or when Publishing. To prevent this, you will need to change the provided iFrame code.
For example:
Try to change:
to:

Change the src of video tag or iframe to my domain name / id in PHP Youtube clone Script

I am working on a php youtube clone script, similar to playit.pk
If you have experience with such a script then you could probably help me.
On th page watch.php where an iframe is used to view youtube video in the iframe the php source code is:
src="//www.v.ytapi.com/embed/<?php echo $yt->id ?>
but I want to replace it with
src="//www.myDomainName.com/embed/<?php echo $yt->id ?>
How can I achieve that or else Instead of iframe how can I get src like this
http://r5---sn-vgqs7n7y.c.docs.google.com/videoplayback
and directly use it in a video tag.

playing videos on second page php

This is kind of stupid question but is there any way to play videos just by using two pages, I mean like instead of creating an html page for every video I upload to my website, can I have only two php pages and then pass the video from the first page and play on the second one?
I know about passing variables in url but I am kinda confused on how to do this.
I actually embedded my videos from google drive and have a format like
<iframe src="https://docs.google.com/file/d/0B5ZMsnZoUVNSWHhxeGc/preview" width="640" height="385"></iframe>
would it be possible to do like
<a href="play.php?video='test.mp4'>Click to play</a>
or like
$video= 'test.mp4';
<a href="play.php?vid='$video'>Click to play</a>
play.php
$video = $_GET['vid']
if(!empty(($video)){
//then play the video here.
}
I'm expecting you're usnig HTML5.
Let's say there's a list of videos on the first page, and you click on a name of video and it redirects you to second page. Pass the video url (or any identifier for server to know which video) in the link. You can do it like
Cats!!
This will send vid_url as a GET variable on second page. page2.php is, as you can imagine, very simple
<?php
$vid = $_GET['vid'];
//echo $vid;
echo "<video width='320' height='240' controls>
<source src=".$vid." type='video/mp4'>
Your browser does not support the video tag.
</video>"
?>
Of course, you'll do something more elaborate than simply using "echo" for formatted HTML output.
Yes. Instead of empty use isset:
if (isset($_GET["vid"])){
$video = $_GET['vid'];
//play video
}
Play This Video
And On Play.php page add this code
if(isset($_GET['video'])&&!empty($_GET['video']))
{
$Video = $_GET['video'];
// Use the Source of your video anyway you like to play the video
}else
{
echo 'No video selected to play';
}

Get URL from IFrame?

I wanted to allow users to be able to add videos but I'm having an issue with one of the sites.
I will allow videos from Youtube, Vimeo, Vevo, Daily Motion and MTV.
The problem is for MTV. The URL is always different, so I was wondering if there ways to get the source value of an IFrame/embed code.
Example: MTV embed code is:
<div style="background-color:#000000;width:520px;">
<div style="padding:4px;">
<iframe src="http://media.mtvnservices.com/embed/mgid:uma:videolist:mtv.com:1687162/cp~instance%3Dfullepisode%26autoPlay%3Dfalse%26series%3D2211%26seriesId%3D29240%26channelId%3D1%26id%3D1687162%26instance%3Dfullepisode%26uri%3Dmgid%3Auma%3Avideolist%3Amtv.com%3A1687162" width="512" height="288" frameborder="0"></iframe>
<p style="text-align:left;background-color:#FFFFFF;padding:4px;margin-top:4px;margin-bottom:0px;font-family:Arial, Helvetica, sans-serif;font-size:12px;">Get More:
Teen Mom (Season 4), Full Episodes</p></div></div>
Is there a way I can get the IFrame source value out of that complete string?
I just want :
http://media.mtvnservices.com/embed/mgid:uma:videolist:mtv.com:1687162/cp~instance%3Dfullepisode%26autoPlay%3Dfalse%26series%3D2211%26seriesId%3D29240%26channelId%3D1%26id%3D1687162%26instance%3Dfullepisode%26uri%3Dmgid%3Auma%3Avideolist%3Amtv.com%3A1687162
Any help would be appreciated. The other video sources were simple since I was able to use the physical URL itself rather than the embed code.
That's valid HTML, so you can easily parse it with SimpleXML:
$root = simplexml_load_string($embed_code);
$url = (string) $root->div->iframe['src'];
To do this it's quite easy, all you need is this one line of code.
document.getElementById("myFrame").src

Categories