What is RSS, and what does RSS stand for?
The RSS Parser
Outputting the Feed
Specific implementations
The end result of having this array is being able to output the RSS feed in whatever format you choose. A fairly generic way would be to use the following code to print out a single item:
<?php
# set $item to be one of the items in the ITEMS array
$item = $myrssfeed->rssarray['ITEMS'][0];
?>
<p><a href="<?php echo $item['LINK']; ?>" target="extnews"><?php echo $item['TITLE']; ?></a><br />
<?php echo $item['DESCRIPTION']; ?></p>
This would produce the following HTML:
<p><a href="http://www.gnpcb.org/esv/search/?passage=Proverbs+3%3A11-12" target="extnews">Proverbs 3:11-12</a><br />
My son, do not despise the LORD's discipline or be weary of his reproof, for the LORD reproves him whom he loves, as a father the son in whom he delights.</p>
Since the RSS items are stored in the ITEMS array, it is simply a matter of looping through this array to display them all:
<?php
while (list($key,$item) = each($myrssfeed->rssarray['ITEMS'])) {
# code to display each item: $item['TITLE'], $item['LINK'], $item['DESCRIPTION']
}
?>
Many RSS feed providers ask that you acknowledge them as the source of your information, by providing a link to their site, which is usually included in the feed for you convenience:
<p>This feed is provided courtesy of <a href="<?php echo $myrssfeed->rssarray['LINK']; ?>" target="extnews"><?php echo $myrssfeed->rssarray['TITLE']; ?></a></p>
The PHP code that handles the XML namespace tags makes use of some functions that are (listed but) not documented in the PHP manual.
For normal tags, the execution order is quite simple:
Start of tag (eg LINK) -> function startElement()
End of tag (eg LINK) -> function endElement()
For namespace tags, the execution order of these functions is shown below:
Start of namespace tag (eg BODY) -> function nsstartElement() -> function startElement()
End of namespace tag (eg BODY) -> function nsendElement() -> function endElement()
These handlers are defined as below:
xml_set_start_namespace_decl_handler($this->parser, "nsstartElement");
xml_set_end_namespace_decl_handler($this->parser, "nsendElement");
The other key function, to make use use of the namespace functionality of the expat extension, is the xml_parser_create_ns() function (used instead of the xml_parser_create() function).
Paul Davey
Paul Davey is an administrator here at CWM and is the webmaster of Whitford Church in Western Australia.