Twitter API mit OAuth

PHP  (12.06.2010 14:51) Eine Klasse die Tweets von einem Twitter Account mit OAuth ausliest.
Die EPITwitter Libs werden hier benötigt: http://wiki.github.com/jmathai/twitter-async/

Quellcode (ausblenden | aufklappen)

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
<?php
 
include_once(ABS_PATH.'modules/twitter/oauth/EpiCurl.php');
include_once(ABS_PATH.'modules/twitter/oauth/EpiOAuth.php');
include_once(ABS_PATH.'modules/twitter/oauth/EpiTwitter.php');
 
class module_twitter extends EpiTwitter {
 
   private $now;
   private $last_cached;
   private $cache_expire_time;
   private $cache_file;
   private $cache_data;
   private $max_entries;
   private $twitter_account_name;
 
   function  __construct($file) {
      if(defined("TWITTER_CONSUMER_KEY") && defined("TWITTER_CONSUMER_SECRET") && defined("TWITTER_USER_TOKEN") && defined("TWITTER_USER_SECRET")) {
         parent::__construct(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_USER_TOKEN, TWITTER_USER_SECRET);
      } else {
         trigger_error("Twitter API Keys nicht gefunden!", E_USER_ERROR);
      }
      if(file_exists($file) && is_readable($file) && is_writable($file)) {
         $this->cache_file = $file;
      } else {
         trigger_error("Cache Datei nicht gefunden!", E_USER_ERROR);
      }
 
      $this->now = time();
      $this->cache_data = new stdClass();
      $this->max_entries = 10;
      $this->cache_expire_time = 10;
      $this->twitter_account_name = "";
      //$this->refresh_cache();
      $this->read_cache();
   }
 
 
   public function set_max_entries($i) {
      if(((int)$i)>0) {
         $this->max_entries = ((int)$i);
         return true;
      } else {
         return false;
      }
   }
 
   public function set_cache_expire_time($i) {
      if(((int)$i)>=0) {
         $this->cache_expire_time = ((int)$i);
         return true;
      } else {
         return false;
      }
   }
   
   
   
   public function is_cache_expired() {
      if(($this->last_cached+$this->cache_expire_time*60)<$this->now) {
         return true;
      } else {
         return false;
      }
   }
 
 
 
   public function read_cache() {
      $this->cache_data = json_decode(file_get_contents($this->cache_file));
      if($this->cache_data===null) {
         $this->cache_data = new stdClass();
         $this->last_cached = 0;
         $this->twitter_account_name = false;
      } else {
         $this->last_cached = $this->cache_data->last_cached;
         $this->twitter_account_name = $this->cache_data->username;
         $this->cache_data = $this->cache_data->entries;
      }
      return true;
   }
 
   public function refresh_cache() {
      $timeline = json_decode($this->get('/statuses/user_timeline.json?count='.$this->max_entries)->responseText);
      if(isset($timeline->error)) {
         return false;
      } else {
         $this->cache_data = new stdClass();
         $this->cache_data->last_cached = time();
         $this->cache_data->username = $timeline[0]->user->screen_name;
         $this->cache_data->entries = $timeline;
         file_put_contents($this->cache_file, json_encode($this->cache_data));
         $this->read_cache();
         return true;
      }
   }
 
 
 
   public function get_account_name() {
      return $this->twitter_account_name;
   }
 
   public function get_cache() {
      return $this->cache_data;
   }
 
   public function get_cache_expire_time() {
      return $this->cache_expire_time;
   }
 
   public function set_update($text) {
      if(strlen($text)>0 && strlen($text)<=140) {
         $this->post("/statuses/update.json?status=".urlencode($text));
         return true;
      } else {
         return false;
      }
   }
 
}
 
?>