获取 Youtube 视频标题、描述和缩略图时出错 [英] Error in Getting Youtube Video Title, Description and thumbnail

查看:30
本文介绍了获取 Youtube 视频标题、描述和缩略图时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从相同的代码中获取 youtube 标题和 youtube 描述,但现在它不起作用我收到以下错误:

I was getting youtube title and youtube description form the same code but now its not working I am getting following error:

警告:DOMDocument::load() [domdocument.load]:http://包装器在服务器配置中被/home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php 中的 allow_url_fopen=0 禁用第 16 行

Warning: DOMDocument::load() [domdocument.load]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

警告:DOMDocument::load(http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY) [domdocument.load]:无法打开流:在/home 中找不到合适的包装器/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php 第 16 行

Warning: DOMDocument::load(http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY) [domdocument.load]: failed to open stream: no suitable wrapper could be found in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

警告:DOMDocument::load() [domdocument.load]:I/O 警告:无法在/中加载外部实体http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY"home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php 第 16 行

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY" in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16

…………………………以下编码用于获取 Youtube 视频数据:

.................................... Following Coding is used to get Youtube Video Data:

$url = "http://gdata.youtube.com/feeds/api/videos/".$embedCodeParts2[0];$doc = 新的 DOMDocument;@$doc->load($url);$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;$videoDescription = $doc->getElementsByTagName("description")->item(0)->nodeValue;

它以前可以工作(此编码在本地服务器中工作正常,但在互联网上无法正常工作)但现在无法正常工作.请指导我如何修复此错误.感谢您抽出宝贵时间.

It was working before (This coding is working fine in Local server but on internet its not working) but now its not working. Please guide me how to fix this error. Thanks for your time.

推荐答案

您服务器的 allow_url_fopen 已禁用(我的也是如此).我感觉到你的痛苦.这就是我所做的.

Your server's allow_url_fopen is disabled (so is mine). I feel your pain. Here's what I did.

尝试使用 cURL,但使用 YouTube 的 v2 以 json 格式返回您的数据接口.您可以通过将该数据附加到您的网址末尾来实现这一点.

Try using cURL, but return your data in json, using YouTube's v2 api. You do that by appending that data to the end of your url.

?v=2&alt=json

您没有发布如何获取 YouTube ID,这可能是问题的一部分(尽管您的示例网址确实有效).所以为了以防万一,我还发布了一个简单的函数来从 YouTube 视频网址中检索 ID.

You didn't post how you're getting your YouTube ID, and that may be a part of the issue (though your sample url did work). So just in case, I'm also posting a simple function to retrieve the ID from the YouTube video url.

function get_youtube_id($url) {
    $newurl = parse_url($url);
    return substr($newurl['query'],2);
}

好的,现在假设您有视频 ID,您可以为要返回的每个字段运行以下函数.

OK, now assuming you have your video id, you can run the following function for each field you wish to return.

// Grab JSON and format it into PHP arrays from YouTube.
// Options defined in the switch. No option returns entire array
// Example of what the returned JSON will look like, pretty, here:
// http://gdata.youtube.com/feeds/api/videos/dQw4w9WgXcQ?v=2&alt=json&prettyprint=true
function get_youtube_info ( $vid, $info ) {
    $youtube = "http://gdata.youtube.com/feeds/api/videos/$vid?v=2&alt=json";
    $ch = curl_init($youtube);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);

    //If $assoc = true doesn't work, try:
    //$output = json_decode($output, true);
    $output = json_decode($output, $assoc = true);

    //Add the ['feed'] in if it exists.
    if ($output['feed']) {
        $path = &$output['feed']['entry'];
    } else {
        $path = &$output['entry'];
    }

    //set up a switch to return various data bits to return.
    switch($info) {
        case 'title':
            $output = $path['title']['$t'];
            break;
        case 'description':
            $output = $path['media$group']['media$description']['$t'];
            break;
        case 'author':
            $output = $path['author'][0]['name'];
            break;
        case 'author_uri':
            $output = $path['author'][0]['uri'];
            break;
        case 'thumbnail_small':
            $output = $path['media$group']['media$thumbnail'][0]['url'];
            break;
        case 'thumbnail_medium':
            $output = $path['media$group']['media$thumbnail'][2]['url'];
            break;
        case 'thumbnail_large':
            $output = $path['media$group']['media$thumbnail'][3]['url'];
            break;
        default:
            return $output;
            break;
    }
    return $output;
}

$url = "http://www.youtube.com/watch?v=oHg5SJYRHA0";
$id = get_youtube_id($url);

echo "<h3><a href=" . $url . ">" . get_youtube_info($id, 'title') . "</a></h3>"; //echoes the title
echo "<p><a href=" . $url . "><img style='float:left;margin-right: 5px;' src=" . get_youtube_info($id, 'thumbnail_small') . " /></a>" . get_youtube_info($id, 'description') . "</p>"; //echoes the description
echo "<br style='clear:both;' /><pre>";
echo print_r(get_youtube_info($id));
echo "</pre>";

这篇关于获取 Youtube 视频标题、描述和缩略图时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆