Posted By

Biocrash on 07/13/12


Tagged

codeigniter


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


RSS Parser


 / Published in: PHP
 

  1. <?php
  2.  
  3. if(!defined('BASEPATH'))
  4. exit('No direct script access allowed');
  5.  
  6. /*
  7.  * This class is written based entirely on the work found below
  8.  * www.techbytes.co.in/blogs/2006/01/15/consuming-rss-with-php-the-simple-way/
  9.  * All credit should be given to the original author
  10.  */
  11.  
  12.  
  13. class RSSParser
  14. {
  15.  
  16.  
  17. // ===================//
  18. //Instance vars //
  19. // ===================//
  20.  
  21. /* Feed URI */
  22. var $feed_uri;
  23.  
  24. /* Associative array containing all the feed items */
  25. var $data;
  26.  
  27. /* Store RSS Channel Data in an array */
  28. var $channel_data;
  29.  
  30. /* Boolean variable which indicates whether an RSS feed was unavailable */
  31. var $feed_unavailable;
  32.  
  33. /* Cache lifetime */
  34. var $cache_life;
  35.  
  36. /* Flag to write to cache - defaulted to false */
  37. var $write_cache_flag = false;
  38.  
  39. /* Code Ignitor cache directory */
  40. var $cache_dir;
  41.  
  42.  
  43.  
  44. // ================ //
  45. // Constructor //
  46. // ================ //
  47. function RSSParser($params)
  48. {
  49. $this->CI = & get_instance();
  50. $this->cache_dir = $this->CI->config->item('rss_cache_path');
  51.  
  52. //$this->cache_dir = '/system/cache';
  53. $this->cache_life = $params['life'];
  54.  
  55. $this->feed_uri = $params['url'];
  56. $this->current_feed['title'] = '';
  57. $this->current_feed['description'] = '';
  58. $this->current_feed['link'] = '';
  59. $this->data = array();
  60. $this->channel_data = array();
  61.  
  62. //Attempt to parse the feed
  63. $this->parse();
  64. }
  65.  
  66.  
  67.  
  68. // =============== //
  69. // Methods //
  70. // =============== //
  71. function parse()
  72. {
  73. //Are we caching?
  74. if($this->cache_life != 0)
  75. {
  76.  
  77. $filename = $this->cache_dir . 'rss_Parse_' . md5($this->feed_uri);
  78.  
  79. //is there a cache file ?
  80. if(file_exists($filename))
  81. {
  82. //Has it expired?
  83. $timedif = (time() - filemtime($filename));
  84. if($timedif < ( $this->cache_life * 60))
  85. {
  86. //its ok - so we can skip all the parsing and just return the cached array here
  87. $this->xml_to_object(implode('', file($filename)));
  88. return true;
  89. }
  90. //So raise the falg
  91. $this->write_cache_flag = true;
  92. }
  93. else
  94. {
  95. //Raise the flag to write the cache
  96. $this->write_cache_flag = true;
  97. }
  98. }
  99.  
  100.  
  101. //Parse the document
  102. $rawFeed = file_get_contents($this->feed_uri);
  103. $this->xml_to_object($rawFeed);
  104.  
  105. //Do we need to write the cache file?
  106. if($this->write_cache_flag)
  107. {
  108. if(!$fp = @fopen($filename, 'wb'))
  109. {
  110. echo 'ERROR';
  111. log_message('error', 'Unable to write cache file: ' . $cache_path);
  112. return;
  113. }
  114. flock($fp, LOCK_EX);
  115. fwrite($fp, $rawFeed);
  116. flock($fp, LOCK_UN);
  117. fclose($fp);
  118. }
  119. return true;
  120. }
  121.  
  122. /* Return the feeds one at a time: when there are no more feeds return false
  123.   * @param No of items to return from the feed
  124.   * @return Associative array of items
  125.   */
  126.  
  127.  
  128.  
  129. function getFeed($num)
  130. {
  131. $c = 0;
  132. $return = array();
  133. foreach($this->data AS $item)
  134. {
  135. $return[] = $item;
  136. $c++;
  137. if($c == $num)
  138. break;
  139. }
  140. return $return;
  141. }
  142.  
  143. /* Return channel data for the feed */
  144.  
  145.  
  146.  
  147. function & getChannelData()
  148. {
  149. if(!empty($this->channel_data))
  150. {
  151. return $this->channel_data;
  152. }
  153. else
  154. {
  155. return FALSE;
  156. }
  157. }
  158.  
  159. /* Were we unable to retreive the feeds ? */
  160.  
  161.  
  162.  
  163. function errorInResponse()
  164. {
  165. return $this->feed_unavailable;
  166. }
  167.  
  168.  
  169.  
  170. function xml_to_object($feed)
  171. {
  172. $xml = new SimpleXmlElement($feed);
  173.  
  174. //Assign the channel data
  175. $this->channel_data['title'] = $xml->channel->title;
  176. $this->channel_data['description'] = $xml->channel->description;
  177. $this->channel_data['lastBuildDate'] = $xml->channel->lastBuildDate;
  178.  
  179. //Build the item array
  180. foreach($xml->channel->item as $item)
  181. {
  182. $data = array();
  183. $data['title'] = $item->title;
  184. $data['description'] = $item->description;
  185. $data['pubDate'] = $item->pubDate;
  186. $data['link'] = $item->link;
  187. $this->data[] = $data;
  188. }
  189.  
  190. //return $this->data;
  191. }
  192.  
  193. }
  194.  

Report this snippet  

You need to login to post a comment.