2010-08-16 95 views
1

進出口使用PHP和鵲,並希望在飼料產品檢測圖像的一般方法。我知道一些網站將圖片放置在機箱標籤中,其他人喜歡這個images[rss],有些網站只是將其添加到說明中。是否有任何一個具有通用功能,用於檢測rss項目是否具有圖像,並在經過喜鵲解析後提取圖像url?從RSS/ATOM拉圖像飼料使用鵲RSS

我覺得reqular表達式將需要從描述中提取,但在那些我是個菜鳥。如果可以的話請幫忙。

回答

4

我花了年齡在尋找通過喜鵲RSS顯示圖像自己的一種方式,並在最後我不得不檢查代碼,以弄清楚如何得到它的工作。

像你說的,喜鵲不會在元素拿起圖像的原因是因爲他們使用的是「圈地」的標籤,這是一個空的標籤,其中的信息是在特定的屬性,例如

<enclosure url="http://www.mysite.com/myphoto.jpg" length="14478" type="image/jpeg" /> 

一劈得到它爲我迅速開展工作,我添加以下代碼行到rss_parse.inc:

function feed_start_element($p, $element, &$attrs) { 
    ... 
    if ($el == 'channel') 
    { 
     $this->inchannel = true; 
    } 
    ... 

    // START EDIT - add this elseif condition to the if ($el=xxx) statement. 
    // Checks if element is enclosure tag, and if so store the attribute values 
    elseif ($el == 'enclosure') { 
     if (isset($attrs['url'])) { 
     $this->current_item['enclosure_url'] = $attrs['url']; 
     $this->current_item['enclosure_type'] = $attrs['type']; 
     $this->current_item['enclosure_length'] = $attrs['length']; 
     } 
    } 
    // END EDIT 
    ... 
} 

的URL圖像是$ myRSSitem [「enclosure_url」 ],大小在$ myRSSitem ['enclosure_length']中。 注意機箱標籤可以指多種類型的介質,所以首先檢查,如果該類型實際上是通過檢查$ myRSSitem [「enclosure_type」]的圖像。

也許別人有更好的建議,我敢肯定這可以做得更優雅,從其他空標籤中獲取屬性,但我需要快速修復(截止期限壓力),但我希望這可能有助於其他人困難!

+0

該解決方案完美地工作爲我自己凌亂的情況。非常感謝你。 – 2012-03-24 17:04:49