Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 62a3ec12c80915a489541d7dd4e3e725c59fe71e 0 parents
@erunyon authored
Showing with 133 additions and 0 deletions.
  1. +107 −0 FeedCache.php
  2. +26 −0 README.md
107 FeedCache.php
@@ -0,0 +1,107 @@
+<?php
+/**
+ * Cache remote feeds to improve speed and reliability
+ * Author: Erik Runyon
+ * Updated: 2012-04-25
+ */
+
+class FeedCache {
+ define('ROOT', $_SERVER['DOCUMENT_ROOT']);
+ private $local;
+ private $remote;
+ private $valid_for;
+ private $is_expired;
+ private $is_local;
+ private $data = false;
+
+ public function __construct($local, $remote, $valid_for=3600) {
+ $this->local = ROOT.'cache/'.$local;
+ $this->remote = $remote;
+ $this->valid_for = $valid_for;
+ $this->is_local = $this->check_local();
+ $this->is_expired = $this->check_expired();
+ $this->data = $this->populate_data();
+ }
+
+ public function get_data() {
+ return $this->data;
+ }
+
+ /**
+ * 1. If local file is valid, use it
+ * 2. If it's not, try to cache it
+ * 3. If that fails, use the local even if its expired so we at least have something
+ */
+ private function populate_data() {
+ if( ($this->is_local && !$this->is_expired) || $this->cache_feed() || $this->is_local) {
+ return file_get_contents($this->local);
+ }
+ }
+
+ private function determine_feed() {
+ $file = '';
+ if($this->is_local && !$this->expired) {
+ $file = $this->local;
+ } else {
+ $file = $this->cache_feed() ? $this->local : $this->remote;
+ }
+ return $file;
+ }
+
+ /**
+ * If remote file exists, get the data and write it to the local cache folder
+ */
+ private function cache_feed() {
+ if($this->remote_file_exists($this->remote)) {
+ $compressed_content = '';
+ $remote_content = file_get_contents($this->remote);
+ $compressed_content = preg_replace('/\s*?\n\s*/', "\n", $remote_content);
+ $compressed_content = preg_replace('/( |\t)( |\t)*/', " ", $compressed_content);
+ file_put_contents($this->local, $compressed_content);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ private function check_local() {
+ return ( (is_file($this->local)) && (filesize($this->local) > 500) ) ? true : false;
+ }
+
+ private function check_expired() {
+ if($this->is_local === true) {
+ $valid_until = filemtime($this->local) + $this->valid_for;
+ return $valid_until < mktime();
+ }
+ return true;
+ }
+
+ /**
+ * Check to see if remote feed exists and responding in a timely manner
+ */
+ private function remote_file_exists($url) {
+ $ret = false;
+ $ch = curl_init($url);
+
+ curl_setopt($ch, CURLOPT_NOBODY, true); // check the connection; return no content
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // timeout after 1 second
+ curl_setopt($ch, CURLOPT_TIMEOUT, 2); // The maximum number of seconds to allow cURL functions to execute.
+ curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11');
+
+ // do request
+ $result = curl_exec($ch);
+
+ // if request is successful
+ if ($result === true) {
+ $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ if ($statusCode === 200) {
+ $ret = true;
+ }
+ }
+ curl_close($ch);
+
+ return $ret;
+ }
+
+}
+?>
26 README.md
@@ -0,0 +1,26 @@
+PHP FeedCache
+=============
+
+How to use
+-----------
+
+Include FeedCache.php in your script
+
+Then simply pass in the local file-name (that you want to save it as) and the remote feed:
+
+```php
+ $feed_cache = new FeedCache('local_file.xml', 'http://example.com/feed.xml');
+ $data = simplexml_load_string($feed_cache->get_data());
+```
+You will also need to make sure you have a writable folder called "cache" in the root of your site.
+
+Changelog
+---------
+
+* v1 (2012-04-26)
+** ADD: Initial commit
+
+License
+-------
+
+FeedCache is released under the MIT license. I make no guarantees about the reliability of this script. Use at your own risk.
Please sign in to comment.
Something went wrong with that request. Please try again.