how to use oembed api using php for youtube

一个人想着一个人 提交于 2019-12-24 17:28:28

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/25228744/how-to-use-oembed-api-using-php-for-youtube

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!