RSS-PHP; a free RSS and XML Parser for PHP 5+
RSS-PHP is now more of an XML-PHP - you can use it to quickly interface with ANY XML document; Further, you can now also convert any Array to XML.
RSS_PHP Introduction
RSS_PHP is a free RSS Parser and XML Parser for PHP 5+, utilising the DOM to parse any valid XML Document including RSS, with provisional support for ATOM feeds.
Unlike many RSS Parsers, RSS_PHP also gives you full access to every part of the original RSS feed; including RSS attributes, RSS extensions, XML namespaces, XML stylesheets and node values.
I've specifically kept this RSS Parser as simple as possible. Here's a working example which will parse a full RSS feed, it'll also automatically convert the XML Document to UTF-8 from any other encoding:
<?php
$rss = new rss_php;
$rss->load('http://rssphp.net/rss.xml');
$items = $rss->getItems(); #returns all rss items
?>
IMPORTANT: A major peice of relatively unknown functionality is that RSS_PHP returns the actual DOMElements from the raw DOMDocument (although they have been modified slightly) - meaning that from any returned element you can access the full DOMDocument and have access to all the normal PHP DOM properties and methods.
Full documentation and examples of RSS_PHP are available on the site, or you can download the latest version of RSS_PHP for all your RSS & XML Parsing needs for free.
RSS_PHP v3 released
With great joy v3 of the RSS Parser / XML Parser for PHP [rss_php] is now released and available for download. This release fixes a couple of tiny bugs and adds far more functionality (its now more of an XML-PHP ;)
- Full XML Parser, parse anything XML Based
- Array to XML conversion, convert XML to Arrays and Arrays to XML [array to xml example]
- XML Modification, turn XML into an array, change values and write the XML back out, preserving the original formatting
- Automatic XML Encoding Detection and conversion to UTF-8
- XPath support and functionality
- Greater flexibility with the addition of getElementsByTagName and getValuesByTagName
- Preliminary ATOM feed support
- Support of XML Namespaces and DOM Processing Instructions (XML Style-sheets)
- Custom Integrated HTTP Transport Handler
IMPORTANT: getChannel() function deprication.
In version 3 of RSS_PHP the RSS Parser specific function getChannel() has been depricated; users are encouraged to use any of query('xpath/here'), getElementsByTagName('tagname') or getValuesByTagName('tagname') instead.
RSS_PHP Information
I've spent years using XML in all it's formats, within that time I've seen literally thousands of people ask how can I parse RSS with PHP?
; and the majority of the time the answer has been quite simply: use REGEX
.
Personally, I think REGEX is fine solution, I've even used it to built my own XML & Markup parsers and established PHP frameworks on them, however the aim should be to keep things Really Simple, seeing as RSS isn't exactly the most complicated XML document to date.
Thankfully PHP (5+) already gives us everything we need to get started parsing RSS and XML, indeed it negates the need for REGEX or third-party extensions such as expat in this context.
Parsing RSS and XML in PHP using DOM Functions
With PHP 5 came the intoduction of the DOM Extension; to quote directly from the source:
The DOM extension allows you to operate on XML documents through the DOM API with PHP 5.
Rather than using any third party modules, or custom XML parsers like many other RSS parsers; RSS_PHP makes good use of the aforementioned DOM Extension.
RSS_PHP Dependencies
As RSS_PHP utilises the DOM to parse RSS, and the DOM Extension is installed as part of the PHP Core, RSS_PHP's only dependency is PHP 5.
RSS_PHP Internationalization and Character Encoding Support
RSS_PHP now features automatic XML Encoding Detection and conversion to UTF-8