doushun9875 2016-06-12 09:10
浏览 78
已采纳

RSS XML显示itunes:PHP中的图像URL

Hello I need some help getting the image URL of the itunes:image attribute from my XML rss feed. I can retrieve everything else in the array, but not 'mainimg'. The current code returns nothing for the image although it's there. Do you have any idea how I can do this, I'm looking for the HREF so I can place it inside 'img' tags.

<?php
    $doc = new DOMDocument();
    $doc->load('http://rss.acast.com/globalpillage');
    $cnt=0;

    foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array ( 
    'maintitle' => $node->getElementsByTagName('title')->item(0)->nodeValue,
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    'enclosure' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url'),
    'mainimg' => $node->getElementsByTagName('image')->item(0)->getAttribute('url')/*This is what I'm trying to call the itunes:image attribute(HREF) with*/,
    );

    ?>
    <div class="showcontent">
        <img src="<?php echo $itemRSS['image']; /*echo the itunes:image attribute(HREF)*/ ?>">
        <h2><a href="<?php echo $itemRSS['link']; ?>"><?php echo $itemRSS['title']; ?></a></h2>
        <p><strong>Published</strong> <?php echo $itemRSS['date']; ?></p>

        <audio controls>
          <source src="<?php echo $itemRSS['enclosure']; ?>" type="audio/mpeg">
        Your browser does not support the audio element.
        </audio>
    </div>

    <?php $cnt ++; } ?>

展开全部

  • 写回答

1条回答 默认 最新

  • duanping5306 2016-06-12 09:34
    关注
    // Get namespace URI for prefix `itunes`
    echo $ns = $s->lookupNamespaceURI('itunes');
    // Get `image` tag from that namespace
    echo $href = $node->getElementsByTagNameNS($ns, 'image')->item(0)->getAttribute('href');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?