Mashing up the Twitter API with PHP

<?php
$username = "youraccountname";
$password = "yourpassword";
$url="http://twitter.com/statuses/friends/londonstreetlif.xml";
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec ($curl); //remove space before parenthesis
curl_close($curl);
?>

Now lets use SimpleXML to loop through the returned xml and create an array of my friends:

<?php
$friendsArr=array();
$feed = simplexml_load_string($result);
foreach($feed->user AS $userXML){
   $user = simplexml_import_dom($userXML);
   $friendsArr[]=(string)$user->screen_name;
}
?>

Lets assume we have done the same thing to create an array of my followers, now we can find everyone that I am friends with but doesn’t follow me:

<?php
$badfriendsArr=array_diff($friendsArr,$followersArr);
?>

And finally lets display, the lists are returned from Twitter in the order the relationship began from newest to oldest, so lets order alphabetically and print:

<?php
natcasesort($badfriendsArr);
foreach($badfriendsArr AS $badfriend){
   echo "<a href="http://www.twitter.com/$badfriend">$badfriend</a><br>";
}
?>

Leave a Reply