将图像添加到RSS源的正确方法?

时间:2011-11-29 10:18:27

标签: php mysql xml rss

每个项目一个,而不是整个饲料的标识。

我正在使用php来生成包含db值的feed。 firefox上没有显示任何内容,但如果我打开chrome中的url,我可以在那里看到所有xml输出,对我来说看起来很好。

我不确定的一件事是包括图片,我使用这个网站并实现它所说的:

http://twigstechtips.blogspot.com/2010/10/add-image-to-your-rss-feed-item.html

我也看到了这个问题:RSS feed: to feed images to your rss。这可能是最好的方法,我假设有一个PHP函数来确定图像的文件大小?

我见过的最常见的方式也是关于SO的问题:Images in RSS feed

我已经尝试验证Feed以测试它为什么不在Firefox上显示但是因为它太大了我到目前为止访问过的网站都无法验证它。这可能是因为它没有在Firefox上显示(它曾经在某些时候我曾经在截断CDATA部分并尝试添加图像之前使用过。)

唯一的问题是我不确定它在我的Feed中的位置。请参阅下面的源代码和示例输出:

来源(减去不必要的内容,例如包含等)

header("Content-Type: application/rss+xml; charset=utf-8");

$rssfeed = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$rssfeed .= '<rss version="2.0">' . "\n";
$rssfeed .= '<channel>' . "\n";
$rssfeed .= '<title>RSS feed</title>' . "\n";
$rssfeed .= '<link>http://www.website.co.uk</link>' . "\n";
$rssfeed .= '<description>RSS feed from www.website.co.uk</description>' . "\n";
$rssfeed .= '<language>en-uk</language>' . "\n";
$rssfeed .= '<copyright>Copyright (C) 2011 website.co.uk</copyright>' . "\n\n";

$query = 'SELECT * 
          FROM uk_newsreach_article t1
              INNER JOIN uk_newsreach_article_photo t2
                  ON t1.id = t2.newsArticleID
              INNER JOIN uk_newsreach_photo t3
                  ON t2.newsPhotoID = t3.id
          ORDER BY t1.publishDate DESC;';

$result = mysql_query($query) or die ("Could not execute query");


while($row = mysql_fetch_array($result)) {
    extract($row);

    $rssfeed .= '<item>' . "\n\t";
    $rssfeed .= '<title>' . $row['headline'] . '</title>' . "\n\t";

    $link= 'http://www.website.co.uk';
    $url = ' . $link . '/' . $row['newsArticleID'];

    $rssfeed .= '<link>' . $url . '</link>' . "\n\t";
    $rssfeed .= '<guid>' . $url . '</guid>' . "\n\t";

    $text = $row['text'];
    $rssfeed .= '<description><![CDATA[' . $text . ']]></description>' . "\n\t";
    // $rssfeed .= '<description>' . htmlentities($text) . '</description>' . "\n\t";

    // add image
    $rssfeed .= '<media:thumbnail url="' . $row['mediumURL'] . '" height="' . $row['mediumHeight'] . '" width="' . $row['mediumWidth'] . '" />'. "\n\t";

    $rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($row['publishDate'])) . '</pubDate>' . "\n";
    $rssfeed .= '</item>' . "\n\n";
}

$rssfeed .= '</channel>' . "\n";
$rssfeed .= '</rss>' . "\n";

echo $rssfeed;

示例输出

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>RSS feed</title>
<link>http://www.website.co.uk</link>
<description>RSS feed from www.website.co.uk</description>
<language>en-uk</language>
<copyright>Copyright (C) 2011 website.co.uk</copyright>

<item>
<title>Wear safe but stylish sunglasses on the slopes</title>
<link>http://www.website.co.uk/news/articles/wear-safe-but-stylish-sunglasses-on-the-slopes/800747215</link>
<guid>http://www.website.co.uk/news/articles/wear-safe-but-stylish-sunglasses-on-the-slopes/800747215</guid>
<description><![CDATA[<p>Holidaymakers hitting the slopes this year must remember to pack a pair of sunglasses alongside their skis, one expert has highlighted.</p>
<p>Dharmesh Patel, chairman of the Eyecare Trust, noted that UV protective eyewear is a must for skiers.</p>
<p>&quot;It&#39;s vital that you protect your eyes both when you&#39;re on the slopes and whilst enjoying your apres ski,&quot; he noted.</p>
<p>Mr Patel explained that high altitudes increase UV radiation levels, with fresh snow able to reflect some 80 per cent of these rays.</p>
<p>This means that sunglasses and ski goggles should be able to block out at least 99 per cent of UVA and UVB light.</p>
<p>When choosing the perfect eyewear for a skiing trip, shoppers should look out for the CE Mark or British Standard BS EN 1836:2005 to ensure adequate protection is provided.    </p>
<p>He continued that sunglasses come in either variable tints, such as transition lenses, or with a filter category number from one to four. In this case four is the darkest lens.</p>
<p>&quot;Category four is suitable for exceptionally sunny conditions such as skiing, but beware, as they are illegal for driving in as they do not allow sufficient light transmission through the lens,&quot; noted the expert.</p>
<p>While many skiers opt for goggles to hit the slopes, sunglasses are a popular alternative.</p>
<p>Furthermore, skiers who prefer wearing goggles for the sport should also pack a pair of sunglasses, for a less chunky alternative for other outdoor activities.</p>
<p>Even cloudy days on the slopes require eye protection, as the reflection of light off snow is still significant.</p>
<p>While some sort of eye protection is clearly important on this type of trip, what style should skiers go for?</p>
<p>Those intending to spend more time sipping hot chocolate in the cosy lodge than out on the slopes should go for bold, designer shades, with oversized frames being a popular option.</p>
<p>People who prefer function over fashion on the other hand need to opt for a pair of shades that will stay on, such as Oakleys.</p>
<p>Posted by Martin Carlin</p>]]></description>
<media:thumbnail url="http://www.website.co.uk/img/Sunglasses-are-important-for-skiers-due-to-the-high-levels-of-UV-radiation-they-are-exposed-to_16000539_800747215_0_0_7047638_300.jpg" height="300" width="203" />
<pubDate>Mon, 03 Oct 2011 16:57:09 +0000</pubDate>
</item>
....
....
</channel>
</rss>

2 个答案:

答案 0 :(得分:8)

通过插入CDATA部分进行管理以包含图像,如下所示:

$text = $row['text'];
$image = '<p><img class="center" src="' . $row['mediumURL'] . '"  alt="' . $row['htmlAlt'] . '"/></p>';
$description = $image . $text;

$rssfeed .= '<description><![CDATA[' . $description . ']]></description>' . "\n\t";

http://www.pearsonified.com/2007/06/how-to-format-images-for-feed-readers.php#comment-185597

答案 1 :(得分:5)

&lt; rss version =“2.0”xmlns:media =“http://search.yahoo.com/mrss/”&gt;

http://video.search.yahoo.com/mrss