Home / RSS with PHP – Don’t forget to set the Content-Type

RSS with PHP – Don’t forget to set the Content-Type

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.

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.

Feeds should not be served with the "text/html" media type

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.