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 www.NicheTrivia.com. It’s just below the fold. You’ll see “Niche Triva has X friends. “

Now here is the code:

<strong>
Niche Triva has
<u>
<?php
$xmlfriends = simplexml_load_file(“http://www.twitter.com/friends/ids/nichetrivia.xml“);
$x=0;
$idsfriends=array();
foreach($xmlfriends->children() as $child)
{
$idsfriends[$x]=$child;
$x+=1;
}
echo “$x”;
?>
</u>
friends.
</strong>

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”.

<ids>
<id>15796096</id>
<id>19313145</id>
<id>18268881</id>
<id>23756726</id>
<id>22743026</id>
<id>27210555</id>
<id>23443596</id>
<id>25737522</id>
<id>28701269</id>
<id>13820332</id>
<id>18999248</id>
<id>24762237</id>
</ids>

Leave a Reply