Monday, January 5, 2015

Learn How to Parse RDF and RSS feed Using PHP

RSS stands for Really Simple Syndication, which allows us to syndicate our website content. RDF stands for Resource Description Framework to describe resources on the web. As we know, there are so many online tools for RSS reader. But here, I explain how to parse RDF and RSS feed with the help of Simple PHP function. Here, we will use SimpleXML library of PHP, which converts XML data to an object.

Parse RDF and RSS feed Using PHP

Function for RSS & RDF XML Parser :-

function rss_rdf_parser($feed_url){
 $path_parts = pathinfo($feed_url);
 switch($path_parts['extension']){
 case 'rdf':
 $getContents = file_get_contents($feed_url);
 $rdf_replace = str_replace("rdf:", "", $getContents);  // delete all rdf: values to manipulate input with SimpleXml object
 $arrXml = new SimpleXmlElement($rdf_replace);

 $returnHtml = '<ul>';
 foreach($arrXml->item as $entry){
 $returnHtml .= "<li><a href='".$entry->link."' title='".$entry->title."' target='_blank'>" . $entry->title . "</a></li>";
 }
 $returnHtml .= '</ul>';
 break;

 case 'xml':
 $getContents = file_get_contents($feed_url);
 $arrXml = new SimpleXmlElement($getContents);
 $returnHtml = '<ul>';
 foreach($arrXml->channel->item as $entry){
 $returnHtml .= "<li><a href='".$entry->link."' title='".$entry->title."' target='_blank'>" . $entry->title . "</a></li>";
 }
 $returnHtml .= '</ul>';
 break;
 }
 return $returnHtml;
 }

index.php :-

echo 'RSS Result:'
 $rssResult = rss_rdf_parser("http://rss.dailynews.yahoo.co.jp/fc/economy/rss.xml");
 echo $rssResult;

 echo 'RDF Result:'
 $rdfResult = rss_rdf_parser("http://www.mhlw.go.jp/stf/news.rdf");
 echo $rdfResult;

Source : stepblogging

0 comments:

Post a Comment