获取youtube缩略图simplepie [英] Get youtube thumbnail simplepie

查看:100
本文介绍了获取youtube缩略图simplepie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图以简单的方式获取youtube视频的缩略图,但我的问题是get_thumbnail()函数似乎并未将其提取出来,因为get_enclosure函数似乎未返回任何值.

是否有必要做一些事情来初始化simplepie对象以正确获得机柜?

解决方案

并非所有提要都支持/使用RSS附件,它不是RSS标准的一部分,至少不是原始RSS标准的一部分.它是名为 MediaRSS 的一部分.仍然可以做到这一点.另一个问题是Google一直在更改 GData API ,这实际上是为YouTube制作RSS feed的原因,或者是,您可能想使用此API 来生成Atom供稿.您可能想看看一些文档.

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

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";
}

格式似乎有点奇怪,但这是从我的运行代码的此处撕下来的.您仍然最好不要从不支持它们的供稿中制作很多缩略图.

So I'm trying to get a thumbnail of a youtube video in simple pie, my problem is that the get_thumbnail() function doesn't seem to be pulling it because the get_enclosure function seems to be returning no values.

Is there something that must be done to initialize the simplepie object to get the enclosure properly?

解决方案

Not all feeds support/use RSS enclosures it isn't part of the RSS standard at least not the original RSS standard. It is part of something called MediaRSS. None the less this can be done. Another problem is Google has been changing GData API which is what actually makes the RSS feeds for YouTube or it did, you might want to use this API instead which produces Atom feeds. You probably want to look at some documentation.

You have to use additional code beyond SimplePie to create a thumbnail for some feeds, I used something called simple_html_dom and another script called thumbnail.php to make thumbnails as necessary. Your life is better if your have a feed like Flickr which supports MediaRSS but if you gotta force the creation of a thumbnail, I used this code:

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";
}

The formatting seems a bit odd, but that is ripped right from my code which is running here. You're still best off not making a lot of thumbnails from a feed that doesn't support them.

这篇关于获取youtube缩略图simplepie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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