2013-03-09 97 views
0

所以我試圖讓簡單的餡餅YouTube視頻的縮略圖,我的問題是,get_thumbnail()函數似乎並沒有被拉動,因爲get_enclosure功能似乎沒有返回值。獲取YouTube縮略圖了SimplePie

有什麼必須做初始化對象了SimplePie正確獲取機箱?

回答

1

並非所有的飼料支持/使用RSS附件它不是RSS標準的一部分,至少不是原來的RSS標準。這是一種叫做MediaRSS一部分。無論如何,這是可以做到的。另一個問題是谷歌一直在不斷變化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。您最好還是不要從不支持它們的Feed中製作很多縮略圖。