forked from Leacocks/Landing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed-cache-twitter.php
131 lines (114 loc) · 3.74 KB
/
feed-cache-twitter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
date_default_timezone_set('America/New_York');
//echo ini_get('display_errors');
//
//if (!ini_get('display_errors')) {
// ini_set('display_errors', 'all');
//}
//echo ini_get('display_errors');
/**
* Cache remote feeds to improve speed and reliability
* Author: Erik Runyon
* Modified by: Parker Moore
* Updated: 2012-10-08
*/
class FeedCache {
private $local;
private $remote;
private $valid_for;
private $is_expired;
private $is_local;
private $data = false;
public function __construct($local, $remote, $valid_for=600) {
$this->local = dirname(__FILE__).'/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 ) {
return file_get_contents($this->local);
} else if( $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 = '';
// grab contents of feed from
$ch = curl_init($this->remote);
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');
$remote_content = curl_exec($ch);
curl_close($ch);
$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;
}
}
header("Content-type: text/xml; charset=utf-8");
ob_start();
$feed_cache = new FeedCache('cache_twitter.xml', 'http://api.twitter.com/1/statuses/user_timeline.rss?count=1&user_id=192014565');
if (substr(trim($feed_cache->get_data()), 0, 5) == "<?xml") {
echo $feed_cache->get_data();
} else {
echo "";
}
ob_flush();