Parse / Read RSS Feed using PHP

Reading any RSS feed using PHP is not a complex job. RSS feed is also a kind of XML with information defined in a pre-defined format. So the task is simple. We need to read the remote RSS file, parse the XML and display the content. Thats it!!!

I have observed that programmers these days are constantly getting request to display RSS feed on a website home page or some where on a website in a readable form. Most of the request includes display first 3 latest blog entried from a wordpress blog.

Nothing to worry about. Things are simple. Here is the script which will do your work in 5 min.

The RSS XML is parsed and an array is created like $data, you can easily run a for() loop or preferably foreach() loop to display the data.

<?php
function GetXMLTree ($xmldata)
{
// we want to know if an error occurs
ini_set ('track_errors', '1');

$xmlreaderror = false;

$parser = xml_parser_create ('ISO-8859-1');
xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
$xmlreaderror = true;
echo "error";
}
xml_parser_free ($parser);

if (!$xmlreaderror) {
$result = array ();
$i = 0;
if (isset ($vals [$i]['attributes']))
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];

$result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, 'open'));
}

ini_set ('track_errors', '0');
return $result;
}

function GetChildren ($vals, &$i, $type)
{
if ($type == 'complete') {
if (isset ($vals [$i]['value']))
return ($vals [$i]['value']);
else
return '';
}

$children = array (); // Contains node data

/* Loop through children */
while ($vals [++$i]['type'] != 'close') {
$type = $vals [$i]['type'];
// first check if we already have one and need to create an array
if (isset ($children [$vals [$i]['tag']])) {
if (is_array ($children [$vals [$i]['tag']])) {
$temp = array_keys ($children [$vals [$i]['tag']]);
// there is one of these things already and it is itself an array
if (is_string ($temp [0])) {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
} else {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}

$children [$vals [$i]['tag']][] = GetChildren ($vals, $i, $type);
} else
$children [$vals [$i]['tag']] = GetChildren ($vals, $i, $type);
// I don't think I need attributes but this is how I would do them:
if (isset ($vals [$i]['attributes'])) {
$attributes = array ();
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
// now check: do we already have an array or a value?
if (isset ($children [$vals [$i]['tag']])) {
// case where there is an attribute but no value, a complete with an attribute in other words
if ($children [$vals [$i]['tag']] == '') {
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']] = $attributes;
}
// case where there is an array of identical items with attributes
elseif (is_array ($children [$vals [$i]['tag']])) {
$index = count ($children [$vals [$i]['tag']]) - 1;
// probably also have to check here whether the individual item is also an array or not or what... all a bit messy
if ($children [$vals [$i]['tag']][$index] == '') {
unset ($children [$vals [$i]['tag']][$index]);
$children [$vals [$i]['tag']][$index] = $attributes;
}
$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
} else {
$value = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']]['value'] = $value;
$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
}
} else
$children [$vals [$i]['tag']] = $attributes;
}
}

return $children;
}
$url = "http://www.unclecode.com/feed/rss/"; //URL of the XML FEED

$contents = file_get_contents($url);

$data = GetXMLTree ($contents);
echo $data["rss"]["channel"]["item"][0]["title"];
echo "<br>";
echo $data["rss"]["channel"]["item"][0]["link"];
echo "<br>";
echo $data["rss"]["channel"]["item"][0]["pubDate"];
echo "<br>";
echo $data["rss"]["channel"]["item"][0]["dc:creator"];

?>

 

 

Bookmark and Share
Both comments and pings are currently closed.

2 Responses to “Parse / Read RSS Feed using PHP”

  1. Very attention grabbing post – could be old, nevertheless it was new to me. I wished say thanks for this nice post UncleCode.Com – Technology Blog | How to parse remote RSS feed to display within a page using PHP ! I definitely enjoyed reading each bit of it. I have you bookmarked to check out new articles you publish.

  2. UncleCode.Com – Technology Blog | How to parse remote RSS feed to display within a page using PHP is a fairly good article. I simply stumbled upon your weblog and wished to say that I’ve actually enjoyed reading your weblog articles. Any way I’ll be subscribing to your feed and I hope you post once more soon.

n