【问题标题】:How do I put media:thumbnail in RSS feed如何将媒体:缩略图放入 RSS 提要
【发布时间】:2018-12-24 17:28:55
【问题描述】:

我无法在 RSS 提要项目中显示缩略图。我在哪里将图像的代码放在哪里?

首先尝试了 getElementsByTagName 但当然这不起作用,然后我尝试了您的解决方案 - $item_img = $item->getElementsByTagNameNS('the namespace URI you found','thumbnail') ->item(0)->getAttribute('url'); 但这带来了一个破坏整个页面的错误。我想我知道缩略图是媒体的一个子元素:我看到它是媒体:组下的 jwplayer:feedid 的一部分。那么,它是否像其他项目一样进入数组?我在阵列内部和外部也尝试过。我应该把它放在哪里以在我的新闻源项目中显示缩略图?

<?php
$rss = new DOMDocument();
$rss->load('https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,

        );

    array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<small><em>Posted on '.$date.'</em></small></p>';
    echo '<p>'.$description.'</p>';
}

这是我要解析的 xml 文件输出:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:jwplayer="http://rss.jwpcdn.com/">
<channel>
  <title>Car Repairs</title>
  <description>Car Repairs</description>
  <jwplayer:kind>MANUAL</jwplayer:kind>
  <jwplayer:feedid>IYxiCISJ</jwplayer:feedid>
  <jwplayer:feed_instance_id>7beba58b-b2a2-4000-af11-1e43a7cb8680</jwplayer:feed_instance_id>
  <jwplayer:link rel="first" href="https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss&amp;page_offset=1&amp;page_limit=500"/>
  <jwplayer:link rel="last" href="https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss&amp;page_offset=1&amp;page_limit=500"/>
  <item>
    <title>Preparing the Audi A8 for Service- The American Garage</title>
    <link>https://cdn.jwplayer.com/previews/ybcuKyZl</link>
    <description>Putting Audi A8 in service mode.</description>
    <pubDate>Mon, 17 Sep 2018 14:35:11 -0000</pubDate>
    <guid isPermaLink="false">ybcuKyZl</guid>
    <enclosure url="https://cdn.jwplayer.com/videos/ybcuKyZl-qQFQ3TOZ.mp4" type="video/mp4" length="515"/>
    <jwplayer:feedid>IYxiCISJ</jwplayer:feedid>
      <media:group>
      <media:content url="https://cdn.jwplayer.com/manifests/ybcuKyZl.m3u8" medium="video" type="application/vnd.apple.mpegurl" duration="515"/>
      <media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-jTncGIBU.mp4" medium="video" type="video/mp4" duration="515" width="320" height="180" fileSize="19714361"/>
      <media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-i4o7KXqD.mp4" medium="video" type="video/mp4" duration="515" width="480" height="270" fileSize="29378682"/>
      <media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-XMc5nvLA.mp4" medium="video" type="video/mp4" duration="515" width="720" height="406" fileSize="40249178"/>
      <media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-qQFQ3TOZ.mp4" medium="video" type="video/mp4" duration="515" width="1280" height="720" fileSize="109187664"/>
      <media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-MTvbpSOY.m4a" medium="video" type="audio/mp4" duration="515" fileSize="7304077"/>
      <media:thumbnail url="https://cdn.jwplayer.com/thumbs/ybcuKyZl-720.jpg" width="720" />
      <media:keywords>The American Garage,thermostat,timing belt audi,audi a8,audi 4.2,water pump,service mode,A8,audi,car repairs</media:keywords>
    </media:group>
    <jwplayer:track file="https://cdn.jwplayer.com/strips/ybcuKyZl-120.vtt" kind="thumbnails"/>
  </item>
</channel>
</rss>

【问题讨论】:

    标签: php xml


    【解决方案1】:

    只需将正确的命名空间 URI 传递给 DOMDocument::getElementsByTagNameNS()。 (如果您认为它会改变,您可以从 XML 文档中获取属性,而不是对其进行硬编码。)

    此外,您不能仅通过将 &amp;amp; 替换为 &amp;amp; 来转义 HTML 数据,built-in functions 用于此目的,将它们用于所有数据。

    <?php
    $rss = new DOMDocument();
    $rss->load('https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss');
    // get the namespace URI
    $mediaURL = $rss->documentElement->getAttribute("xmlns:media");
    foreach ($rss->getElementsByTagName('item') as $node) {
        $feed[] = [
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            // get the namespaced element value
            'thumbnail' => $node->getElementsByTagNameNS($mediaURL, 'thumbnail')->item(0)->getAttribute('url'),
        ];
    }
    
    foreach ($feed as $item) {
        $title = htmlspecialchars($item["title"]);
        $link = htmlspecialchars($item["link"]);
        $description = htmlspecialchars($item["desc"]);
        $date = htmlspecialchars(date('l F d, Y', strtotime($item['date'])));
        $thumbnail = htmlspecialchars($item["thumbnail"]);
        echo <<< HTML
    
    <p>
        <a href="$link" title="$title">
            <img src="$thumbnail" alt="$title"/>
            <br/>
            <strong>$title</strong>
        </a>
        <small><em>Posted on $date</em></small>
    </p>
    <p>
        $description
    </p>
    
    HTML;
    }
    

    【讨论】:

    • 谢谢@Miken32,效果很好。我对 PHP 还不是很好。使用 echo 来放置 html 似乎比回显每一行要干净得多。现在我可以使用任何我想要风格化的 html。我用 & 和 &因为那是我之前看到的。另一个问题 - 我试图删除 $limit 行以显示所有可用记录,但它破坏了页面。我在记录数之上设置了一个限制,但它显示了额外的空白记录以将列表填充到该数字。是否有简单的方法使限制等于现有记录的数量?
    • 为什么要从文档中读取 media-rss 命名空间 URI?与别名/前缀不同,它不是可以更改的值。
    • 版本更改可能会导致不同的命名空间 URI。
    • @StephenDevine 代码更新为一个简单的foreach 循环。
    • @miken32 你太棒了!我刚刚开始了解如何更改它,您更新了代码!谢谢你的圣诞礼物!这是到目前为止页面的结果-theamericangarage.us/watch-videos/car-repair
    猜你喜欢
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 2018-04-20
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多