我无法将缩略图显示在RSS feed中。我应该将图像的代码放在哪里?
首先尝试了getElementsByTagName,但是当然不起作用,然后我尝试了您的解决方案-$ item_img = $ item-> getElementsByTagNameNS('找到的名称空间URI','缩略图') -> item(0)-> getAttribute('url'); 但这带来了一个错误,整个页面都被破坏了。我想我知道缩略图是media的子元素:并且我看到它是media:group下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(' & ', ' & ', $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&page_offset=1&page_limit=500"/>
<jwplayer:link rel="last" href="https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss&page_offset=1&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>
答案 0 :(得分:0)
所需要做的就是将正确的名称空间URI传递给DOMDocument::getElementsByTagNameNS()
。 (如果您认为它会发生变化,则可以从XML文档中获取属性,而不是对其进行硬编码。)
此外,您不能仅通过将&
替换为&
来将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;
}