Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct Method For Adding Images To An RSS Feed?

Tags:

php

mysql

xml

rss

As in one per item, not a logo for the feed as a whole.

I am using php to generate my feed with values from a db. Nothing shows on firefox but if I open the url in chrome I can see all the xml output there and it looks fine to me.

One thing I am unsure about is including images, I used this website and implemented what it said:

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

I have also seen this SO question as well: RSS feed: to feed images to your rss . This might be the best way and I assume there's a php function to determine the file size of an image?

The most common way I have seen is also a question on SO: Images in RSS feed

I have tried validating the feed to test why it doesn't show on firefox but because it is so large no website I've visited so far can validate it. Is it possible that this is why it doesn't show on Firefox (it did used to at some point when I truncated the CDATA section and before I tried to add images).

The only thing is I am unsure where it goes in my feed. Please see source code and example output below:

Source (minus unnecessary things like includes etc)

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;

Example Output

<?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>Some Title</title>
<link>http://www.website.co.uk/news/articles/slug</link>
<guid>http://www.website.co.uk/news/articles/slug</guid>
<description><![CDATA[<p>Text</p>
<p>More text</p>
<p>Posted by Firstname Lastname</p>]]></description>
<media:thumbnail url="http://www.website.co.uk/img/image.jpg" height="300" width="203" />
<pubDate>Mon, 03 Oct 2011 16:57:09 +0000</pubDate>
</item>
....
....
</channel>
</rss>
like image 800
martincarlin87 Avatar asked Nov 29 '11 10:11

martincarlin87


2 Answers

Managed to include the images by inserting inside the CDATA section as follows:

$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

like image 176
martincarlin87 Avatar answered Nov 14 '22 21:11

martincarlin87


<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">

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

like image 42
josemimg Avatar answered Nov 14 '22 22:11

josemimg