Wednesday, April 9, 2014

How to Fetch Data from RSS Using PHP


<style>
#data{
width:400px;

}
#data h2{
width:100%;
color:#1266ae;
padding:5px;
}
#data p{
border:1px solid #c6c6c6;
padding:5px;
}
#data p img{
width:100%;
}
</style>
<?php
// create a new DOMDocument object
$dom = new DOMDocument();
  //load a remote xml file
  $result = $dom->load("http://blog.lettersandlight.org/rss");
  //did the load work?
  if ($result) {
    //get an array of the items in the rss
    $items = $dom->getElementsByTagName('item');
    //loop through the array
    foreach($items as $item){
      //get the title of the current item
      $title = $item->getElementsByTagName('title')->item(0)->nodeValue;
      //get the description of the current item
      $desc = $item->getElementsByTagName('description')->item(0)->nodeValue;
      //output some of the collected data
 echo("<div id='data'>");
      echo "<h2> $title</h2><br>";
      echo "<p>$desc</p>";
 echo("</div>");
    }
  } else {
    echo "there was a problem reading the rss feed, bummer\n";
  }
?>