所以我试着在简单的饼图中获取youtube视频的缩略图,我的问题是get_thumbnail()函数似乎没有拉它,因为get_enclosure函数似乎没有返回值。
是否必须执行某些操作才能初始化simplepie对象以正确获取封装?
发布于 2013-03-12 10:04:42
并不是所有的提要都支持/使用RSS封装,它不是RSS标准的一部分,至少不是原始的RSS标准。它是称为MediaRSS的东西的一部分。然而,这是可以做到的。另一个问题是谷歌一直在改变GData API,它实际上是为YouTube制作instead的,或者它确实改变了,你可能想使用this API来代替它来产生Atom feed。您可能需要查看一些文档。
您必须使用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的代码中抄袭过来的。最好还是不要从不支持缩略图的提要中创建大量缩略图。
https://stackoverflow.com/questions/15306738
复制相似问题