QuickTwit: PHP API for Fetching Tweets
- February 16, 2009 12:02 am
This is a PHP API wrapping the Twitter search APIs. It follows the same pattern as an ASP:Repeater style control in which you declare a header template that is rendered at the beginning on the tweet list, an item template that is rendered for each tweet matching the search criteria, and a footer template that is rendered after all items have been processed. Most of the search criteria have been implemented including: Creator, To, Contains, Ands, Ors, Nots, and tweet dates.
Methods
setSearchAttributes($creator, $to, $contains, $ands, $ors, $nots, $startDate, $endDate, $numResults): Sets the search criteria. Pass blank strings for any unimportant criteria. $numResults is integer number of results to return.
setHeaderTemplate(string $inTemplateText): Set’s the header template which is drawn before the items and footer
setItemTemplate(string $inTemplateText): Set’s the Item template which is drawn before the items and after the header
setFooterTemplate(string $inTemplateText): Set’s the Footer template which is drawn after the items and header
setDateFormat(string $newFormat): Sets the format used when rendering a date in any of the templates
setTimeFormat(string $newFormat): Sets the format used when rendering a time in any of the templates
getTweets(): Triggers the execution of the tweet search and drawing of the templates
Usage
The API works by assigning a header, item, and footer template as well as search criteria. When getTweets is called, the search is executed according to the criteria. The header template is drawn, followed by the item template for each returned tweet, and finally the footer template is drawn. A number of keywords can be used in the templates which will be replaced with the search-returned values at render time. Those keywords are:
[$Creator]: Creator of the tweet
[$Date]: Date of the tweet
[$Time]: Time of the tweet
[$Tweet]: Plain text of the tweet
[$PostID]: Twitter’s unique post ID
[$UserID]: Twitter’s unique user ID
[$sCreator]: The item passed to setSearchCriteria as the $Creator variable*
[$SearchStartDate]: The item passed to setSearchCriteria as the $startDate variable*
[$SearchEndDate]: The item passed to setSearchCriteria as the $endDate variable*
*Most keywords will be blank when used in header and footer templates since no search item is active. These keywords will have meaningful values in header and footer templates.
Apart from keyword replacement the templates will be drawn exactly as specified to the set*Template functions.
Examples
This is code similar to that in use on the front-page to retrieve my five latest tweets:
$test = new QuickTwit();
$test->setHeaderTemplate("[#sCreator]'s Tweets<hr />");
$test->setFooterTemplate("<hr />");
$test->setItemTemplate("<p>Creator: [#Creator]<br />Date:[#Date]<br />Tweet:[#HTMLTweet]<br />Blah: [#PostID] [#UserID] [#SearchStartDate] [#SearchEndDate]</p>");
$test->setSearchAttributes("LilMikeyDotCom", "", "", "", "", "", "", "", 5);
$test->getTweets();
Unfortunately I don’t have the phpExec plugin installed so I can’t run the thing live but at the time of posting the result looked like this: Creator: LilMikeyDotCom (Mike Randle) Creator: LilMikeyDotCom (Mike Randle) Creator: LilMikeyDotCom (Mike Randle) Creator: LilMikeyDotCom (Mike Randle) Creator: LilMikeyDotCom (Mike Randle)
LilMikeyDotCom's Tweets
Date:02-15-2009
Tweet:@colleenjaycox Trattoria Branica was great! Thx again for the rec.
Blah: 0tag:search.twitter.com,2005:1212702097
Date:02-15-2009
Tweet:V-day: bad reason to cover everything in chocolate. http://twitpic.com/1i99o
Blah: 0tag:search.twitter.com,2005:1212698299
Date:02-13-2009
Tweet:I think I’ve come to the conclusion that I operate at highest efficiency when in crisis mode.
Blah: 0tag:search.twitter.com,2005:1208346261
Date:02-13-2009
Tweet:@cadred ‘have’ or ‘I am a’?
Blah: 0tag:search.twitter.com,2005:1207840294
Date:02-13-2009
Tweet:Mongolian, good
Blah: 0tag:search.twitter.com,2005:1207263966
That’s a simple creator-based search but with the criteria you can go subject or date based as well. Here’s 10 posts since Feb 1st, 2009 with watchmen and movie in them (since it looks awesome) and not comic (since comics are for nerds):
$test = new QuickTwit();
$test->setHeaderTemplate("<hr />");
$test->setFooterTemplate("<hr />");
$test->setItemTemplate("<p>Creator: [#Creator]<br>Date:[#Date]<BR>Tweet:[#HTMLTweet]<BR></p>");
$test->setSearchAttributes("", "", "watchmen movie", "", "", "comic", "", "", 10);
$test->getTweets();
Results:
Creator: ZuckerBaby (ZuckerBaby)
Date:02-15-2009
Tweet:@Bronzethumb And this is where my fear about the Watchmen movie comes in. Stylish, looks like the book? Yes. Content remains awesome? Hmmmm
Creator: DavidSpinks (David Spinks)
Date:02-15-2009
Tweet:Getting really excited for the Watchmen movie. Currently working on the novel.
Creator: FitBusyPeople (GeorgeLouris)
Date:02-15-2009
Tweet:can't wait for the Watchmen movie, looks great
Creator: freshmoo (freshmoo)
Date:02-15-2009
Tweet:Watchmen movie http://tinyurl.com/4u9mrd
Creator: pdbtg (Andy Goldstein)
Date:02-15-2009
Tweet:I've been pretty meh about the upcoming Watchmen movie, but Malin Akerman's costume may have just tipped it into the 'must see' column.
Creator: ArtemisWinter (Diana Lamphiere)
Date:02-15-2009
Tweet:I'm terrified that the watchmen movie is going to suck. The best graphic novel of all time could so easily be ruined by hollywood.
Creator: charlesjurries (Charles Jurries)
Date:02-15-2009
Tweet:Saw commercial for WATCHMEN movie. How many more mopey "being a superhero is tough" movies can we take? Especially post-DAREDEVIL?
Creator: 1daymovies (Free Movies)
Date:02-15-2009
Tweet:Check Out 30 New Watchmen Movie Images — Released Today! - MTV.com
Creator: coreymull (Corey Mull)
Date:02-15-2009
Tweet:can't wait for the Watchmen movie!!
Creator: zegolf (Zach Egolf)
Date:02-15-2009
Tweet:Anyone have an opinion on the Watchmen movie? Is it going to be good?
You get the keyword highlighting for free from twitter in the html tweet.
So, here’s the source. Have a ball.
No Comments »
No comments yet.
RSS feed for comments on this post. TrackBack URI
Leave a comment
