Witam,

Mam klasę:

  1. <?php
  2. class rss_parser {
  3.   var $update_interval = 60;    
  4.  
  5.   var $data_directory = &#092;"/home/neoprog/www/rss\";
  6.  
  7.   var $rss_url;
  8.   var $num_to_show;
  9.   var $do_update;
  10.   var $tags = array();
  11.   var $content;
  12.   var $rss = array();
  13.  
  14.   var $feed_title;
  15.   var $feed_link;
  16.   var $feed_description;
  17.  
  18.   var $my_html;
  19.  
  20.   function rss_parser($url, $numtoshow = 10, $html = &#092;"\", $update = 0)
  21.   {
  22.     $this->rss_url = $url;
  23.     $this->num_to_show = $numtoshow;
  24.     $this->do_update = $update;
  25.     $this->my_html = preg_replace(&#092;"/(#{.*?):(.*?})/\", \"1__2\", $html); //xx:xx tag workaround
  26.  
  27.     $this->content = $this->fetch_feed();
  28.     $this->parse_feed();
  29.     $this->show();
  30.   }
  31.  
  32.  
  33.   function fetch_feed()
  34.   {
  35.     $url_parts = parse_url($this->rss_url);
  36.  
  37.     $filename = $url_parts['host'] . str_replace(&#092;"/\", \",\", $url_parts['path']) . \"_\" . @$url_parts['query'];
  38.     if(file_exists($this->data_directory . &#092;"/$filename\")) {
  39.       $last = filemtime($this->data_directory . &#092;"/$filename\");
  40.       if(time() - $last > $this->update_interval * 60 || $this->update_interval == 0) {
  41.         $update = 1;
  42.       }
  43.     } else {
  44.         $create= 1;
  45.     }
  46.  
  47.     if($create == 1 || ($this->do_update == TRUE && $update == 1)) {
  48.       $fp = @fsockopen($url_parts['host'], 80, $errno, $errstr, 5);
  49.       if (!$fp) {
  50.         echo &#092;"Couldn't open rss feed {$this->feed_url} in {$_SERVER['PHP_SELF']}
  51. &#092;n\";
  52.         return;
  53.       }
  54.  
  55.       fputs($fp, &#092;"GET \" . $url_parts['path'] . \"?\" . @$url_parts['query'] . \" HTTP/1.0r\n\"
  56.                 .&#092;"Host: \" . $url_parts['host'] . \"r\n\"
  57.                 .&#092;"User-Agent: Drew's RSS Reader 0.1r\n\"
  58.                 .&#092;"Connection: Closer\nr\n\");
  59.  
  60.       while(!feof($fp)) {
  61.         $rss_data .= @fgets($fp, 1024);
  62.       }
  63.  
  64.       list(, $rss_data) = explode(&#092;"r\nr\n\", $rss_data, 2);
  65.  
  66.       $output = @fopen($this->data_directory . &#092;"/$filename\", \"w+\");
  67.       if(!$output) {
  68.         return $rss_data;
  69.       } else {
  70.         flock($output, LOCK_EX);
  71.         fputs($output, $rss_data);
  72.         flock($output, LOCK_UN);
  73.         fclose($output);
  74.       }
  75.     } //update
  76.  
  77.     return file_get_contents($this->data_directory . &#092;"/$filename\");
  78.   }
  79.  
  80.   /* void */
  81.   function parse_feed()
  82.   {
  83.     preg_match(&#092;"//\", $this->content, $title);
  84.     $this->feed_title = @$title[1];
  85.  
  86.     preg_match(&#092;"/(.*?)/\", $this->content, $link);
  87.     $this->feed_link = @$link[1];
  88.  
  89.     preg_match(&#092;"/(.*?)/\", $this->content, $description);
  90.     $this->feed_description = @$description[1];
  91.  
  92.     preg_match_all(&#092;"/]*>(.*?)/s\", $this->content, $items);
  93.     if (sizeof($items[0]) == 0) {
  94.       echo &#092;"No item elements found in rss feed.
  95. &#092;n\";
  96.     }
  97.  
  98.     for($i = 0; $i < sizeof($items[0]); ++$i) {
  99.       preg_match_all(&#092;"/(?:<([w:]*)[^>]*>(?:)?)+?/si\", preg_replace(\"/]*>/\", \"\", $items[0][$i]), $elements);
  100.       for($j = 0; $j < sizeof($elements[0]); ++$j) {
  101.         $elements[1][$j] = str_replace(&#092;":\", \"__\", $elements[1][$j]);  //regex fix for items with : like dc:date
  102.         $this->rss[$i][$elements[1][$j]] = trim(html_entity_decode($elements[2][$j]));
  103.       }
  104.     }
  105.   }
  106.  
  107.  
  108.   function show()
  109.   {
  110.     if($this->my_html == &#092;"\") {
  111.       $this->show_html();
  112.     } else {
  113.       $this->show_user_html();
  114.     }
  115.   }
  116.  
  117.   function show_html()
  118.   {
  119.     $show = (sizeof($this->rss)  > $this->num_to_show ? $this->num_to_show : sizeof($this->rss));
  120.     for($i = 0; $i < $show; ++$i) {
  121. &#092;n\";
  122.     }
  123.   }
  124.  
  125.   function show_user_html()
  126.   {
  127.     $show = (sizeof($this->rss) > $this->num_to_show ? $this->num_to_show : sizeof($this->rss));
  128.     for($i = 0; $i < $show; ++$i) {
  129.       extract($this->rss[$i]);
  130.       $item = preg_replace(&#092;"/#{([^}]+)}/e\", \"$1\", $this->my_html);
  131.       echo $item;
  132.     }
  133.   }
  134.  
  135. } // end class
  136.  
  137.  
  138. ?>


Ok poradziłem już sobie. Można zamknąć.