-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtwitter-feed-parser.php
150 lines (112 loc) · 5.02 KB
/
twitter-feed-parser.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Twitter Feed Parser
*
* @version 1.1.2
* @author Dario Zadro
* @link http://zadroweb.com/your-twitter-feed-is-broken/
*
* Notes:
* Caching is used - Twitter only allows 180 queries for every 15 minutes
* See: https://dev.twitter.com/docs/rate-limiting/1.1
* Super simple debug mode will output returned API variable
* --
* Twitter time is displayed (ex. "about 1 hour ago")
*
* Credits:
* Twitter API: https://github.com/J7mbo/twitter-api-php
* Hashtag/Username Parsing: http://snipplr.com/view/16221/get-twitter-tweets/
* Time Ago (modified) Function: http://css-tricks.com/snippets/php/time-ago-function/
*/
// Your Twitter App Settings
// https://dev.twitter.com/apps
$access_token = 'Add_Your_Access_Token';
$access_token_secret = 'Add_Your_Access_Token_Secret';
$consumer_key = 'Add_Your_Consumer_Key';
$consumer_secret = 'Add_Your_Consumer_Secret';
// Some variables
$twitter_username = 'zadroweb';
$number_tweets = 5; // How many tweets to display? max 20
$ignore_replies = true; // Should we ignore replies?
$twitter_caching = true; // You can change to false for some reason
$twitter_cache_time = 60*60; // 1 Hour
$twitter_cache_file = 'tweets.txt'; // Check your permissions
$twitter_debug = false; // Set to "true" to see all returned values
require_once('TwitterAPIExchange.php');
// Settings for TwitterAPIExchange.php
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name='.$twitter_username;
$requestMethod = 'GET';
$settings = array(
'oauth_access_token' => $access_token,
'oauth_access_token_secret' => $access_token_secret,
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret
);
// Flag for twitter error
$tweet_flag = 0;
if (!$twitter_debug) {
// Time the cache was last created
$cache_created_on = ((@file_exists($twitter_cache_file))) ? @filemtime($twitter_cache_file) : 0;
// Output the cache file if valid time
if ( (time() - $twitter_cache_time < $cache_created_on) && $twitter_caching) {
// Tweets should be in cache file, all good
$tweet_flag = 1;
// Get tweets from cache
@readfile($twitter_cache_file);
} else {
// Let's run the API then JSON decode and store in variable
$twitter = new TwitterAPIExchange($settings);
$twitter_stream = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest());
// Check if at least 1 tweet returned from API
if (isset($twitter_stream[0]->text)) {
ob_start(); // Start buffer
$tweets = '<ul class="twitter_stream">'; // Start display element
$tweet_count = 0; // Initialize tweet start count
foreach ($twitter_stream as $tweet){
$tweet_text = htmlspecialchars($tweet->text);
$tweet_start_char = substr($tweet_text, 0, 1);
if ($tweet_start_char != '@' || $ignore_replies == false) {
// Let's create links from hashtags, mentions, urls
$tweet_text = preg_replace('/(https?:\/\/[^\s"<>]+)/','<a href="$1">$1</a>', $tweet_text);
$tweet_text = preg_replace('/(^|[\n\s])@([^\s"\t\n\r<:]*)/is', '$1<a href="http://twitter.com/$2">@$2</a>', $tweet_text);
$tweet_text = preg_replace('/(^|[\n\s])#([^\s"\t\n\r<:]*)/is', '$1<a href="http://twitter.com/search?q=%23$2">#$2</a>', $tweet_text);
// Building tweets display element
$tweets .= '<li>'.$tweet_text.' <span class="twitter_date">'.ago($tweet->created_at,$tweet->id,$tweet->user->screen_name).'</span></li>'."\n";
// Count tweets and quit if necessary
$tweet_count++; if ($tweet_count >= $number_tweets) break;
}
}
echo $tweets.'</ul>'; // End display element
// Write new cache file in the same directory
$file = @fopen(dirname(__FILE__).'/'.$twitter_cache_file, 'w');
// Save contents and flush buffer
@fwrite($file, ob_get_contents());
@fclose($file);
ob_end_flush();
// Tweets present, all good
$tweet_flag = 1;
}
}
} else {
// Let's run the API then JSON decode and store in variable
$twitter = new TwitterAPIExchange($settings);
$twitter_stream = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest());
// Debug mode, just output twitter stream variable
echo '<pre>';
print_r($twitter_stream);
echo '</pre>';
}
// If API didn't work for some reason, output some text
if (!$tweet_flag) {
echo $tweets = '<ul class="twitter_stream twitter_error"><li>Oops, something went wrong with our twitter feed - <a href="http://twitter.com/'.$twitter_username.'/">Follow us on Twitter!</a></li></ul>';
}
// Simple function to get Twitter style "time ago"
function ago($tweet_time,$tweet_id,$tweet_name) {
$m = time()-strtotime($tweet_time); $o='just now';
$t = array('year'=>31556926,'month'=>2629744,'week'=>604800,'day'=>86400,'hour'=>3600,'minute'=>60,'second'=>1);
foreach($t as $u=>$s){
if($s<=$m){$v=floor($m/$s); $o='about '.$v.' '.$u.($v==1?'':'s').' ago'; break;}
}
return '<a href="http://twitter.com/'.$tweet_name.'/statuses/'.$tweet_id.'">('.$o.')</a>';
}