Reading XML Using PHP (RSS Reader)
This article explains about reading the XML content through a PHP page. As we know, that various sites now a days provide useful information for usage in the form of a RSS feed. Here is the way to use those information and display it in your website easily. This article explains about the step by step implementation of using RSS feed in PHP.
RSS Reader is a simple tool that parses RSS Feeds and present the data in a viewable format. A simple web based RSS Reader can be created using PHP with the help of domxml extensions. As a process of explanation, we are now going to extract information from a RSS feed (http://rss.cnn.com/rss/cnn_world.rss) provided by CNN.
Step 1: Initialize a variable with the rss feed url.
<?
$strRssFeed = "http://rss.cnn.com/rss/cnn_world.rss";
?>
Step 2: Use file function to get the content of rss feed in to an array of string. Convert the string array into single string using implode function and then store the rss xml content in a variable
<?
$xml = implode ('', file($strRssFeed));
?>
Step 3: Create a DOM object for the rss xml document
using the function domxml_open_mem.
<?
$dom = domxml_open_mem($xml);
?>
Step 4: Fetch the root element node of the xml document
using document_element function
<?
$root = $dom->document_element();
?>
Step 5: Fetch the content of xml tags title, description
and link using the get_elements_by_tagname function and store the values
in array variables.
<?
$title = $root->get_elements_by_tagname("title");
$desc = $root->get_elements_by_tagname("description");
$link = $root->get_elements_by_tagname("link");
?>
Step 6: Fetch each values from the array variables and
display
<?
for($i=0;$i<count($title);$i++)
{
echo "<p>";
echo "<a href=\"".$link[$i]->get_content()."\">".$title[$i]->get_content()."</a>";
echo "<br>".$desc[$i]->get_content();
echo "</p>";
}
?>
Download your copy today and try it.Click here to download Rss_Reader.zip
(Size:659 bytes)

