【问题标题】:Get youtube thumbnail simplepie获取 youtube 缩略图 simplepie
【发布时间】:2013-03-09 03:20:56
【问题描述】:

所以我试图在简单的饼图中获取 youtube 视频的缩略图,我的问题是 get_thumbnail() 函数似乎没有拉它,因为 get_enclosure 函数似乎没有返回任何值。

是否必须做一些事情来初始化 simplepie 对象以正确获取外壳?

【问题讨论】:

    标签: youtube simplepie


    【解决方案1】:

    并非所有提要都支持/使用 RSS 附件,它不是 RSS 标准的一部分,至少不是原始 RSS 标准。它是 MediaRSS 的一部分。尽管如此,这是可以做到的。另一个问题是 Google 一直在更改 GData API,这实际上是为 YouTube 制作 RSS 提要的原因,或者确实如此,您可能希望使用 this API 来代替生成 Atom 提要。您可能想查看一些文档。

    您必须使用 SimplePie 之外的其他代码来为某些提要创建缩略图,我使用了一个名为 simple_html_dom 的东西和另一个名为 thumbnail.php 的脚本来根据需要制作缩略图。如果您有像 Flickr 这样支持 MediaRSS 的提要,您的生活会更好,但如果您必须强制创建缩略图,我使用了以下代码:

    if ($enclosure = $item->get_enclosure())
    {
    // Check to see if we have a thumbnail.  We need it because this is going to display an image.
        if ($thumb = $enclosure->get_thumbnail())
    {
        // Add each item: item title, linked back to the original posting, with a tooltip containing the description.
        $html .= '<li class="' . $item_classname . '">';
        $html .= '<a href="' . $item->get_permalink() . '" title="' . $title_attr . '">'; 
        $html .= '<img src="' . $thumb . '" alt="' . $item->get_title() . '" border="0" />';
        $html .= '</a>';
        $html .= '</li>' . "\n";
    }
    }
    else
    {
    // There are feeds that don't use enclosures that none the less are desireable to dsipaly wide as they contain primarily images
    // Dakka Dakka and some YouTube feeds fall into this category, not sure what is up with Chest of Colors...
    $htmlDOM = new simple_html_dom();
    $htmlDOM->load($item->get_content());
    
    $image = $htmlDOM->find('img', 0);
    $link = $htmlDOM->find('a', 0); 
    
    // Add each item: item title, linked back to the original posting, with a tooltip containing the description.
    $html .= '<li class="' . $item_classname . '">';
    $html .= '<a href="' . $link->href . '" title="' . $title_attr . '">'; 
    // Sometimes I'm not getting thumbnails, so I'm going to try to make them on the fly using this tutorial:
    // http://www.webgeekly.com/tutorials/php/how-to-create-an-image-thumbnail-on-the-fly-using-php/
    $html .= '<img src="thumbnail.php?file=' . $image->src . '&maxw=100&maxh=150" alt="' . $item->get_title() . '" border="0" />';
    $html .= '</a>';
    $html .= '</li>' . "\n";
    }
    

    格式似乎有点奇怪,但这是从我运行 here 的代码中删除的。您仍然最好不要从不支持它们的提要中制作大量缩略图。

    【讨论】:

    • 在您的所有答案中,您都没有包含有助于用户理解的关键信息之一。这就是获取简单 HTML Dom 库的方法:github.com/simplehtmldom/simplehtmldom 您的示例在当前版本中也不起作用。应该使用new HtmlDocument() 而不是new simple_html_dom()
    • 你说得对,我将近 8 岁的答案和代码不再起作用了。谷歌和许多其他人已经改变了他们的 API 和代码库。也许我会去更新它,但它看起来越来越不可能,很忙。
    猜你喜欢
    • 2014-08-05
    • 2011-06-03
    • 2011-10-09
    • 2018-04-15
    • 1970-01-01
    • 2017-02-11
    • 2013-09-11
    • 2013-09-19
    • 2020-08-27
    相关资源
    最近更新 更多