Rss parser library codeigniter

Posted on November 22 2009 by pradeep

I wrote a codeigniter library through which you can retrieve the rss feeds from xml file

Add the below code in Rss.php file and save the file in application library folder ,

  1. <?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package             CodeIgniter
  8.  * @author              Pradeep Kumar
  9.  * @copyright   Copyright (c) 2009, hyderabad, Inc.
  10.  * @license             http://tech-pundits.com/license.html
  11.  * @link                http://tech-pundits.com
  12.  * @since               Version 1.0
  13.  * @filesource
  14.  */
  15.  
  16. // ————————————————————————
  17.  
  18. /**
  19.  * Rss Class
  20.  *
  21.  * @package             CodeIgniter
  22.  * @subpackage  Libraries
  23.  * @category    Rss Parser
  24.  * @author              Pradeep Kumar
  25.  */
  26. class Rss {
  27.         public $url = ""; // saves the rss feed url
  28.         private $aFiledata = ""; // saves the file data in array format
  29.         private $sFiledata = "";
  30.         private $item = "";
  31.         private $entry = "";
  32.         private $title = "";
  33.         private $link = "";
  34.         private $data = "";
  35.         private $description = "";
  36.         private $date = "";
  37.         private $date_gmt = "";
  38.         private $image = "";
  39.         private $result;
  40.         /**
  41.          * @return unknown
  42.          */
  43.         public function getAFiledata() {
  44.                 return $this->aFiledata;
  45.         }
  46.         /**
  47.          * @return unknown
  48.          */
  49.         public function getEntry() {
  50.                 return $this->entry;
  51.         }
  52.         /**
  53.          * @return unknown
  54.          */
  55.         public function getItem() {
  56.                 return $this->item;
  57.         }
  58.         /**
  59.          * @return unknown
  60.          */
  61.         public function getSFiledata() {
  62.                 return $this->sFiledata;
  63.         }
  64.         /**
  65.          * @return unknown
  66.          */
  67.         public function getTitle() {
  68.                 return $this->title;
  69.         }
  70.         /**
  71.          * @return unknown
  72.          */
  73.         public function getUrl() {
  74.                 return $this->url;
  75.         }
  76.        
  77.         /**
  78.          * @return unknown
  79.          */
  80.         public function getData() {
  81.                 return $this->data;
  82.         }
  83.        
  84.         /**
  85.          * @return unknown
  86.          */
  87.         public function getDescription() {
  88.                 return $this->description;
  89.         }
  90.        
  91.         /**
  92.          * @return unknown
  93.          */
  94.         public function getDate() {
  95.                 return $this->date;
  96.         }
  97.         /**
  98.          * @return unknown
  99.          */
  100.         public function getDate_gmt() {
  101.                 return $this->date_gmt;
  102.         }
  103.        
  104.         /**
  105.          * @return unknown
  106.          */
  107.         public function getImage() {
  108.                 return $this->image;
  109.         }
  110.        
  111.         /**
  112.          * @return unknown
  113.          */
  114.         public function getResult() {
  115.                 return $this->result;
  116.         }
  117.        
  118.         /**
  119.          * @param unknown_type $result
  120.          */
  121.         public function setResult($result) {
  122.                 $this->result = $result;
  123.         }
  124.  
  125.         /**
  126.          * @param unknown_type $image
  127.          */
  128.         public function setImage($image) {
  129.                 $this->image = $image;
  130.         }
  131.  
  132.         /**
  133.          * @param unknown_type $date_gmt
  134.          */
  135.         public function setDate_gmt($date_gmt) {
  136.                 $this->date_gmt = $date_gmt;
  137.         }
  138.        
  139.         /**
  140.          * @return unknown
  141.          */
  142.         public function getLink() {
  143.                 return $this->link;
  144.         }
  145.        
  146.         /**
  147.          * @param unknown_type $link
  148.          */
  149.         public function setLink($link) {
  150.                 $this->link = $link;
  151.         }
  152.  
  153.         /**
  154.          * @param unknown_type $date
  155.          */
  156.         public function setDate($date) {
  157.                 $this->date = $date;
  158.         }
  159.         /**
  160.          * @param unknown_type $data
  161.          */
  162.         public function setData($data) {
  163.                 $this->data = $data;
  164.         }
  165.        
  166.         /**
  167.          * @param unknown_type $description
  168.          */
  169.         public function setDescription($description) {
  170.                 $this->description = $description;
  171.         }
  172.  
  173.         /**
  174.          * @param unknown_type $aFiledata
  175.          */
  176.         public function setAFiledata($aFiledata) {
  177.                 $this->aFiledata = $aFiledata;
  178.         }
  179.        
  180.         /**
  181.          * @param unknown_type $Entry
  182.          */
  183.         public function setEntry($entry) {
  184.                 $this->entry = $entry;
  185.         }
  186.        
  187.         /**
  188.          * @param unknown_type $Item
  189.          */
  190.         public function setItem($item) {
  191.                 $this->item = $item;
  192.         }
  193.        
  194.         /**
  195.          * @param unknown_type $sFiledata
  196.          */
  197.         public function setSFiledata($sFiledata) {
  198.                 $this->sFiledata = $sFiledata;
  199.         }
  200.        
  201.         /**
  202.          * @param unknown_type $Title
  203.          */
  204.         public function setTitle($title) {
  205.                 $this->title = $title;
  206.         }
  207.        
  208.         /**
  209.          * @param unknown_type $Url
  210.          */
  211.         public function setUrl($url) {
  212.                 $this->url = $url;
  213.         }
  214.        
  215.        
  216. /**
  217.          * Constructor
  218.          *
  219.          * @access      public
  220.          * @param       array   initialization parameters
  221.          */
  222.         function Rss($params = array())
  223.         {
  224.                 if (count($params) > 0)
  225.                 {
  226.                         $this->initialize($params);            
  227.                 }
  228.                
  229.                 log_message("debug", "Rss Class Initialized");
  230.         }
  231.        
  232.         // ——————————————————————–
  233.        
  234.         /**
  235.          * Initialize Preferences
  236.          *
  237.          * @access      public
  238.          * @param       array   initialization parameters
  239.          * @return      void
  240.          */
  241.         function initialize($params = array())
  242.         {
  243.                 if (count($params) > 0)
  244.                 {
  245.                         foreach ($params as $key => $val)
  246.                         {
  247.                                 if (isset($this->$key))
  248.                                 {
  249.                                         $this->$key = $val;
  250.                                 }
  251.                         }
  252.                 }
  253.         }
  254.        
  255.        
  256.         function getRss()
  257.         {
  258.                 return $this->run();
  259.                
  260.         }
  261.        
  262.         function run()
  263.         {
  264.         $this->setAFiledata(file($this->getUrl()));
  265.         $this->setSFiledata(implode("", $this->getAFiledata()));
  266.         $this->setSFiledata(str_replace(array ("\r\n", "\r"), "\n", $this->getSFiledata()));
  267.         preg_match_all("|<item [^>]*>(.*?)</item>|is", $this->getSFiledata(), $this->item);
  268.         $items = $this->getItem();
  269.         $this->setItem($items[0]);
  270.         $this->setData("<table>");
  271.         $this->result = array();
  272.            if(empty($this->item))
  273.                 {
  274.                 preg_match_all("|<entry [^>]*>(.*?)</entry>|is", $this->getSFiledata(), $this->item);
  275.                 $items = $this->getItem();
  276.                 $this->setItem($items[0]);
  277.                 $this->setData("</table><table>");
  278.                 $this->result = array();
  279.                 }
  280.                 foreach ($this->getItem() as $item)
  281.                 {
  282.                   preg_match("|<title [^>]*>(.*?)</title>|is", $item, $this->title);
  283.                   $this->setTitle(strip_tags(str_replace(array("< ![CDATA[', ']]>"), array("",""), trim($this->title[1]))));
  284.                   preg_match("|<pubdate [^>]*>(.*?)</pubdate>|is", $item, $this->date_gmt);
  285.         if ($this->date_gmt) {
  286.                   $this->setDate_gmt(strtotime($this->date_gmt[1]));
  287.         } else {
  288.                 preg_match("|<dc :date[^>]*>(.*?)</dc>|is", $item, $this->date_gmt);
  289.         if(isset($this->date_gmt[1]))
  290.                 {
  291.                 $this->setDate_gmt(preg_replace("|([-+])([0-9]+):([0-9]+)$|", "\1\2\3", $this->date_gmt[1]));
  292.                 $this->setDate_gmt(str_replace("T", " ", $this->date_gmt));
  293.                 $this->setDate_gmt(strtotime($this->date_gmt));
  294.                 }
  295.         }
  296.         if($this->getDate_gmt())
  297.                 {
  298.                 $this->setDate_gmt(gmdate("Y-m-d H:i:s", $this->getDate_gmt()));
  299.                 $this->setDate($this->getDate_gmt());
  300.                 }
  301.                 preg_match(‘|<media :content url="(.*?)" />|is’, $item, $this->image);
  302.                 preg_match("|<media :text[^>]*>(.*?)</media>|is", $item, $this->image);
  303.                 $sarr = array("&amp;nbsp;","#60;","&gt;","&lt;");
  304.                 $repl = array("","< ",">","< ");
  305.                 if(isset($this->image[1]))
  306.                         {
  307.                         $this->image = str_replace($sarr,$repl,$this->image[1]);
  308.                         preg_match(‘|<img [^/>]*src="(.*?)"[^>]*/>|is’, $this->image, $this->image);
  309.                         $this->setImage($this->image[1]);
  310.                         }
  311.                         else
  312.                         $this->image = "";
  313.                         preg_match("|<description [^>]*>(.*?)</description>|is", $item, $this->description);
  314.                         if($this->getDescription())
  315.                         {
  316.                         $this->setDescription(str_replace(array("< ![CDATA[', ']]>"), "", trim($this->description[1])));
  317.                         $this->setDescription(str_replace($sarr,$repl,$this->description));
  318.                         }
  319.                         else
  320.                         {
  321.                         preg_match("|<summary [^>]*>(.*?)</summary>|is", $item, $this->description);
  322.                         if($this->getDescription())
  323.                         {
  324.                         $this->setDescription(str_replace(array("< ![CDATA[', ']]>"), "", trim($this->description[1])));
  325.                         $this->setDescription(str_replace($sarr,$repl,$this->description));
  326.                         }
  327.                         else
  328.                         $this->setDescription("");
  329.                         }
  330.                         preg_match("|<link [^>]*>(.*?)</link>|is", $item, $this->link);
  331.                         if(!empty($this->link))
  332.                         {      
  333.                         if(eregi("<media ",$this->link[1]))
  334.                                 {
  335.                                 $img ="";
  336.                         preg_match("|</media><media :thumbnail[^>]*>(.*?)</media>|is", $item, $img);
  337.                         preg_match(‘|<link rel="alternate"[^>]* href="(.*?)">|is’, $item, $this->link);
  338.                         $this->setDescription($img[1].$this->getDescription());
  339.                                 }
  340.                         $this->setLink(str_replace(array("< ![CDATA[', ']]>",‘"/>’), "", trim(strip_tags($this->link[1]))));
  341.                         $this->link;
  342.                         }
  343.                         else
  344.                         {
  345.                         preg_match(‘|</link><link rel="alternate"[^>]* href="(.*?)"/>|is’, $item, $this->link);
  346.                         $this->setLink(str_replace(array(‘< ![CDATA[', ']]>’,‘"/>’), , trim(strip_tags($this->link[1]))));
  347.                                
  348.                         }
  349.                         $this->data .= ‘<tr><td> <a title="’.strip_tags($this->getDescription()).‘" href="’.$this->getLink().‘">’.$this->getTitle().‘</a></td><td>’.$this->getDate().‘</td></tr>’;
  350.         $this->result[] = array(
  351.                                 "title" => $this->getTitle(),
  352.                                 "link"  => $this->getLink(),
  353.                                 "date"  => $this->getDate(),
  354.                                 "image" => $this->getImage(),
  355.                                 "description" => $this->getDescription(),
  356.                                         );     
  357.                 }
  358.                 $this->data .="</link></table>";
  359.                
  360.          return $this->getResult();
  361.         }
  362.  
  363. }
  364. ?>

Add the below code in your controller

  1. <?php
  2. $this->load->libarary("rss");
  3. $config["utl"]="rsslink";
  4. $this->rss->initialize($config);
  5. $result = $this->rss->getRss();
  6. print_r($result);
  7. ?>

Click here to download code


Similar Posts




One Response to “Rss parser library codeigniter”

  1. sapien2 says:

    Dear Author http://www.programmersguide.net !
    I join. I agree with told all above.

Leave a Reply

1 visitors online now
1 guests, 0 members
Max visitors today: 2 at 04:54 am GMT-1
This month: 5 at 09-02-2010 05:49 pm GMT-1
This year: 18 at 07-14-2010 05:15 am GMT-1
All time: 18 at 07-14-2010 05:15 am GMT-1