0

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

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

4

1 回答 1

1

并非所有提要都支持/使用 RSS 附件,它不是 RSS 标准的一部分,至少不是原始 RSS 标准。它是称为MediaRSS的一部分。尽管如此,这是可以做到的。另一个问题是 Google 一直在更改GData API,这实际上是为 YouTube 制作 RSS 提要的原因,或者确实如此,您可能希望使用此 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";
}

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

于 2013-03-12T02:04:42.197 回答