XML_RSS --
Parsing RSS files with PHP.
Introduction to RSS
Resource Description Framework (RDF) Site Summary (RSS) documents
are XML documents, that provide a lightweight multipurpose
extensible metadata description and syndication format. RSS files
are often used to syndicate news or headlines from portal sites
(e.g. Slashdot or freshmeat.net).
For more information on RSS see the website of the RSS working
group (http://www.purl.org/rss/).
Beispiel 1. RSS file <?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://my.netscape.com/rdf/simple/0.9/">
<channel>
<title>FooBar Inc.</title>
<link>http://example.com/</link>
<description>abcd, xyz, 123</description>
</channel>
<image>
<title>FooBar</title>
<url>http://example.com/images/rssicon.gif</url>
<link>http://example.com/</link>
</image>
<item>
<title>Headline 1</title>
<link>http://example.com/news.php?h=1</link>
</item>
<item>
<title>Headline 2</title>
<link>http://example.com/news.php?h=2</link>
</item>
<textinput>
<title>Search FooBar Inc.</title>
<description>Search FooBar Inc. headlines</description>
<name>q</name>
<link>http://example.com/search.php</link>
</textinput>
</rdf:RDF> |
|
Usage Example
The following script fetches the latest headlines from
Slashdot.org via RSS.
Beispiel 2. Fetching headlines from Slashdot require_once "XML/RSS.php";
$rss =& new XML_RSS("http://slashdot.org/slashdot.rdf");
$rss->parse();
echo "<h1>Headlines from <a href=\"http://slashdot.org\">Slashdot</a></h1>\n";
echo "<ul>\n";
foreach ($rss->getItems() as $item) {
echo "<li><a href=\"" . $item['link'] . "\">" . $item['title'] . "</a></li>\n";
}
echo "</ul>\n"; |
|
XML_RSS::XML_RSS
This function is the constructor for the RSS parser. It takes
on argument, $handle. This parameter
defines the resource, where the RSS data is reachable. It can
be either a string that represents an URL or a local filename
or it can be a file handle to an already opened file.
XML_RSS::parse
This function has to be called in order to parse the content
of the RSS file that has been loaded via XML_RSS:XML_RSS
().
XML_RSS::getStructure
This function returns an array containing all elements of the
RSS file in the same order, than they were ordered in the original
file.
Beispiel 3. Output of getStructure() with the
above
example RSS file: Array
(
[0] => Array
(
[type] => channel
[title] => FooBar Inc.
[link] => http://example.com/
[description] => abcd, xyz, 123
)
[1] => Array
(
[type] => image
[title] => FooBar
[url] => http://example.com/images/rssicon.gif
[link] => http://example.com/
)
[2] => Array
(
[type] => item
[title] => Headline 1
[link] => http://example.com/news.php?h=1
)
[3] => Array
(
[type] => item
[title] => Headline 2
[link] => http://example.com/news.php?h=2
)
[4] => Array
(
[type] => textinput
[title] => Search FooBar Inc.
[description] => Search FooBar Inc. headlines
[name] => q
[link] => http://example.com/search.php
)
) |
|
XML_RSS::getChannelInfo
This function returns an array containing the information
that has been extracted from the <channel>-tag while parsing
the RSS file.
XML_RSS::getItems
This function returns an array containing the set of items
that are provided by the RSS file.
XML_RSS::getImages
This function returns an array containing the set of images
that are provided by the RSS file.
XML_RSS::getTextinputs
This function returns an array containing the set of text input
fields that are provided by the RSS file.