Set the length of time (in seconds) that the contents of a feed will be
cached
public set_cache_duration ( integer $seconds = 3600 ) | ||
$seconds | integer | The feed content cache duration |
/** * Display a wildcard in the back end * * @return string */ public function generate() { if (TL_MODE == 'BE') { /** @var \BackendTemplate|object $objTemplate */ $objTemplate = new \BackendTemplate('be_wildcard'); $objTemplate->wildcard = '### ' . utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['rss_reader'][0]) . ' ###'; $objTemplate->title = $this->headline; $objTemplate->id = $this->id; $objTemplate->link = $this->name; $objTemplate->href = '' . $GLOBALS['TL_CONFIG']['backendPath'] . '/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id; return $objTemplate->parse(); } $this->objFeed = new \SimplePie(); $arrUrls = trimsplit('[\\n\\t ]', trim($this->rss_feed)); if (count($arrUrls) > 1) { $this->objFeed->set_feed_url($arrUrls); } else { $this->objFeed->set_feed_url($arrUrls[0]); } $this->objFeed->set_output_encoding(\Config::get('characterSet')); $this->objFeed->set_cache_location(TL_ROOT . '/system/tmp'); $this->objFeed->enable_cache(false); if ($this->rss_cache > 0) { $this->objFeed->enable_cache(true); $this->objFeed->set_cache_duration($this->rss_cache); } if (!$this->objFeed->init()) { $this->log('Error importing RSS feed "' . $this->rss_feed . '"', __METHOD__, TL_ERROR); return ''; } $this->objFeed->handle_content_type(); return parent::generate(); }
public function html() { // Get settings $settings = $this->config; // Define Simplepie $feed = new \SimplePie(); $feed->set_feed_url($settings['feed']); $feed->enable_cache($settings['enable_cache']); $feed->set_cache_location(cache_path()); $feed->set_cache_duration(60 * 60 * 12); $feed->set_output_encoding($settings['charset']); $feed->init(); $title = $settings['title']; $data = []; foreach ($feed->get_items($settings['offset'], $settings['limit']) as $key => $item) { $data[$key]['title'] = $item->get_title(); $data[$key]['permalink'] = $item->get_permalink(); $data[$key]['date'] = $item->get_date(); $data[$key]['updated_date'] = $item->get_updated_date(); $data[$key]['author'] = $item->get_author(); $data[$key]['category'] = $item->get_category(); $data[$key]['description'] = $item->get_description(); $data[$key]['content'] = $item->get_content(); } return $this->view('rssfeed', compact('title', 'data')); }
public function respond() { $url = trim($this->matches[1]); $index = 0; if (isset($this->matches[2])) { $index = intval(trim($this->matches[2])); $index = $index - 1; $index = max(0, $index); } $error_level = error_reporting(); error_reporting($error_level ^ E_USER_NOTICE); $feed = new \SimplePie(); if ($this->cacheEnabled()) { $feed->set_cache_location($this->config['cache_directory']); $feed->set_cache_duration(600); } $feed->set_feed_url($url); $feed->init(); $feed->handle_content_type(); if ($index > $feed->get_item_quantity() - 1) { $index = $feed->get_item_quantity() - 1; } $item = $feed->get_item($index); $result = null; if ($item) { $title = html_entity_decode($item->get_title()); $link = $item->get_permalink(); $date = $item->get_date(); $i = $index + 1; $result = "[{$i}] {$date} - {$title} - {$link}"; } error_reporting($error_level); return $result; }
function fetch_feed2($url) { require_once ABSPATH . WPINC . '/class-feed.php'; $feed = new SimplePie(); $feed->set_sanitize_class('WP_SimplePie_Sanitize_KSES'); // We must manually overwrite $feed->sanitize because SimplePie's // constructor sets it before we have a chance to set the sanitization class $feed->sanitize = new WP_SimplePie_Sanitize_KSES(); $feed->set_cache_class('WP_Feed_Cache'); $feed->set_file_class('WP_SimplePie_File'); $feed->set_feed_url($url); $feed->force_feed(true); /** This filter is documented in wp-includes/class-feed.php */ $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url)); /** * Fires just before processing the SimplePie feed object. * * @since 3.0.0 * * @param object &$feed SimplePie feed object, passed by reference. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged. */ do_action_ref_array('wp_feed_options', array(&$feed, $url)); $feed->init(); $feed->handle_content_type(); if ($feed->error()) { return new WP_Error('simplepie-error', $feed->error()); } return $feed; }
function showBlogLastArticles() { $content = ''; $feed = new SimplePie(); $feed->set_feed_url(array('http://bilboplanet.com/feed/')); $feed->set_cache_duration(600); # $feed->enable_xml_dump(isset($_GET['xmldump']) ? true : false); $success = $feed->init(); $feed->handle_content_type(); if ($success) { $content .= '<div class="box-dashboard"><div class="top-box-dashboard">' . T_('BilboPlanet news - Official Website :') . '</div>'; $content .= '<ul>'; $itemlimit = 0; foreach ($feed->get_items() as $item) { if ($itemlimit == 4) { break; } $content .= '<li>' . $item->get_date('j/m/y') . ' : '; $content .= '<a class="tips" rel="' . $item->get_title() . '" href="' . $item->get_permalink() . '" target="_blank">' . $item->get_title() . '</a>'; $content .= '</li>'; $itemlimit = $itemlimit + 1; } $content .= '</ul></div>'; } return $content; }
function show_news($newsfeed, $newsitems, $newsprefiximage, $newssuffiximage) { $rss = new SimplePie($newsfeed, dirname(__FILE__) . '/cache'); $rss->force_feed(true); // MAKE IT WORK $rss->set_cache_duration(60 * 5); $rss->init(); $rss->handle_content_type(); // if ($rss->error()) { // echo htmlentities($rss->error()); // return; // } foreach ($rss->get_items(0, $newsitems) as $item) { $title = $item->get_title(); $url = $item->get_permalink(); if ($newsprefiximage != '') { echo "<img src='{$newsprefiximage}'>"; } echo "<a href={$url}>{$title}</a>"; if ($newssuffiximage != '') { echo "<img src='{$newssuffiximage}'>"; } echo "<br />"; } }
public function import($forceResync) { if (get_option('goodreads_user_id')) { if (!class_exists('SimplePie')) { require_once ABSPATH . WPINC . '/class-feed.php'; } $rss_source = sprintf(self::$apiurl, get_option('goodreads_user_id')); /* Create the SimplePie object */ $feed = new SimplePie(); /* Set the URL of the feed you're retrieving */ $feed->set_feed_url($rss_source); /* Tell SimplePie to cache the feed using WordPress' cache class */ $feed->set_cache_class('WP_Feed_Cache'); /* Tell SimplePie to use the WordPress class for retrieving feed files */ $feed->set_file_class('WP_SimplePie_File'); /* Tell SimplePie how long to cache the feed data in the WordPress database */ $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', get_option('reclaim_update_interval'), $rss_source)); /* Run any other functions or filters that WordPress normally runs on feeds */ do_action_ref_array('wp_feed_options', array(&$feed, $rss_source)); /* Initiate the SimplePie instance */ $feed->init(); /* Tell SimplePie to send the feed MIME headers */ $feed->handle_content_type(); if ($feed->error()) { parent::log(sprintf(__('no %s data', 'reclaim'), $this->shortname)); parent::log($feed->error()); } else { $data = self::map_data($feed); parent::insert_posts($data); update_option('reclaim_' . $this->shortname . '_last_update', current_time('timestamp')); } } else { parent::log(sprintf(__('%s user data missing. No import was done', 'reclaim'), $this->shortname)); } }
/** * Fetches and parses an RSS or Atom feed, and returns its items. * * Each element in the returned array will have the following keys: * * - **authors** – An array of the item’s authors, where each sub-element has the following keys: * - **name** – The author’s name * - **url** – The author’s URL * - **email** – The author’s email * - **categories** – An array of the item’s categories, where each sub-element has the following keys: * - **term** – The category’s term * - **scheme** – The category’s scheme * - **label** – The category’s label * - **content** – The item’s main content. * - **contributors** – An array of the item’s contributors, where each sub-element has the following keys: * - **name** – The contributor’s name * - **url** – The contributor’s URL * - **email** – The contributor’s email * - **date** – A {@link DateTime} object representing the item’s date. * - **dateUpdated** – A {@link DateTime} object representing the item’s last updated date. * - **permalink** – The item’s URL. * - **summary** – The item’s summary content. * - **title** – The item’s title. * * @param string $url The feed’s URL. * @param int $limit The maximum number of items to return. Default is 0 (no limit). * @param int $offset The number of items to skip. Defaults to 0. * @param string $cacheDuration Any valid [PHP time format](http://www.php.net/manual/en/datetime.formats.time.php). * * @return array|string The list of feed items. */ public function getFeedItems($url, $limit = 0, $offset = 0, $cacheDuration = null) { $items = array(); if (!extension_loaded('dom')) { Craft::log('Craft needs the PHP DOM extension (http://www.php.net/manual/en/book.dom.php) enabled to parse feeds.', LogLevel::Warning); return $items; } if (!$cacheDuration) { $cacheDuration = craft()->config->getCacheDuration(); } else { $cacheDuration = DateTimeHelper::timeFormatToSeconds($cacheDuration); } $feed = new \SimplePie(); $feed->set_feed_url($url); $feed->set_cache_location(craft()->path->getCachePath()); $feed->set_cache_duration($cacheDuration); $feed->init(); // Something went wrong. if ($feed->error()) { Craft: log('There was a problem parsing the feed: ' . $feed->error(), LogLevel::Warning); return array(); } foreach ($feed->get_items($offset, $limit) as $item) { $date = $item->get_date('U'); $dateUpdated = $item->get_updated_date('U'); $items[] = array('authors' => $this->_getItemAuthors($item->get_authors()), 'categories' => $this->_getItemCategories($item->get_categories()), 'content' => $item->get_content(true), 'contributors' => $this->_getItemAuthors($item->get_contributors()), 'date' => $date ? new DateTime('@' . $date) : null, 'dateUpdated' => $dateUpdated ? new DateTime('@' . $dateUpdated) : null, 'permalink' => $item->get_permalink(), 'summary' => $item->get_description(true), 'title' => $item->get_title(), 'enclosures' => $this->_getEnclosures($item->get_enclosures())); } return $items; }
protected function fetch_rss() { include_once ABSPATH . WPINC . '/class-simplepie.php'; $feed = new SimplePie(); $feed->set_feed_url($this->feed_url); $feed->enable_cache(true); $feed->set_cache_location($this->cache_directory); $feed->set_cache_duration($this->cache_duration); $feed->init(); $this->feed = $feed; return $this; }
public function displayfeed() { $this->load->library('simplepie'); $simplepie = new SimplePie(); $simplepie->set_cache_location(APPPATH . '/cache'); $simplepie->set_cache_duration(7200); $simplepie->set_feed_url('http://feeds.feedburner.com/the99percent/DPIn'); $simplepie->init(); // echo $simplepie->get_title(); //$simplepie->handle_content_type(); $data['feed'] = $simplepie; $this->load->view("/feed/listing.php", $data); }
public static function create($cacheEnabled, $cacheDir, $cacheDuration) { $feed = new \SimplePie(); $feed->enable_cache($cacheEnabled); $feed->set_cache_duration($cacheDuration); $feed->set_cache_location($cacheDir); // Create dir if not exists if ($cacheEnabled && !is_dir($cacheDir)) { $fileSystem = new Filesystem(); $fileSystem->mkdir($cacheDir); } return $feed; }
/** * Render a newsletter * @return string the newsletter. */ public function make() { $module = Yii::app()->controller->module; require_once 'protected/vendors/simplepie/autoloader.php'; require_once 'protected/vendors/simplepie/idn/idna_convert.class.php'; $timeLimit = KeyValue::model()->findByPk('newsletter_execution_time')->value; $simplePie = new SimplePie(); $simplePie->set_cache_location('./protected/cache/simplepie'); $simplePie->set_cache_duration(1); // 1 seconde // This makes sure that the content is sent to the browser as // text/html and the UTF-8 character set (since we didn't change it). $simplePie->handle_content_type(); if ($module->multiLang) { if (isset($this->language)) { $feeds = $module->feeds[$this->language]; $renderLanguage = $this->language; } else { $feeds = $module->feeds[Yii::app()->language]; $renderLanguage = Yii::app()->language; } } else { $feeds = $module->feeds; $renderLanguage = Yii::app()->language; } $atLeastOne = false; for ($i = 0; $i < count($feeds); $i++) { if (isset($feeds[$i]['expression'])) { $feeds[$i]['content'] = $this->evaluateExpression($feeds[$i]['expression'], array('timeLimit' => $timeLimit, 'language' => $renderLanguage)); if ($feeds[$i]['content'] != '') { $atLeastOne = true; } } else { $simplePie->set_feed_url($feeds[$i]['url']); $simplePie->init(); $feeds[$i]['link'] = $simplePie->get_permalink(); $feeds[$i]['items'] = array(); foreach ($simplePie->get_items(0, $feeds[$i]['limit']) as $item) { if ($item->get_date('U') > strtotime($timeLimit)) { $feeds[$i]['items'][] = $item; $atLeastOne = true; } } } } if ($atLeastOne) { return Yii::app()->controller->renderPartial('newsletter.components.views.newsletter', array('feeds' => $feeds, 'language' => $renderLanguage), true); } else { return false; } }
static function read($feed) { $parser = new SimplePie(); $parser->set_feed_url($feed); $parser->set_cache_location(storage_path() . '/cache'); $parser->set_cache_duration(100); $success = $parser->init(); $parser->handle_content_type(); if ($parser->error() || !$success) { return null; } else { return $parser->get_items(); } }
public function loadPath($str, $duration) { // declare the namespaces require_once 'simplepie-master/autoloader.php'; $cache = dirname(__FILE__); $cache .= $this->cache; $feed = new SimplePie(); $feed->set_feed_url($str); $feed->set_cache_location($cache); $feed->set_cache_duration($duration); $feed->init(); $feed->handle_content_type(); return $feed; }
/** * Includes the SimplePie RSS file and sets the cache * * @param string $feed * @param bool $cache * @param int $cache_duration * * @return object|false $sp */ public function newSimplePie($feed = '', $cache = RSS_CACHE, $cache_duration = RSS_CACHE_DURATION) { include_once EXTENSIONS . "SimplePie/simplepie.inc"; if ($feed != '') { $sp = new \SimplePie(); $sp->set_feed_url($feed); $sp->set_cache_location(CACHE . "rss_cache/"); $sp->set_cache_duration($cache_duration); $sp->enable_cache($cache === 'true'); $sp->handle_content_type(); return $sp; } return FALSE; }
/** * Make sure that the model is configured with a valid URL. * * @return mixed model data */ function build() { if ($url = $this->config('url')) { $simplepie = new SimplePie(); $simplepie->set_feed_url($this->config('url')); $simplepie->set_cache_location($this->config('cache_directory')); $simplepie->set_cache_duration($this->config('cache_duration')); $simplepie->init(); $simplepie->handle_content_type(); // is this needed? return $simplepie; } else { trigger_error('The ReasonSimplepieFeedModel must be configured with a feed url.', FATAL); } }
function feed($feed_type, $id, $init) { require_once 'inc/simplepie.inc'; $this->load->model('loader_model'); if ($feed_type == 'label') { if ($id == 'all_feeds') { $url = $this->loader_model->get_feeds_all($this->session->userdata('username')); } else { $label = $this->loader_model->select_label($this->session->userdata('username'), $id); $url = $this->loader_model->get_feeds_forLabel($this->session->userdata('username'), $label->label); } } else { $feed = $this->loader_model->select_feed($id); if ($feed->type == 'site') { $url = $feed->url; } else { if ($feed->type == 'keyword') { $url = 'feed://news.google.com/news/feeds?gl=us&pz=1&cf=all&ned=us&hl=en&q=' . $feed->url . '&output=rss'; } } } $feed = new SimplePie($url); $feed->enable_cache(true); $feed->set_cache_location('cache'); $feed->set_cache_duration(600); //default: 10 minutes $feed->set_item_limit(11); $feed->init(); $feed->handle_content_type(); if ($init == "quantity") { echo $feed->get_item_quantity(); } else { if ($init == "discovery") { if ($feed_type == 'label') { $data['entries'] = $feed->get_items(0, 20); } else { $data['entries'] = $feed->get_items(); } $this->load->view('loader/discovery_viewer', $data); } else { $data['entries'] = $feed->get_items($init, $init + 10); $this->load->view('loader/feed_viewer', $data); } } }
function powerpress_get_news($feed_url, $limit = 10) { include_once ABSPATH . WPINC . '/feed.php'; $rss = fetch_feed($feed_url); // If feed doesn't work... if (is_wp_error($rss)) { require_once ABSPATH . WPINC . '/class-feed.php'; // Try fetching the feed using CURL directly... $content = powerpress_remote_fopen($feed_url, false, array(), 3, false, true); if (!$content) { return false; } // Load the content in a fetch_feed object... $rss = new SimplePie(); $rss->set_sanitize_class('WP_SimplePie_Sanitize_KSES'); // We must manually overwrite $feed->sanitize because SimplePie's // constructor sets it before we have a chance to set the sanitization class $rss->sanitize = new WP_SimplePie_Sanitize_KSES(); $rss->set_cache_class('WP_Feed_Cache'); $rss->set_file_class('WP_SimplePie_File'); $rss->set_raw_data($content); $rss->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $feed_url)); do_action_ref_array('wp_feed_options', array(&$rss, $feed_url)); $rss->init(); $rss->set_output_encoding(get_option('blog_charset')); $rss->handle_content_type(); if ($rss->error()) { return false; } } $rss_items = $rss->get_items(0, $rss->get_item_quantity($limit)); // If the feed was erroneously if (!$rss_items) { $md5 = md5($this->feed); delete_transient('feed_' . $md5); delete_transient('feed_mod_' . $md5); $rss->__destruct(); unset($rss); $rss = fetch_feed($this->feed); $rss_items = $rss->get_items(0, $rss->get_item_quantity($num)); $rss->__destruct(); unset($rss); } return $rss_items; }
/** * Create a SimplePie object * @param string $url URL of the RSS feed to parse * @param integer $duration Length of the cache in minutes * @param string $cache_name Name of the cache directory within /cache * @return Object SimplePie object */ public function create($url, $duration = 180, $cache_name = '') { $feed = new SimplePie(); $feed->set_feed_url($url); // Load our own caching driver for SimplePie $feed->registry->call('Cache', 'register', array('ee', 'EE_SimplePie_Cache_driver')); // Establish the cache $feed->set_cache_location('ee:' . $cache_name); $feed->set_cache_duration($duration * 60); // Get parameter to seconds // Check to see if the feed was initialized, if so, deal with the type $success = $feed->init(); $feed->handle_content_type(); if ($success) { return $feed; } throw new Exception("RSS Parser Error: " . $feed->error()); }
function get_rss_feed($url) { require_once ABSPATH . WPINC . '/class-feed.php'; $feed = new SimplePie(); $feed->set_sanitize_class('WP_SimplePie_Sanitize_KSES'); $feed->sanitize = new WP_SimplePie_Sanitize_KSES(); $feed->set_useragent('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'); $feed->set_cache_class('WP_Feed_Cache'); $feed->set_file_class('WP_SimplePie_File'); $feed->set_feed_url($url); $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url)); do_action_ref_array('wp_feed_options', array(&$feed, $url)); $feed->init(); $feed->handle_content_type(); if ($feed->error()) { return new WP_Error('simplepie-error', $feed->error()); } return $feed; }
function feed($feed_url, $options = array()) { $options = array_merge(array('start' => 0, 'length' => 10, 'cache' => true, 'fields' => array('title', 'permalink')), $options); if ($options['cache']) { $items = Cache::read(md5($feed_url)); if ($items !== false) { return $items; } } //make the cache dir if it doesn't exist if (!file_exists($this->cache)) { $folder = new Folder($this->cache, true); } //include the vendor class App::import('vendor', 'simplepie'); //setup SimplePie $feed = new SimplePie(); $feed->set_feed_url($feed_url); // http://simplepie.org/wiki/faq/how_does_simplepie_s_caching_http_conditional_get_system_work $feed->set_cache_location($this->cache); $feed->set_cache_duration(120); //retrieve the feed $feed->init(); //get the feed items $items = $feed->get_items($options['start'], $options['length']); if ($options['cache']) { $cache = array(); foreach ($items as $item) { $holder = array(); foreach ($options['fields'] as $field) { $holder[$field] = $item->{"get_{$field}"}(); } $cache[] = $holder; } return $cache; } //return if ($items) { return $items; } else { return false; } }
/** * Fetches a feed by URL and caches it - using the SimplePie Library. * * @param string $feed_url This is the URL you want to parse. * @param int $cache_duration This is the number of seconds that you want to store the feedcache file for. * @param string $cache_location This is where you want the cached feeds to be stored. */ public static function fetchRSS($feed_url, $number_of_items = null, $cache_duration = null, $cache_location = null) { /** * SimplePie is a bunch of crap, and not e_strict, yet. hmpf! * Therefore we have to cheat with the error_reporting toggle. * @link: http://tech.groups.yahoo.com/group/simplepie-support/message/3289 */ $old_errorlevel = error_reporting(); error_reporting(0); // load simplepie include ROOT_LIBRARIES . 'simplepie/simplepie.inc'; // instantiate simplepie $simplepie = new \SimplePie(); // if cache_location was not specified manually if ($cache_location == null) { // we set it to the default cache directory for feeds $cache_location = ROOT_CACHE; // . 'feeds'; } // if cache_duration was not specified manually if ($cache_duration == null) { // we set it to the default cache duration time of 1800 $cache_duration = 1800; } // if number of items to fetch is null if ($number_of_items == null) { // we set it to the default value of 5 items $number_of_items = 5; } // finally: fetch the feed and cache it! $simplepie->set_feed_url($feed_url); $simplepie->set_cache_location($cache_location); $simplepie->set_cache_duration($cache_duration); $simplepie->set_timeout(5); $simplepie->set_output_encoding('UTF-8'); $simplepie->set_stupidly_fast(true); $simplepie->init(); $simplepie->handle_content_type(); // set old error reporting level error_reporting($old_errorlevel); return $simplepie; }
/** * Fetches and parses an RSS or Atom feed, and returns its items. * * Each element in the returned array will have the following keys: * * - **authors** – An array of the item’s authors, where each sub-element has the following keys: * - **name** – The author’s name * - **url** – The author’s URL * - **email** – The author’s email * - **categories** – An array of the item’s categories, where each sub-element has the following keys: * - **term** – The category’s term * - **scheme** – The category’s scheme * - **label** – The category’s label * - **content** – The item’s main content. * - **contributors** – An array of the item’s contributors, where each sub-element has the following keys: * - **name** – The contributor’s name * - **url** – The contributor’s URL * - **email** – The contributor’s email * - **date** – A {@link DateTime} object representing the item’s date. * - **dateUpdated** – A {@link DateTime} object representing the item’s last updated date. * - **permalink** – The item’s URL. * - **summary** – The item’s summary content. * - **title** – The item’s title. * * @param string $url The feed’s URL. * @param int $limit The maximum number of items to return. Default is 0 (no limit). * @param int $offset The number of items to skip. Defaults to 0. * @param string $cacheDuration Any valid [PHP time format](http://www.php.net/manual/en/datetime.formats.time.php). * * @return array|string The list of feed items. */ public function getFeedItems($url, $limit = 0, $offset = 0, $cacheDuration = null) { $items = array(); if (!extension_loaded('dom')) { Craft::log('Craft needs the PHP DOM extension (http://www.php.net/manual/en/book.dom.php) enabled to parse feeds.', LogLevel::Warning); return $items; } if (!$cacheDuration) { $cacheDuration = craft()->config->getCacheDuration(); } else { $cacheDuration = DateTimeHelper::timeFormatToSeconds($cacheDuration); } // Potentially long-running request, so close session to prevent session blocking on subsequent requests. craft()->session->close(); $feed = new \SimplePie(); $feed->set_feed_url($url); $feed->set_cache_location(craft()->path->getCachePath()); $feed->set_cache_duration($cacheDuration); $feed->init(); // Something went wrong. if ($feed->error()) { Craft::log('There was a problem parsing the feed: ' . $feed->error(), LogLevel::Warning); return array(); } foreach ($feed->get_items($offset, $limit) as $item) { // Validate the permalink $permalink = $item->get_permalink(); if ($permalink) { $urlModel = new UrlModel(); $urlModel->url = $item->get_permalink(); if (!$urlModel->validate()) { Craft::log('An item was omitted from the feed (' . $url . ') because its permalink was an invalid URL: ' . $permalink); continue; } } $date = $item->get_date('U'); $dateUpdated = $item->get_updated_date('U'); $items[] = array('authors' => $this->_getItemAuthors($item->get_authors()), 'categories' => $this->_getItemCategories($item->get_categories()), 'content' => $item->get_content(true), 'contributors' => $this->_getItemAuthors($item->get_contributors()), 'date' => $date ? new DateTime('@' . $date) : null, 'dateUpdated' => $dateUpdated ? new DateTime('@' . $dateUpdated) : null, 'permalink' => $item->get_permalink(), 'summary' => $item->get_description(true), 'title' => $item->get_title(), 'enclosures' => $this->_getEnclosures($item->get_enclosures())); } return $items; }
public function getFeed($refresh = false) { require_once dirname(__FILE__) . "/../../autoloader.php"; $feed = new SimplePie(); $url = "http://www.pinterest.com/taivasogilvy/feed.rss"; $feed->set_feed_url($url); if ($refresh) { $feed->enable_cache(false); } else { $feed->set_cache_location($this->config->cacheFolder); $feed->set_cache_duration($this->config->cacheTime); } $feed->enable_order_by_date(false); $feed->init(); $feed->handle_content_type(); if ($feed->error()) { return false; } else { return $feed; } }
/** * Returns the items for the Feed widget. * * @param string|array $url * @param int $limit * @param int $offset * @return array */ public function getFeedItems($url, $limit = 0, $offset = 0) { $items = array(); if (!extension_loaded('dom')) { Craft::log('Craft needs the PHP DOM extension (http://www.php.net/manual/en/book.dom.php) enabled to parse feeds.', LogLevel::Warning); return $items; } $this->_registerSimplePieAutoloader(); $feed = new \SimplePie(); $feed->set_feed_url($url); $feed->set_cache_location(craft()->path->getCachePath()); $feed->set_cache_duration(craft()->config->getCacheDuration()); $feed->init(); //$feed->handle_content_type(); foreach ($feed->get_items(0, $limit) as $item) { $date = $item->get_date('U'); $dateUpdated = $item->get_updated_date('U'); $items[] = array('authors' => $this->_getItemAuthors($item->get_authors()), 'categories' => $this->_getItemCategories($item->get_categories()), 'content' => $item->get_content(true), 'contributors' => $this->_getItemAuthors($item->get_contributors()), 'date' => $date ? new DateTime('@' . $date) : null, 'dateUpdated' => $dateUpdated ? new DateTime('@' . $dateUpdated) : null, 'permalink' => $item->get_permalink(), 'summary' => $item->get_description(true), 'title' => $item->get_title(), 'enclosures' => $this->_getEnclosures($item->get_enclosures())); } return $items; }
function feed() { if (function_exists('curl_init')) { if (!class_exists('SimplePie')) { App::import('Vendor', 'simplepie/simplepie.inc'); } $feedUrl = "http://www.reviewsforjoomla.com/smf/index.php?board=7.0&type=rss2&action=.xml"; $feed = new SimplePie(); $feed->set_feed_url($feedUrl); $feed->enable_cache(true); $feed->set_cache_location(PATH_ROOT . 'cache'); $feed->set_cache_duration(3600); $feed->init(); $feed->handle_content_type(); $items = $feed->get_items(); $this->set('items', $items); $page = $this->render('about', 'feed'); } else { $page = 'News feed requires curl'; } echo $page; }
function sync_feed($url) { $ALCHEMY_QUOTA = 30000; date_default_timezone_set('GMT'); require_once 'inc/simplepie.inc'; // SimplePie for parsing RSS feeds require_once 'inc/AlchemyAPI.php'; // AlchemyAPI for extracting keywords $this->load->model('feed_model'); //Fetch the RSS feeds using SimplePie $feed = new SimplePie($url); $feed->enable_cache(false); $feed->set_cache_location('cache'); $feed->set_cache_duration(100); //default: 10 minutes $feed->set_item_limit(11); $feed->init(); $feed->handle_content_type(); // For each articles... foreach ($feed->get_items() as $item) { // Parse the data in the article fetched. $permalink = $item->get_permalink(); if (!$this->feed_model->check_aid($permalink)) { // Proceed only if we don't have the same article in our database. $source = $item->get_feed()->get_title(); $title = $item->get_title(); $date = $item->get_date(); $content = $item->get_content(); // Identify the topic of each article using AlchemyAPI, as long as we are under the quota (30000) if ($this->feed_model->check_meter() < $ALCHEMY_QUOTA) { $result = $this->extract_keyword($content, $permalink); // Save the article and the tag associated with the article into our database. $article_id = $this->feed_model->add_article($permalink, 1, $title, $source, $result['category'], $date, $content); $this->feed_model->add_tags($result['tags'], $result['category'], $article_id, $source); } } } return "Sync in progress..."; }
function load($feed_url, $retry = false) { $this->load->library('simplepie'); $f = new SimplePie(); $f->set_cache_duration($this->config->item('simplepie_cache_lifetime')); $f->cache_location = $this->config->item('simplepie_cache'); $f->set_feed_url($feed_url); $f->init(); $f->handle_content_type(); // SimplePie tends to have cache issues, so clear the cache and try one more time // garbage code for garbage environment if ($f->error) if ($retry) return false; else { $this->_clear_cache_file($feed_url); $this->load($feed_url, true); } else return $f; }
public function getFeed($refresh = false) { require_once dirname(__FILE__) . "/../../autoloader.php"; $feed = new SimplePie(); $url = "http://taivasfi.blogspot.fi/feeds/posts/default"; $url = "http://blogi.taivas.fi/?feed=rss2"; $feed->set_feed_url($url); if ($refresh) { $feed->enable_cache(false); } else { $feed->set_cache_location($this->config->cacheFolder); $feed->set_cache_duration($this->config->cacheTime); } $feed->enable_order_by_date(false); $feed->init(); $feed->handle_content_type(); if ($feed->error()) { return false; } else { return $feed; } }
/** * Opens an RSS feed, parses and loads the contents. * * @param string $url The URL of the RSS feed * @param bool $nativeOrder If true, disable order by date to preserve native ordering * @param bool $force Force SimplePie to parse the feed despite errors * * @return object An object that encapsulates the feed contents and operations on those contents. * @throws FeedParserException If opening or parsing feed fails **/ public function parseFeed($url, $nativeOrder = false, $force = false) { require_once PATH_SYSTEM . '/vendors/SimplePie.php'; $feed = new SimplePie(); $feed->set_timeout($this->timeout); $feed->set_feed_url($url); $feed->enable_order_by_date(!$nativeOrder); $feed->force_feed($force); if ($this->cacheDuration != null) { $feed->set_cache_duration(intval($this->cacheDuration)); } if ($this->cacheDirectory != null) { $feed->set_cache_location($this->cacheDirectory); } if ($this->stripHtmlTags != null) { $feed->strip_htmltags($this->stripHtmlTags); } @$feed->init(); if ($err = $feed->error()) { throw new FeedParserException($err); } return $feed; }