August 19th, 2009
Hello, World.
Explanation
It seems fitting that our first Silverlight application was a Twitter client. They’re the new “Hello, World”. It was a decent way to dip our toes in, since I’ve built similar client’s in other environments, and because they’re so straightforward.
Watch the video above to see me build one in about a half an hour.
A few notes on what’s transpiring above.
As I mention in the screencast, we can’t connect directly to Twitter to download the latest tweet from a user due to how Twitter’s setup the cross domain security on their site, so my solution was to build a quick and dirty PHP script and throw it up on our server.
To save time, I ended up using Matt Williams’s excellent PHP Twitter library, but you could easily write your own without too many headaches.
All we need to do is connect to Twitter via the API, and grab the latest tweet from a given user’s timeline. For our site, we’re actually aggregating several user’s twitter feeds, one of which we’re displaying all tweets from (the @MessSilverlight account) and three of which we only want to display those tweets tagged with #mws from.
The PHP looks like this:
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 | <?php error_reporting(0); header("Cache-Control: no-cache"); header("Expires: -1"); require_once("Arc90/Service/Twitter.php"); $username = 'your_username'; $password = 'your_password'; $statuses = array(); $mostrecentstatus = null; $users_to_search = array(); $usernames = explode(",", $_GET['users']); foreach($usernames as $user) { $users_to_search[]['id'] = $user; if ($_GET['onlytagged']) $only_when_tagged_with[$user] = "#".$_GET['onlytagged']; } if (count($usernames) < 1) die("No users given to fetch"); $twitter = new Arc90_Service_Twitter($username, $password); $twitter->setSSL(false); foreach($users_to_search as $user) { $latest = null; $big = $twitter->getRateLimitStatus('json', $user); $timeline = $twitter->getUserTimeline('json', $user); $data = json_decode($timeline); if ($only_when_tagged_with[$user['id']]) { foreach($data as $status) { if ($latest) continue; if (substr_count($status->text, $only_when_tagged_with[$user['id']]) > 0) { $status->text = str_ireplace($only_when_tagged_with[$user['id']], '', $status->text); $latest = $status; } } } else { $latest = $data[0]; } $statuses[] = $latest; $mostrecentstatus = $latest; } foreach($statuses as $status) { $dateof = strtotime($status->created_at); if ($dateof > strtotime($mostrecentstatus->created_at)) $mostrecentstatus = $status; } if ($mostrecentstatus->text) { $status_obj = new StdClass(); $status_obj->status = $mostrecentstatus->text; $status_obj->created_at = date("F j, Y g:i a", strtotime($mostrecentstatus->created_at)); $status_obj->screen_name = $mostrecentstatus->user->screen_name; $status_obj->status_id = $mostrecentstatus->id; echo json_encode($status_obj); exit; } ?> |
The code above is released under the Microsoft Public License, same as every other project on this site, so feel free to use it.
