Read RSS Feeds with SimpleXML

RSS(Really Simple Syndication) is used to distribute updated web content of the website to all over the world. It is generally used by those who updated his work over the web such as blog entries, news bulletins, audios or videos. RSS basically a Standardized XML format which allow publishing the data on the web and easily reading by any programming language Like Java, .NET, PHP etc. RSS provide benefit to there user who want to get the favorite latest information from subscribed website.

Here is an example to read RSS feeds with SimpleXML. SimpleXML is a extension of PHP that allow users to read or manipulate xml data. If you want to use this extension then you have to use PHP5 or grater then.

In this example i have wordpress blog RSS file placed in my local folder. And here is the code:

// Load XML File
$rss = simplexml_load_file('file.xml');
$getChild = $rss->children();

foreach( $rss->children() as $item ){

// Count Total Items in the Files
$total_items = $item->item->count();
for( $i=0; $i < $total_items; $i++ ){
$title = $item->item[$i]->title;

echo $title . '<br />';

// Or You can store that in your
// Database like
/*
$query = mysql_query("insert into tbl_name (name)
value ('" . stripslashes($title) . "')");
if( $query ){
echo "Inserted Successfully! <br>";
}
else{
echo "Error in Query!<br>";
}
*/
}
}

 

in the above example you just echoing the title from the RSS file. If you want to add some information in your database like title, link or whatever you want then follow the below code:

$conn = mysql_connect("localhost", "*******", "*****");
if($conn){
mysql_select_db("test", $conn);
}

// Load XML File
$rss = simplexml_load_file('file.xml');
$getChild = $rss->children();

foreach( $rss->children() as $item ){

// Count Total Items in the Files
$total_items = $item->item->count();
for( $i=0; $i < $total_items; $i++ ){
$title = $item->item[$i]->title;

//echo $title . '<br />';

// Or You can store that in your
// Database like

$query = mysql_query("insert into tbl_name (name)
value ('" . stripslashes($title) . "')");
if( $query ){
echo "Inserted Successfully! <br>";
}
else{
echo "Error in Query!<br>";
}

}
}