RSS with PHP - Don't forget to set the Content-Type
Posted December 27th, 2008 in PHP
Just a quick little post to remind that PHP/Apache sets the content-type header by default to text/html. If you are serving other content, such as an RSS feed, you need to remember to change the content type to the appropriate format.
For an RSS feed, which is in XML format, it probably won't cause any issues with feed readers, but to on the safe side it's a good idea to set the content type correctly.
For example, I recently set up a new website (Electric Bookmarks) which has two RSS feeds served by Feedburner. Feedburner grabs the feed from my site and my end-users subscribe to the Feedburner feed to get the info.
When troubleshooting something that was going wrong the validator told me the following error:
This feed is valid, but interoperability with the widest range
of feed readers could be improved by implementing the following
recommendations.
It's a PHP script that serves this particular feed, and I hadn't set the content type myself, so it was defaulting to text/html.
To change it to the corect type of text/xml it's simply a matter of setting the content-type header like so:
header('Content-Type: text/xml');
I made the change, checked the headers were now being served correctly, got the Feedburner validator to check the script again and now it was happy.
Related posts:
- Get the first sentence with PHP (Thursday, January 29th 2009)
- Set the content encoding type with PHP headers (Saturday, December 13th 2008)
- Image headers with PHP (Tuesday, July 22nd 2008)
- 301 redirect with PHP (Saturday, March 15th 2008)
- Replacing relative URLs with absolute URLs in PHP (Thursday, December 6th 2007)

Comments
blog comments powered by Disqus