Putting Twitter on Your Blog

Today I wanted to put my Twitter feed on my blog but didn’t really want to use a widget since that is for distribution purposes and I was looking for something more permanent. Twitter gives everyone the ability to see their tweets via rss or atom feeds so I choose to use the rss feed. Mine feed was located at http://twitter.com/statuses/user_timeline/2376811.rss. If you have PHP5 then parsing out an XML (rss) file is very easy thanks to the SimpleXML functions. One small problem that I ran into was that my host doesn’t allow me to read in the file like I wanted to. I had to use CURL to read the file save it locally and then reopen that file to parse it with SimpleXML. Let step though the code. First you have to get the data from the rss feed and use curl to store this as a new file on your local server.

//open the remote fee from twitter.
$ch = curl_init("http://twitter.com/statuses/user_timeline/2376811.rss");
//overwrite this feed to a local file
$fp = fopen ("twitter_feed.xml", "w");
curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);

Then you have to parse the local file that you just created. Twitter’s RSS feed puts the feed data in item nodes but nested in a channel node. Here we will use SimpleXML (PHP5 Only!) and a foreach loop to parse through each item node.

$xml = simplexml_load_file ("twitter_feed.xml");
foreach ($xml->channel->item as $item) {
echo str_replace ("farrelley:", "", $item->description);
}

Basically that is the heart of the code. Next we will put it all together into a function. We will also add in a counter that when you call the function you can specify how many item nodes or tweets you want to see.

// Display Twitter Feed
function getTwitterFeed($disp) {
//use Curl to get the feed and store it locally
$ch = curl_init ("http://twitter.com/statuses/user_timeline/2376811.rss");
//open the local feed.
$fp = fopen ("twitter_feed.xml", "w");
//set the counter = 1 its the first time through the script/
$count = 1;
curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);
//parse through locally stored feed
$xml = simplexml_load_file ("twitter_feed.xml");
foreach ($xml->channel->item as $item) {
//strip out my username each description has username: at beginning.
echo str_replace ("farrelley:", "", $item->description);
//if the count = the number to display then break out of foreach
if ($count == $disp) break;
//else increment count and continue.
else $count++;
}
}

Then you just have to add this call to the function where you want the data to be output.

getTwitterFeed (1);

You can see this in action (on the right sidebar) on my other blog at Living in the District (http://www.livinginthedistrict.com/). Like always and comments please post comments.

Good Programmers Make Mistakes!

With this statement:

Even good programmers make mistakes. The difference between a good programmer and a bad programmer is that the good programmer uses tests to detect his mistakes as soon as possible. The sooner you test for a mistake the greater your chance of finding it and the less it will cost to find and fix. This explains why leaving testing until just before releasing software is so problematic. Most errors do not get caught at all, and the cost of fixing the ones you do catch is so high that you have to perform triage with the errors because you just cannot afford to fix them all. (http://www.phpunit.de/pocket_guide/3.2/en/automating-tests.html)

I have put together a list of some good Unit and System Testing programs.  For unit testing I suggest PHPUnit or Simple Test.  These are both PHP based and very handy.  For System testing, making sure all your ducks are in a line, I like to use Selenium.  Anyone have any other handy QA Testing programs?

podCampDC T-Minus 3 Days

update: due to a capacity issue, the Friday night social meetup will be at the Capitol City Brewing Company on Capitol Hill, not downtown.  (2 Massachusetts Avenue NE)

podCampDC is this weekend and I hope to be on twitter and writing some posts while I am there. I looked at the schedule for speakers and plan on attending the following;

  • 10AM: New Media Marketing: How New Media Powers Business. Christopher Penn
  • 11AM: Not sure yet. Any Suggestions?
  • 1PM: Making Your Video Shareable – Justin Thorp
  • 2PM: Social Media and New Journalism – Andy Carvin and Jim Long
  • 3PM: Identity, Privacy, and Security: How are they different in social web environments?- Andrew Feinberg OR Power Your Personal Network with LinkedIn – Christopher Penn and Dan Williams

Things get started on Friday at Capitol City Brewing (Metro Center Metro Stop) Capitol City Brewing (Union Station Metro Stop) at 8PM. See you all soon!

Magento

WOW…I was surfing around the web today and found a new OpenSource eCommerce system developed off the Zend FrameworkMagento, by Varien looks really powerful.  This is a huge step for both eCommerce and the Zend Framework.  Oh yea don’t leave out the OpenSource World.  You can see Magento in all its glory here and download a copy for yourself here.  I will be looking at this very soon, as soon as I get out of work.   Varien has put a lot of time and effort into this.  The administrator interface looks clean and easy to us and has tons of features.  Some of them include; Localization, Pricing rules, RSS, etc.. See them all for yourself.

Finally looks like Zend Framework is going to be a leader in the development industry.