forked from mombrea/twitter-api-php-cached
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
executable file
·91 lines (77 loc) · 1.86 KB
/
index.php
File metadata and controls
executable file
·91 lines (77 loc) · 1.86 KB
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
<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
/** Set cache file **/
$tweet_file = 'TweetCache.json';
/** Set cache time in minutes **/
$cache_time = 2;
/** Set access tokens here **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
ReadLatestUpdate();
function ReadLatestUpdate(){
global $tweet_file;
global $cache_time;
if(!file_exists($tweet_file)){
UpdateTimeline();
return;
}
$handle = fopen($tweet_file,'r');
$strUpdateDate = fgets($handle);
fclose($handle);
if(empty($strUpdateDate)){
//file is empty
UpdateTimeline();
}
else{
$updateDate = new DateTime($strUpdateDate);
$now = new DateTime("now");
$since = $updateDate->diff($now);
$minutes = $since->i;
if($minutes > $cache_time){
//reload feed
UpdateTimeline();
}
else{
//read cache
ReadFromCache();
}
}
}
function ReadFromCache(){
global $tweet_file;
$handle = fopen($tweet_file,'r');
$data = fgets($handle); //skip first line
$data = '';
while(!feof($handle)){
$data.= fgets($handle);
}
fclose($handle);
echo $data;
}
function UpdateCache($timeline){
global $tweet_file;
$handle = fopen($tweet_file,'w') or die ('Cannot open cache file');
$data = date('m/d/Y h:i:s a', time())."\r\n".$timeline;
fwrite($handle,$data);
fclose($handle);
}
function UpdateTimeline(){
global $settings;
/** Perform a GET request and echo the response **/
$url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
$getfield = '?count=30';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$timeline = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
//save to cache
UpdateCache($timeline);
//echo results;
echo $timeline;
}