从rss feed获取enclosure img url

时间:2013-11-12 13:38:10

标签: php rss

我在php中使用rss feed时遇到问题。我想从“封闭”获取img-url但它不起作用。

我刚才的代码:

$rss = simplexml_load_file($url);

$i = 0;
if($rss)
{
$items = $rss->channel->item;
foreach($items as $item)
{
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$phpDate = strtotime($published_on);
$enclosure = $item['enclosure'][0]['url'];

来自RSS:

<enclosure url="http://www.svenskafans.com/image/7/141433/Snalla-Pelle-stanna-i-Gefle.jpg" lenght="51265" type="image/jpeg" />

需要注意的是,有时候没有enclosure-tag,所以即使它丢失也必须有效。

谢谢! 最好的祝福 查尔斯

1 个答案:

答案 0 :(得分:9)

怎么样:

$rss=simplexml_load_file('http://www.svenskafans.com/rss/team/77.aspx');
foreach ($rss->channel->item as $item) {
    if (isset($item->enclosure)) {
        echo $item->enclosure['url'].'<br>';
    }
}

输出:

 http://www.svenskafans.com/image/7/393988/Bilder-fran-tifot-for-Hugo-och-Bernhard.jpg
 http://www.svenskafans.com/image/7/141433/Snalla-Pelle-stanna-i-Gefle.jpg
 http://www.svenskafans.com/image/7/392527/Efter-Gefle-Elfsborg-En-skitmatch-i-regnet-gav-5-insikter.jpg
 http://www.svenskafans.com/image/7/363552/Infor-Gefle-IF-IF-Elfsborg.jpg
 http://www.svenskafans.com/image/7/211783/Gefles-Silly-Season-2013-2014-Angekeepern-Lloyd-Saxton-provtranar-med-Gefle.jpg
 http://www.svenskafans.com/image/7/363058/Gefle-Panelen-17-Pensionera-Hugos-och-Bernhards-trojnummer.jpg
 http://www.svenskafans.com/image/7/328214/Kungsbacksv-24-17-Hoppas-Hugo-satter-en-straff-mot-Elfsborg-i-89e-minuten.jpg
 http://www.svenskafans.com/image/7/192682/Intervju-med-Daniel-Bernhardsson-Gefle-har-en-ljus-framtid.jpg
 http://www.svenskafans.com/image/7/74875/Besked-idag-Bade-Bernhard-och-Hugo-spelar-sin-sista-match-i-Gefle-IF-pa-sondag.jpg
 http://www.svenskafans.com/image/7/343968/Overraskande-piggt-Gefle-nar-Oremo-och-Jawo-natade.jpg
 http://www.svenskafans.com/image/7/330399/Tack-AIK-nu-klart-till-100-att-Gefle-spelar-i-Allsvenskan-2014.jpg
 http://www.svenskafans.com/image/7/363552/Rosta-fram-Gefles-MVP-2013.jpg
 http://www.svenskafans.com/image/7/220468/Par-Asp-berattar-om-tiden-i-Gefle-roligaste-matchen-och-om-att-spela-med-Guidetti.jpg