I'm having a hard time trying to syndicate my blogs. I want to create an RSS feed via PHP. So far, I have it running through all of the entries and spitting out <title> <link> <description> <time>. My problem is with the header. How do you declare those first few lines of <XML> and <RSS> and what version of RSS am I supposed to be using?!

Dani AI

Generated

Short answer: send an HTTP Content-Type, print the XML prolog, then open the RSS root with a version. For most new feeds use RSS 2.0 (simple and widely supported); consider publishing an Atom 1.0 feed if you need stricter semantics. The XML declaration (for example <?xml version="1.0" encoding="UTF-8"?>) belongs immediately before the <rss> element. (w3.org)

Minimal PHP skeleton (put this where no output has been sent, and beware of BOMs/leading whitespace):

<?php
header('Content-Type: application/rss+xml; charset=UTF-8');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<rss version="2.0">' . "\n";
echo '  <channel>';
echo '    <title>Your Site Title</title>';
echo '    <link>https://example.com/</link>';
echo '    <description>Short description</description>';

// loop your posts
// echo item titles/links/descriptions safely:
echo '    <item>';
echo '      <title>' . htmlspecialchars($title, ENT_XML1, 'UTF-8') . '</title>';
echo '      <link>'  . htmlspecialchars($link,  ENT_XML1, 'UTF-8') . '</link>';
echo '      <description><![CDATA[' . $htmlContent . ']]></description>';
echo '      <pubDate>' . date(DateTime::RFC2822, strtotime($pubDate)) . '</pubDate>';
echo '      <guid>' . htmlspecialchars($guid, ENT_XML1, 'UTF-8') . '</guid>';
echo '    </item>';

echo '  </channel>';
echo '</rss>';

Use ENT_XML1 when escaping for XML and output pubDate in RFC-822/RFC-2822 format. (php.net)

Troubleshooting tips: remove any whitespace or UTF-8 BOM before the XML prolog (XML parsers require the prolog to be the very first bytes), avoid closing ?> in included PHP files so accidental newlines do not leak, and call header() before any echo. If you want safer generation use DOMDocument or XMLWriter so PHP emits the declaration and encoding for you. After changes validate the feed with a feed validator and check the Content-Type (recommended application/rss+xml, application/xml as fallback). (w3.org)

As was already outputting items, compare their output to a working feed like showed, and use the checklist above. For validation and pinpointing issues run your feed through the W3C Feed Validator to catch encoding, date, and element problems. (dev.w3.org)

Recommended Answers

All 4 Replies

Check out my feed from my website. Last time i checked it was a valid RSS feed.

Maybe you can use it as a basic example ;)

Got trouble with your RSS have you now....

Suggest you pop along and read this http://www.xml.com/pub/a/2002/12/18/dive-into-xml.html should answer some of your questions.....or not as the case mybe.

And although this doesn't quite fit your PHP requirements I found this fantastic little program takes all the effort out of XML infact its dam right cheating.

RSS Builder at

You look like you're doing okay. Are you still having trouble?

Alcides.

Well CSCgal - to me, you are an IT Goddess! You have a script that partially works. You actually know something about the required scripting. You probably know what RSS stands for. I'm envious!

I know that RSS is useful, almost mandatory now, but what I need is the complete idiot's guide to setting up RSS so that MY content is syndicated. I've trawled Google at length, but writers can make English read like Swahili.

You know the kind of help I mean....really simple, like;

"You put the left leg in,
you put the left leg out,
you put the left leg in an' shake it all about,
You do the hokey-pokey and you turn around...
That's what it's all about"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.