RSS XML 在 PHP 中显示 itunes:image url

RSS XML show itunes:image url in PHP

您好,我需要一些帮助来从我的 XML rss 提要中获取 itunes:image 属性的图像 URL。我可以检索数组中的所有其他内容,但不能检索 'mainimg'。当前代码 returns 没有图像,尽管它在那里。你知道我该怎么做吗,我正在寻找 HREF,这样我就可以将它放在 'img' 标签中。

<?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 ++; } ?>
// 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');

如果有人无意中发现了这一点,我将无法获得评论或答案。我认为评论应该 $doc 而不是 $dom 来模仿示例代码,但这每次只会产生播客的图像,而不是“项目”。它看起来像这样:

echo $href = $doc->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href');

但是当我在循环中更改为$node时,我收到了正确的图像:

echo $href = $node->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href');

此外,我在示例代码中遇到了一些其他拼写错误,例如存储 $item['maintitle']echoing $item['title']

我永远无法得到公认的工作答案。最后,这就是我在抓取 Anchor 播客 RSS 提要时的代码:

<?php
$doc = new DOMDocument();
$doc->load('https://anchor.fm/s/xxxxxxx/podcast/rss');

foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array ( 
        'title' => $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->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href')
    );  

?>
    <div class="showcontent">
        <img src="<?php echo $itemRSS['mainimg']; ?>">
        <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 } ?>