 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
05-10-2009, 07:49 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
Feedback on my TwitterAPI?
Hey guys, I know I haven't posted in a long time.. but I still think about you! I swear!! Okay, okay.. I forgot about you guys
Anyways, I would like some feedback on my TwitterAPI. It's perfect in my eyes, but then again.. I love feedback.
Please note that I did this in about.. 3 hours? I haven't had time to improve the error handling, it does need some work
The idea of the API is to only return useful information.. if you've ever used the Twitter API, it returns a bunch of information on every query that's not needed.. and if I included it all in the cache, the cache would be like 10 times larger
PHP Code:
<?php class TwitterAPI { public $caching = false; public $cache_dir = null; public $cache_time = 0; private $modified = false; private $twitter_username = null; private $twitter_password = null; protected $api_calls = 0; protected $data = array( 'tweets' => null, 'tweet_lookup' => null, 'friend_tweets' => null, 'friend_ids' => null, 'public_tweets' => null, 'user_lookup' => null, 'mentioned' => null, 'followers' => null, 'follower_ids' => null, 'direct_messages' => null, 'direct_messages_sent' => null, 'rate' => null ); static public $instance = null; /** * Sets default settings, and if caching is enabled, we try to load the cache file * @param string $username (Optional) Default username * @param string $password (Optional) Default password * @param bool $caching (Optional) True to enable caching, false to disable * @param string $cache_dir (Optional) Default cache directory (where we store cache files) * @param int $cache_time (Optional) The max age of a cache file, in seconds (IE: 300 would be 5 minutes -- 60*5) * @public * @return nothing */ public function __construct($username = null, $password = null, $caching = false, $cache_dir = null, $cache_time = 0) { $this->twitter_username = $username; $this->twitter_password = $password; $this->caching = $caching; $this->cache_dir = $cache_dir; $this->cache_time = $cache_time; if($caching) { $this->load_cache(); } } /** * Gets the instance of this object, if there isn't one, then one is created and returned * @static * @public * @return TwitterAPI */ static public function getInstance() { if(self::$instance === null) { self::$instance = new self(); } return self::$instance; } /** * Attempts to verify user credentials * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-account*verify_credentials * @public * @return bool True on success or false on failure */ public function verifyCredentials() { try { $this->APICall('account/verify_credentials'); return true; } catch(TwitterException $error) { return false; } } /** * Quote: Ends the session of the authenticating user, returning a null cookie. * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-account*end_session * @public * @return bool True on success or false on failure */ public function endSession() { if($this->APICall('account/end_session', null, 'POST') !== false) { return true; } return false; } /** * Sends a direct message to a recipient (NOTE: Recipient must be on friends list) * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-direct_messages*new * @param string $recipient The recipient of the message * @param string $message The message being sent to the recipient * @public * @return bool True on success or false on failure */ public function sendDirectMessage($recipient, $message) { if($this->APICall('direct_messages/new', array('text' => $message, 'user' => $recipient), 'POST') !== false) { return true; } return false; } /** * Deletes a tweet via ID * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses*destroy * @param int $tweet_id The tweet ID to delete * @public * @return bool True on success or false on failure */ public function deleteTweet($tweet_id) { if($this->APICall('statuses/destroy/'.$tweet_id, null, 'DELETE') !== false) { return true; } return false; } /** * Deletes a direct message via ID * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-direct_messages*destroy * @param int $message_id The message ID to delete * @public * @return bool True on success or false on failure */ public function deleteDirectMessage($message_id) { if($this->APICall('direct_messages/destroy/'.$message_id, null, 'DELETE') !== false) { return true; } return false; } /** * Sends a tweet * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses*update * @param string $message The tweet text to post * @param int $reply_to_id (Optional) An ID representing a tweet that you are replying to * @public * @return bool True on success or false on failure */ public function sendTweet($message, /*$shorten_urls = true, */$reply_to_id = null) { // // TODO: Add support for automatically finding and replacing urls with their shortened style // if($this->APICall('statuses/update', array('status' => $message, 'in_reply_to_status_id' => $reply_to_id), 'POST') !== false) { return true; } return false; } /** * Befriends a user by ID or screen name * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-friendships*create * @param string|int $user The user to befriend (either user ID or screen name, depending on $user_identifier) * @param string $user_identifier (Default: screen_name) Tells how we identify the user, possible methods are: user_id, screen_name * @param bool $follow_user (Optional) Allows you to follow the user after befriending * @public * @return bool True on success or false on failure */ public function addFriend($user, $user_identifier = 'screen_name', $follow_user = 'true') { $user_identifier = strtolower($user_identifier); if($user_identifier !== 'screen_name' && $user_identifier !== 'user_id') { return false; } if($this->APICall('friendships/create', array($user_identifier => $user), 'POST') !== false) { return true; } return false; } /** * Gets a list of friend IDs * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-friends*ids * @param array An array of params to pass to the API call * @public * @return array Returns an array of friend IDs */ public function getFriendIDList($params = null) { if($this->data['friend_ids'] === null) { $buffer = $this->APICall('friends/ids', $params); // It -is- possible to not have any friends.. if(isset($buffer[0])) { foreach($buffer as $id) { $this->data['friend_ids'][] = $id; } } else { $this->data['friend_ids'] = array(); } $this->modified = true; } return $this->data['friend_ids']; } /** * Gets a list of follower IDs * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-friends*ids * @param array An array of params to pass to the API call * @public * @return array Returns an array of follower IDs */ public function getFollowerIDList($params = null) { if($this->data['follower_ids'] === null) { $buffer = $this->APICall('followers/ids', $params); // It -is- possible to not have any followers.. if(isset($buffer[0])) { foreach($buffer as $id) { $this->data['follower_ids'][] = $id; } } else { $this->data['follower_ids'] = array(); } $this->modified = true; } return $this->data['follower_ids']; } /** * Removes a friend by their ID or username * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-friendships*destroy * @param string|int $friend The friend to remove (either ID or username) * @param string $friend_identifier (Default: screen_name) Tells how we identify the friend, possible methods are: user_id, screen_name * @public * @return bool True on success or false on failure */ public function deleteFriend($friend, $friend_identifier = 'screen_name') { $friend_identifier = strtolower($friend_identifier); if($friend_identifier !== 'screen_name' && $friend_identifier !== 'user_id') { return false; } if($this->APICall('friendships/destroy', array($friend_identifier => $friend), 'DELETE') !== false) { return true; } return false; } /** * Checks to see if a username or ID is a friend * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-friendships-exists * @param string|int $friend Check to see if this user is a friend of us * @public * @return bool True if the friend exists, false if not */ public function existsFriend($friend) { return $this->APICall('friendships/exists', array('user_a' => $this->twitter_username, 'user_b' => $friend)); } /** * Returns a 200 status OK header, just a test function * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-help*test * @public * @return bool True on success or false on failure */ public function helpTest() { return $this->APICall('help/test'); } /** * Gets a list of direct messages sent to you * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-direct_messages * @param string $date_format The format used in date() for times * @param array $params (Optional) A list of parameters to pass to Twitter * @param bool $use_href (Optional, Default: true) Turns all '@<username>' matches to links * @public * @return array Returns an array of direct messages */ public function getDirectMessages($date_format = null, $params = null, $use_href = true) { if($this->data['direct_messages'] === null) { $buffer = $this->APICall('direct_messages', $params); foreach($buffer as $message) { if($use_href) { $message->text = preg_replace('/@([a-z0-9]+)/i', '<a href="http://www.twitter.com/$1" title="$1 on Twitter">@$1</a>', $message->text); } $this->data['direct_messages'][] = array( 'id' => $message->id, 'from-name' => $message->sender->name, 'from-screen_name' => $message->sender_screen_name, 'text' => $message->text, 'time' => $date_format !== null ? date($date_format, strtotime($message->created_at)) : strtotime($message->created_at) ); } $this->modified = true; }
return $this->data['direct_messages']; } /** * Gets a list of sent direct messages sent to you * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-direct_messages*sent * @param string $date_format The format used in date() for times * @param array $params (Optional) A list of parameters to pass to Twitter * @param bool $use_href (Optional, Default: true) Turns all '@<username>' matches to links * @public * @return array Returns an array of sent direct messages */ public function getSentDirectMessages($date_format = null, $params = null, $use_href = true) { if($this->data['direct_messages_sent'] === null) { $buffer = $this->APICall('direct_messages/sent', $params); foreach($buffer as $message) { if($use_href) { $message->text = preg_replace('/@([a-z0-9]+)/i', '<a href="http://www.twitter.com/$1" title="$1 on Twitter">@$1</a>', $message->text); } $this->data['direct_messages_sent'][] = array( 'id' => $message->id, 'to-id' => $message->recipient_id, 'to-name' => $message->recipient->name, 'to-screen_name' => $message->recipient->screen_name, 'text' => $message->text, 'time' => $date_format !== null ? date($date_format, strtotime($message->created_at)) : strtotime($message->created_at) ); } $this->modified = true; }
return $this->data['direct_messages_sent']; } /** * Gets a list of your followers * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses*followers * @param string $date_format The format used in date() for times * @param array $params (Optional) A list of parameters to pass to Twitter * @param bool $use_href (Optional, Default: true) Turns all '@<username>' matches to links * @public * @return array Returns an array of direct messages */ public function getFollowers($date_format = null, $params = null, $use_href = true) { if($this->data['followers'] === null) { $buffer = $this->APICall('statuses/followers', $params); foreach($buffer as $follower) { if($use_href) { $follower->status->text = preg_replace('/@([a-z0-9]+)/i', '<a href="http://www.twitter.com/$1" title="$1 on Twitter">@$1</a>', $follower->status->text); } $this->data['followers'][] = array( 'follower-id' => $follower->id, 'follower-name' => $follower->name, 'follower-screen_name' => $follower->screen_name, 'follower-created' => $date_format !== null ? date($date_format, strtotime($follower->created_at)) : strtotime($follower->created_at), 'follower-following' => $follower->following, 'follower-followers' => $follower->followers_count, 'follower-friends' => $follower->friends_count, 'follower-favorites' => $follower->favourites_count, 'follower-tweets' => $follower->statuses_count, 'tweet-text' => $follower->status->text, 'tweet-time' => $date_format !== null ? date($date_format, strtotime($follower->status->created_at)) : strtotime($follower->status->created_at) ); } $this->modified = true; }
return $this->data['followers']; } /** * Gets user information about a specific user * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-users*show * @param string $user The user to retrieve (the id, screen name, or user-id) * @param string $user_identifier Valid entries are: id, screen_name, and user_id * @param string $date_format The format used in date() for times * @public * @return bool The only time it will return a bool is when $user_identifier is incorrect, in which case it will always be false when returned */ public function getUser($user, $user_identifier = 'screen_name', $date_format = null) { $user_identifier = strtolower($user_identifier); if($user_identifier !== 'id' && $user_identifier !== 'screen_name' && $user_identifier !== 'user_id') { return false; } if($this->data['user_lookup'] === null || !isset($this->data['user_lookup'][$user_identifier . '-' . $user])) { $buffer = $this->APICall('users/show', array($user_identifier => $user));
$this->data['user_lookup'][$user_identifier . '-' . $user] = array( 'id' => $buffer->id, 'name' => $buffer->name, 'screen_name' => $buffer->screen_name, 'created' => $date_format !== null ? date($date_format, strtotime($buffer->created_at)) : strtotime($buffer->created_at), 'following' => $buffer->following, 'followers' => $buffer->followers_count, 'friends' => $buffer->friends_count, 'favorites' => $buffer->favourites_count, 'tweets' => $buffer->statuses_count, 'tweet-text' => $buffer->status->text, 'tweet-time' => $date_format !== null ? date($date_format, strtotime($buffer->status->created_at)) : strtotime($buffer->status->created_at) ); $this->modified = true; } return $this->data['user_lookup'][$user_identifier . '-' . $user]; } /** * Gets information about a specific tweet * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses*show * @param int $tweet_id The ID of the tweet to retrieve * @param string $date_format The format used in date() for times */ public function getTweet($tweet_id, $date_format = null) { if($this->data['tweet_lookup'] === null || !isset($this->data['tweet_lookup'][$tweet_id])) { $buffer = $this->APICall('statuses/show/'.$tweet_id);
$this->data['tweet_lookup'][$tweet_id] = array( 'id' => $tweet->id, 'author' => $buffer->user->name, 'screen_name' => $buffer->user->screen_name, 'text' => $buffer->text, 'time' => $date_format !== null ? date($date_format, strtotime($buffer->created_at)) : strtotime($buffer->created_at) ); $this->modified = true; } return $this->data['tweet_lookup'][$tweet_id]; } /** * Gets a list of your tweets * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses-user_timeline * @param string $date_format The format used in date() for times * @param array $params (Optional) A list of parameters to pass to Twitter * @param bool $use_href (Optional, Default: true) Turns all '@<username>' matches to links * @public * @return array Returns an array of tweets */ public function getTweets($date_format = null, $params = null, $use_href = true) { if($this->data['tweets'] === null) { $buffer = $this->APICall('statuses/user_timeline', $params); foreach($buffer as $tweet) { if($use_href) { $tweet->text = preg_replace('/@([a-z0-9]+)/i', '<a href="http://www.twitter.com/$1" title="$1 on Twitter">@$1</a>', $tweet->text); } $this->data['tweets'][] = array( 'id' => $tweet->id, 'text' => $tweet->text, 'time' => $date_format !== null ? date($date_format, strtotime($tweet->created_at)) : strtotime($tweet->created_at) ); } $this->modified = true; }
return $this->data['tweets']; } /** * Gets a list of public tweets * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses-public_timeline * @param string $date_format The format used in date() for times * @param array $params (Optional) A list of parameters to pass to Twitter * @param bool $use_href (Optional, Default: true) Turns all '@<username>' matches to links * @public * @return array Returns an array of public tweets */ public function getPublicTweets($date_format = null, $params = null, $use_href = true) { if($this->data['public_tweets'] === null) { $buffer = $this->APICall('statuses/public_timeline', $params); foreach($buffer as $tweet) { if($use_href) { $tweet->text = preg_replace('/@([a-z0-9]+)/i', '<a href="http://www.twitter.com/$1" title="$1 on Twitter">@$1</a>', $tweet->text); }
$this->data['public_tweets'][] = array( 'id' => $tweet->id, 'author' => $tweet->user->name, 'screen_name' => $tweet->user->screen_name, 'text' => $tweet->text, 'time' => $date_format !== null ? date($date_format, strtotime($tweet->created_at)) : strtotime($tweet->created_at) ); } $this->modified = true; } return $this->data['public_tweets']; } /** * Gets a list of your friend's tweets * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses-friends_timeline * @param string $date_format The format used in date() for times * @param array $params (Optional) A list of parameters to pass to Twitter * @param bool $use_href (Optional, Default: true) Turns all '@<username>' matches to links * @public * @return array Returns an array of your friend's tweets */ public function getFriendTweets($date_format = null, $params = null, $use_href = true) { if($this->data['friend_tweets'] === null) { $buffer = $this->APICall('statuses/friends_timeline', $params); foreach($buffer as $tweet) { if($use_href) { $tweet->text = preg_replace('/@([a-z0-9]+)/i', '<a href="http://www.twitter.com/$1" title="$1 on Twitter">@$1</a>', $tweet->text); }
$this->data['friend_tweets'][] = array( 'id' => $tweet->id, 'author' => $tweet->user->name, 'screen_name' => $tweet->user->screen_name, 'text' => $tweet->text, 'time' => $date_format !== null ? date($date_format, strtotime($tweet->created_at)) : strtotime($tweet->created_at) ); } $this->modified = true; } return $this->data['friend_tweets']; } /** * Gets a list of all the tweets which mention you * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses-mentions * @param string $date_format The format used in date() for times * @param array $params (Optional) A list of parameters to pass to Twitter * @param bool $truncate_me (Optional) If set to true, and your name (@<you>) is the first word, it gets truncated * @param bool $use_href (Optional, Default: true) Turns all '@<username>' matches to links * @public * @return array Returns an array of tweets which mention you */ public function getMentioned($date_format = null, $params = null, $truncate_me = true, $use_href = true) { if($this->data['mentioned'] === null) { $buffer = $this->APICall('statuses/mentions', $params); foreach($buffer as $tweet) { if($truncate_me) { // Not all mentions start with @ (IE: "thanks to @JStrese my life is complete!") if($tweet->text[0] === '@') { $tweet->text = substr($tweet->text, strlen($this->twitter_username)+2); } } if($use_href) { $tweet->text = preg_replace('/@([a-z0-9]+)/i', '<a href="http://www.twitter.com/$1" title="$1 on Twitter">@$1</a>', $tweet->text); }
$this->data['mentioned'][] = array( 'id' => $tweet->id, 'author' => $tweet->user->name, 'screen_name' => $tweet->user->screen_name, 'text' => $tweet->text, 'time' => $date_format !== null ? date($date_format, strtotime($tweet->created_at)) : strtotime($tweet->created_at) ); } $this->modified = true; }
return $this->data['mentioned']; } /** * Gets information (hourly rate, remaining hits, and reset time) about your API calls * @see http://apiwiki.twitter.com/Twitter-REST-API-Method:-account*rate_limit_status * @public * @return array An array of information regarding your API calls */ public function getRateLimit() { $buffer = $this->APICall('account/rate_limit_status'); $this->data['rate'][] = array( 'hourly-limit' => $buffer->hourly_limit, 'hourly-remaining' => $buffer->remaining_hits, 'resets' => strtotime($buffer->reset_time) ); return $this->data['rate']; } /** * Shortens a url with the is.gd (http://is.gd/) service * @see http://is.gd/api_info.php * @param string $url The url to be shortened * @public * @static * @return string|bool Returns string if the url is shortened, or false if there was an error */ public static function shortenURL($url) { $ch = curl_init('http://is.gd/api.php?longurl='.urlencode($url)); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10); $url = curl_exec($ch); if(curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) { curl_close($ch); return $url; } curl_close($ch); return false; } /** * Requests an API call from Twitter * @param string $call The API call (IE: account/rate_limit_status) * @param array $params (Optional) Extra parameters to pass along to Twitter * @param string $method The HTTP method to issue (GET, REQUEST, POST, UPDATE, DELETE, etc) [MUST CONFORM TO API CALL] * @final * @private * @return bool|array Returns false on any error, or an array of JSON data with a successful call */ final private function APICall($call, $params = null, $method = 'GET') { $ch = null; if($params !== null) { $ch = curl_init('http://twitter.com/'.$call.'.json?' . http_build_query($params)); } else { $ch = curl_init('http://twitter.com/'.$call.'.json'); } curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_USERPWD, $this->twitter_username . ':' . $this->twitter_password); $data = curl_exec($ch); $response = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); if($response !== 200 && $response !== 304) { throw new TwitterException((int)$response); } curl_close($ch); if($data !== false) { $this->api_calls += 1; return json_decode($data); } return false; } /** * Saves data to a cache file * @public * @return bool False on error, true on success */ public function save_cache() { if($this->modified && $this->cache_dir !== null && is_dir($this->cache_dir)) { $this->data['cached_at'] = time(); file_put_contents(rtrim($this->cache_dir, '/\\').'/twitter.'.$this->twitter_username.'.txt', serialize($this->data)); return true; } return false; } /** * Attemps to load a cache file for this username * @public * @return bool False on error, true on success */ public function load_cache() { if($this->cache_dir !== null && is_dir($this->cache_dir)) { $path = rtrim($this->cache_dir, '/\\').'/twitter.'.$this->twitter_username.'.txt'; if(file_exists($path) && is_readable($path)) { $data = unserialize(file_get_contents($path)); if((time() - (int)$data['cached_at']) < $this->cache_time) { $this->data = $data; return true; } } } return false; } } class TwitterException extends Exception { public function __construct($code) { switch(1) { case $code === 400: $message = 'The request was invalid. This is the status code that will be returned during rate limiting.'; break; case $code === 401: $message = 'Authentication credentials were missing or incorrect.'; break; case $code === 403: $message = 'The request is understood, but it has been refused.'; break; case $code === 404: $message = 'The URI requested is invalid or the resource requested, such as a user, does not exists.'; break; case $code === 406: $message = 'Returned by the Search API when an invalid format is specified in the request.'; break; case $code === 500: $message = 'Something is broken. Please post to the group so the Twitter team can investigate.'; break; case $code === 502: $message = 'Twitter is down or being upgraded.'; break; case $code === 503: $message = 'The Twitter servers are up, but overloaded with requests. Try again later. The search and trend methods use this to indicate when you are being rate limited.'; break; default: $message = 'An unknown error occured while querying Twitter, please report this to the TwitterAPI developers.'; break; } parent::__construct($message, $code); } public function __toString() { return '[TwitterAPI Error (code: '.$this->code.')]: ' . $this->message; } } ?>
Last edited by Jay : 05-11-2009 at 05:46 PM.
|
|
|
|
05-11-2009, 05:26 AM
|
#2 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
|
Looks good from just a quick glance, I am swamped right now but have this twitter api on my plate for the coming week. I shall bookmark and let you know after I give it a shot later this week.
|
|
|
|
05-11-2009, 06:06 AM
|
#3 (permalink)
|
|
The Wanderer
Join Date: May 2009
Posts: 23
Thanks: 4
|
*pokes Jay* I never knew that Twitter has an API! grrr, I'll get back to you! I swear! *raises left hand* hehe
|
|
|
|
05-11-2009, 11:30 AM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
That getInstance method seems odd to me, the new self(), should be self::$instance = new self; plus its odd to have this static call just for this.
But else it looks good by skimming over it, I wrote a similar thing in C as a PHP extension, however I didn't use curl but rather the streams api, and a notifier for grabbing the HTTP code which is a quite useful feature that no many seems to know about :)
__________________
|
|
|
05-11-2009, 02:42 PM
|
#5 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
Hehe, I changed the getInstance function..
@foobarph
There are a few PHP Twitter APIs out there, mine only adds to the bunch
@CoryMathews
Lemme know if you find any irregularities.. I tested each function after I made it to see that it worked, but I could have missed something..
PHP Code:
$twitter = new TwitterAPI('Username', 'Password', true, './twitter_cache', 300);
if($twitter->verifyCredentials()) { // It's your job to catch exceptions // All TwitterAPI exceptions are of type TwitterException // verifyCredentials() is the only function that won't raise an exception
// the date format parameter is optional $myTweets = $twitter->getTweets('F jS, Y');
// gets all the tweets that mentioned you (@<you>) $mentioned = $twitter->getMentioned('F jS, Y');
// Auto-saving of the cache isn't implemented.. so we gotta call this to save it. Don't worry, if you haven't requested new data since the cache was written, no new cache will be saved :-P (it checks for $modified to be true) $twitter->saveCache(); } else { print 'Incorrect twitter credentials, yo.'; }
|
|
|
|
05-11-2009, 05:41 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
This line is deprecated in PHP5:
PHP Code:
self::$instance = &new self()
In favor of this:
PHP Code:
self::$instance = new self()
As objects get passed around by reference auto-magically in PHP5.
That code produces a PHP strict warning:
Code:
Assigning the return value of new by reference is deprecated
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
05-11-2009, 05:47 PM
|
#7 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
My bad, fixed 
|
|
|
|
05-11-2009, 06:22 PM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
I'm not convinced that there needs to be any Singleton here. Why would you only ever want one instance of this class?
Also, I know your design goal was to remove the cruft that is attached to Twitter's API responses but how about having the ability to get at extra stuff if/when needed? (You are allowed to say no.)
Rather than wrapping the meat of a method within an if statment, determine whether the cache is available and return early instead. For example:
PHP Code:
public function getDirectMessages($date_format = null, $params = null, $use_href = true)
{
// In internal cache, return early
if( ! empty($this->data['direct_messages']))
{
return $this->data['direct_messages'];
}
$buffer = $this->APICall('direct_messages', $params);
foreach($buffer as $message)
{
if($use_href)
{
$message->text = preg_replace('/@([a-z0-9]+)/i', '<a href="http://www.twitter.com/$1" title="$1 on Twitter">@$1</a>', $message->text);
}
$this->data['direct_messages'][] = array(
'id' => $message->id,
'from-name' => $message->sender->name,
'from-screen_name' => $message->sender_screen_name,
'text' => $message->text,
'time' => $date_format !== null ? date($date_format, strtotime($message->created_at)) : strtotime($message->created_at)
);
}
$this->modified = true;
return $this->data['direct_messages'];
}
|
|
|
|
05-11-2009, 06:40 PM
|
#9 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
Quote:
|
Originally Posted by Salathe
Rather than wrapping the meat of a method within an if statment, determine whether the cache is available and return early instead. For example:
|
Uhm.. Both of our methods return at the same rate.. It's just a matter of design preference
And what's wrong with including a singleton method? You don't have to use it. It's there for the people that can find use for it
Also, care to elaborate on your usage of empty() instead of the !== operator? By default $data is set to null, so it's safe to use it.
|
|
|
|
05-11-2009, 07:03 PM
|
#10 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Creating a singleton is not just writing a method. The idea is to only ever have one instance of the class: mixing an instance stored in a class property with the ability to create as many instances you like (via __construct) defeats the point of trying to implement the singleton pattern.
I just used empty due to personal preference: "If not empty..." reads better, for me, than "if ... is not equal to null" and there isn't any requirement to be checking only for NULL (is there?). !== null, as you know, would work equally well in this case.
|
|
|
|
05-11-2009, 07:38 PM
|
#11 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
Quote:
Originally Posted by Salathe
Creating a singleton is not just writing a method. The idea is to only ever have one instance of the class: mixing an instance stored in a class property with the ability to create as many instances you like (via __construct) defeats the point of trying to implement the singleton pattern.
|
I fixed that.. that was a bug with my original code, I admit that..
But now you are free to use getInstance() or just create a new object using new.
Quote:
Originally Posted by Salathe
I just used empty due to personal preference: "If not empty..." reads better, for me, than "if ... is not equal to null" and there isn't any requirement to be checking only for NULL (is there?). !== null, as you know, would work equally well in this case.
|
Indeed, empty() might be perceived by the human mind easier.. but I chose the !== operator because calling empty() (a function) is not necessary.. the performance benefit is trivial, I know, but that's just me 
|
|
|
|
05-11-2009, 08:57 PM
|
#12 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Jay
Indeed, empty() might be perceived by the human mind easier.. but I chose the !== operator because calling empty() (a function) is not necessary.. the performance benefit is trivial, I know, but that's just me 
|
empty is not a function, but rather a language construct. As far as I'm aware, there is no (read: negligible, if any) performance hit using empty versus the not-identical operator so it just comes down to personal style in how you want the code to read.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|