3 * Sean's Simple Twitter Library
5 * Probably a little more or a little less than you need.
7 * Copyright 2008, Sean Coates
8 * Usage of the works is permitted provided that this instrument is retained
9 * with the works, so that any entity that uses the works is notified of this
11 * DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
12 * ( Fair License - http://www.opensource.org/licenses/fair.php )
13 * Short license: do whatever you like with this.
15 * komode: le=unix language=php codepage=utf8 tab=4 notabs indent=4
20 * Base URL for Twitter API
22 * Do not specify user/password in URL
24 protected $baseUrl = 'http://twitter.com/';
27 * Full base URL (includes user/pass)
31 protected $baseUrlFull = null;
39 * Twitter API password
44 * Constructor; sets up configuration.
46 * @param string $user Twitter user name; null for limited read-only access
47 * @param string $pass Twitter password; null for limited read-only access
49 public function __construct($user=null, $pass=null) {
50 $this->baseUrlFull = $this->baseUrl;
52 // user is defined, so use it in the URL
55 $parsed = parse_url($this->baseUrl);
56 $this->baseUrlFull = $parsed['scheme'] . '://' . $this->user . ':' .
57 $this->pass . '@' . $parsed['host'];
59 if (isset($parsed['port']) && is_numeric($parsed['port'])) {
60 $this->baseUrlFull .= ':' . $parsed['port'];
62 // append path (default: /)
63 if (isset($parsed['path'])) {
64 $this->baseUrlFull .= $parsed['path'];
66 $this->baseUrlFull .= '/';
72 * Fetches a tweet by its number/id
74 * @param int $num the tweet id/number
75 * @return string (null on failure)
77 public function getTweetByNum($num) {
78 if (!is_numeric($num)) {
81 $tweet = json_decode(file_get_contents($this->getUrlStatus($num)));
86 * Reads [last] tweet from user
88 * @param string $tweeter the tweeter username
89 * @param int $num this many tweets ago (1 = current tweet)
90 * @return string (false on failure)
92 public function getLastTweet($tweeter, $num = 1)
94 $source = json_decode(file_get_contents($this->getUrlUserTimeline($tweeter)));
95 if ($num > count($source)) {
98 $tweet = $source[$num - 1];
99 if (!isset($tweet->user->screen_name) || !$tweet->user->screen_name) {
106 * fetches mentions for a user
108 public function getMentions($sinceId=null, $count=20) {
109 return json_decode(file_get_contents($this->getUrlMentions($sinceId, $count)));
113 * Fetches followers for a user
115 public function getFollowers($cursor=-1) {
116 return json_decode(file_get_contents($this->getUrlFollowers($cursor)));
122 public function follow($userId) {
126 'content' => array(),
127 'header' => 'Content-type: application/x-www-form-urlencoded',
130 $ctx = stream_context_create($params);
131 $fp = fopen($this->getUrlFollow($userId), 'rb', false, $ctx);
135 $response = stream_get_contents($fp);
136 if ($response === false) {
139 $response = json_decode($response);
144 * fetches DMs for a user
146 public function getDMs($sinceId=null, $count=20, $page=1) {
147 return json_decode(file_get_contents($this->getUrlDMs($sinceId, $count, $page)));
153 public function sendDM($screenName, $text) {
154 $data = http_build_query(array('screen_name'=>$screenName, 'text'=>$text));
159 'header' => 'Content-type: application/x-www-form-urlencoded',
162 $ctx = stream_context_create($params);
163 $fp = fopen($this->getUrlSendDM(), 'rb', false, $ctx);
167 $response = stream_get_contents($fp);
168 if ($response === false) {
171 $response = json_decode($response);
178 * @param string $txt the tweet text to send
179 * @return string URL of tweet (or false on failure)
181 public function sendTweet($txt, $limit=true) {
183 $txt = substr($txt, 0, 140); // twitter message size limit
185 $data = 'status=' . urlencode($txt);
190 'header' => 'Content-type: application/x-www-form-urlencoded',
193 $ctx = stream_context_create($params);
194 $fp = fopen($this->getUrlTweetPost(), 'rb', false, $ctx);
198 $response = stream_get_contents($fp);
199 if ($response === false) {
202 $response = json_decode($response);
207 * Returns the base API URL
209 protected function getUrlApi() {
210 return $this->baseUrlFull;
214 * Returns the status URL
216 * @param int $num the tweet number
218 protected function getUrlStatus($num) {
219 return $this->getUrlApi() . 'statuses/show/'. urlencode($num) .'.json';
223 * Returns the user timeline URL
225 protected function getUrlUserTimeline($user) {
226 return $this->getUrlApi() . 'statuses/user_timeline/'. urlencode($user) .'.json';
230 * Returns the tweet posting URL
232 protected function getUrlTweetPost() {
233 return $this->getUrlApi() . 'statuses/update.json';
239 public function getUrlOutputStatus(StdClass $tweet) {
240 return $this->baseUrl . urlencode($tweet->user->screen_name) . '/statuses/' . urlencode($tweet->id);
244 * Return mentions URL
246 public function getUrlMentions($sinceId=null, $count=20) {
247 $url = $this->baseUrlFull . 'statuses/mentions.json?count=' . urlencode($count);
248 if ($sinceId !== null) {
249 $url .= '&since_id=' . urlencode($sinceId);
255 * Returns the followers URL
257 public function getUrlFollowers($cursor=-1) {
258 return $this->baseUrlFull . 'statuses/followers.json?cursor=' . ((int)$cursor);
262 * Returns the follow-user URL
264 public function getUrlFollow($userid) {
265 return $this->baseUrlFull . 'friendships/create/' . ((int) $userid) . '.json';
269 * Returns the get DMs URL
271 public function getUrlDMs($sinceId=null, $count=20, $page=1) {
272 $url = $this->baseUrlFull . 'direct_messages.json?';
273 if ($sinceId !== null) {
274 $url .= 'since_id=' . urlencode($sinceId);
276 $url .= "&page={$page}";
277 $url .= "&count={$count}";
282 * Returns the send DM URL
284 public function getURLSendDM() {
285 return $this->baseUrlFull . 'direct_messages/new.json';