You’d Think I Was Back At The Datacenter With Punchcards Waiting For A Job To Run

by xearther ~ April 13th, 2009. Filed under: Code Lounge.

I’ve been exploring the Twitter API these past few weeks and the one thing I come away with is irony.

Here’s an API that is very simple and clear. Yet the code I’ve come across dancing with the API has been anything but… simple and clear, that is.

But I’ll stop with the wine and cheese here and just dive in and share with you the FACT that you don’t need to be wearing no stinkin’ “programmers badge” to be doing some simple thing… like reporting on a web page how many people you are friends with or are following you.

And that’s what I’m about to cover.

First, check out the functionality of doing just that (reporting on how many friends) over at It’s just below the fold. You’ll see “Niche Triva has X friends. “

Now here is the code:

Niche Triva has
$xmlfriends = simplexml_load_file(““);
foreach($xmlfriends->children() as $child)
echo “$x”;

Note the URL. You can put that in a browser – no password is necessary! And note the Twitter User Name (nichetrivia). It can be anyone’s User Name – even yours! And finally, note the “friends“. Yep, you guessed it. You can change it to “followers“.

When you enter the URL in a browser you will see something like what you see below: a list of Twitter “friend” User IDs (as opposed to User Names) “wrapped” in a XML “package”. The PHP function “simplexml_load_file” pulls in the data, and after creating an array “home” for the data, a “ForEach” loop is created, using “X” as a counter as the loop moves through each “cell” of the array. We then “echo” the value of “X” back to the HTML page, representing the “children” counted in the loop.

There are many ways to achieve this same result. Hopefully, you will find this one is simple, with the lack of a need to pass passwords and hence (somewhat) anonymously a little “icing on the cake”.


Leave a Reply