Advertisement
Guest User

Get Wordpress Feeds cache supports

a guest
Sep 25th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. <?php
  2.  
  3. Class RssReader {
  4.    
  5.     public $feedUrl; // Feed URL
  6.  
  7.     public $cache = true; // enable cache
  8.    
  9.     public $cacheTime = 86400; // 1 day
  10.    
  11.    
  12.     public function __construct($feedUrl) {
  13.        
  14.    
  15.         $this->feedUrl = $feedUrl;
  16.        
  17.     }
  18.    
  19.     protected function _getFeedSource() {
  20.        
  21.         if($this->cache) {
  22.             if(!file_exists('feed.xml')) {
  23.                 $this->_downloadFeed();
  24.             } else if(file_exists('feed.xml') && filemtime('feed.xml') <= time() - $this->cacheTime) {
  25.                 $this->_downloadFeed();
  26.             }
  27.         } else {
  28.             $this->_downloadFeed();
  29.         }
  30.        
  31.  
  32.         return 'feed.xml';     
  33.        
  34.     }
  35.    
  36.     protected function _downloadFeed() {
  37.        
  38.         $xml = file_get_contents($this->feedUrl);
  39.         file_put_contents('feed.xml', $xml);
  40.        
  41.     }
  42.    
  43.     public function getRssItems() {
  44.        
  45.         $source = $this->_getFeedSource();
  46.        
  47.         $content = file_get_contents($source);
  48.         $feed = new SimpleXmlElement($content);
  49.        
  50.         return $feed->channel->item ;
  51.        
  52.     }
  53.        
  54. }
  55.  
  56. $reader = new RssReader('https://en.blog.wordpress.com/feed/');
  57. ?>
  58. <div>
  59. <?php foreach($reader->getRssItems() as $item):?>
  60.     <div>
  61.         <?=$item->title;?><br>
  62.         Kategóriák:
  63.         <?php foreach($item->category as $category):?>
  64.             <?=$category;?>
  65.         <?php endforeach;?>
  66.         <br>
  67.         Kép:
  68.         <img src="<?=$item->children('media', true)->thumbnail->attributes();?>" width="150">
  69.         <br>
  70.         <?=$item->link;?><br>
  71.         <?=$item->description;?><br>
  72.     </div>
  73.     <hr>
  74. <?php endforeach;?>
  75. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement