本文介绍了如何使用php为YouTube使用oembed api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是WordPress 3.8,看到嵌入 API,在WP中,我们可以简单地放置一个链接,它将视频自动嵌入WP中.所以,我的问题是:如何使用嵌入功能?在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

我已经在 Google 中看到了这一点.使用了上面链接中所述的代码:

and i have seen this in Googleused 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,

谢谢您告诉我,

我可以在本地实现此API的方法来测试??

Can i implement this API locally means to test ??

谢谢.

推荐答案

首先,您需要在特殊的URL中传递YouTube视频的ID:

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%3D ID_VIDEO &format=xml

视频示例: https://www.youtube.com/watch?v = l-gQLqv9f4o

我们使用以下网址:

http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3Dl-gQLqv9f4o&format=xml

输出是生成的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>

因此,您所需要做的就是在功能 simplexml_load_file .基本上,您需要读取XML文件才能获取视频的iframe.

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;

?>

您将拥有视频的标题,iframe代码和视频的作者.

You will have the title of the video, the iframe code and the author of the video.

这篇关于如何使用php为YouTube使用oembed api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:15