PHP Jabber Classes

If you are looking for a class that connects to a jabber server then you are in luck.  I have been searching around for a good one and found a couple of them but they just didn’t do what I was looking for.  I just found this on a couple weeks back on Google.  It’s called XMPP PHP.   I have been doing some cool things with it and have integrated it into some twitter code.   More on that later.

Jabber and Twitter Experiment.

I have been experimenting lately with the jabber side of twitter. It’s pretty powerful and allows you to do some cool things in real time. Unlike the HTTP protocol, xmpp (jabber) allows you to do things quicker and with less bandwidth. If you can read and XML file your pretty much golden because jabber pretty much just sends and receives xml.

With the Death of the DC Madam today I decided to test out some code that I have written. If you go to http://farrelley.com/dcmadam/ (currently only showing the last 20 tweets) you can track live twitters tweeting about the death and apparent suicide of the DC Madam. See what people are saying about her in real time and without that large media opinion. Twitter is a great tool for communication so why not use it for situations like this when they arise.

Feed back is welcome.  Please drop me a line in the comments or shoot me an email if you would like to know more of have some suggestions.

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?