-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitterCache.php
63 lines (50 loc) · 1.51 KB
/
twitterCache.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
<?php
/*
Beau West: Twitter Cache
Copyright 2011: [email protected]
*/
class TwitterCache
{
public $Options = array(
'CacheInterval' => 24,
'Path' => 'twitterCache.json',
'twitterUrl' => 'https://twitter.com/statuses/user_timeline/',
'twitterUrlFormat' => 'json',
'twitterUrlOptions' => '?count=5&exclude_replies=1',
'twitterUsername' => 'beaudesigns'
);
public function __construct() {
if(!file_exists($this->Options['Path'])) {
$this->FetchTwitterUpdate();
} else {
$LastAccess = filemtime($this->Options['Path']);
if($LastAccess <= time() - 24) {
$this->FetchTwitterUpdate();
}
}
$Tweets = json_decode(file_get_contents($this->Options['Path']));
if(is_array($Tweets)) {
$this->SendTweetCallback($Tweets[0]->text);
} else {
unlink($this->Options['Path']);
}
}
private function FetchTwitterUpdate() {
$URL = $this->Options['twitterUrl'] . $this->Options['twitterUsername'] . '.' . $this->Options['twitterUrlFormat'] . $this->Options['twitterUrlOptions'];
$Handler = curl_init($URL);
curl_setopt($Handler, CURLOPT_RETURNTRANSFER, TRUE);
$Response = curl_exec($Handler);
$ResponseObject = json_decode($Response);
if(is_array($ResponseObject)) {
$this->SaveTwitterUpdate($Response);
}
}
private function SaveTwitterUpdate($Update) {
file_put_contents($this->Options['Path'], $Update);
}
private function SendTweetCallback($Tweet) {
echo 'tweetBack(' . json_encode($Tweet) . ');';
}
}
header("Content-type: application/x-javascript");
new TwitterCache();