Use your left/right keys to browse tutorials
Using the Twitter API

Using the Twitter API

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on January 22, 2009


Here’s a simple introduction on using the Twitter API. The API offers many different ways to connect to your details including xml, json, atom, and rss. Im going to use xml, php and curl to read an xml file to get my latest followers tweets and display them in the format below.

This method of parsing the xml file requires PHP5 and uses the SimpleXMLElement class.

The Code

$login = "username:password";

$tweets = "";

$tw = curl_init();

curl_setopt($tw, CURLOPT_URL, $tweets);

curl_setopt($tw, CURLOPT_USERPWD, $login);


$twi = curl_exec($tw);

$tweeters = new SimpleXMLElement($twi);

$latesttweets = count($tweeters);

if ($latesttweets>2) {

	echo "<h3>".$latesttweets." latest tweets from the users I follow | <a href=\"\">Follow Me!</a></h3>";


## Printing/Dumping the data

foreach ($tweeters->status as $twit1) {

$description = $twit1->text;

      $description = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"\\2\" >@\\2</a>'", $description);
$description = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $description);
$description = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $description);

echo "<div class='user'><a href=\"", $twit1->user->screen_name,"\" target=\"_blank\"><img border=\"0\" class=\"twitter_followers\" src=\"", $twit1->user->profile_image_url, "\" title=\"", $twit1->name, "\" /></a>\n";
echo "<div class='name'>", $twit1->user->name,"</div>";
echo "<div class='followers'>", $twit1->user->location,"</div>";
echo "<div class='location'>", $twit1->user->url,"</div>";
echo "<div class='text'>".$description."<div class='description'>From ", $twit1->source,"</div></div></div>";}


The first thing you need to do to connect to the API is to add your login details in the variable $login, this is required to authenticate the connection to the API. Next we specify which xml file we want to get data from in our case we’re targeting the friends_timeline: at the end i’ve appended a parameter that limits the number of results to 5, you can change this to anything up to 20.

Then using curl we can authenticate the connection and parse the xml file using the SimpleXMLElement class in PHP5. Then we cycle through the results in a ‘foreach’ loop and display the results on the page. you can then style it with CSS.


To enable @reply links and tiny url links in your updates i have added regular expression, the new code is above, i have also updated the demo

you can change the xml file, to target different user data, you may have to tweek the code to point to the correct xml tags. Here is a list of xml files to play with.

Twitter Public Timeline.

Users Timeline.

Shows a users @replies

Shows a users friends list up to 100 (with most recent tweet)


More tutorials from Papermashup
  • Cheri

    HELP… so some strange reason my Twitter Feed use to work and now it doesn’t. I create my websites using Adobe Muse (I am a designer not a developer) and a buddy of mine sent me code that worked awesome and I tweaked it a wee bit to get the font colour and leading I wanted.

    Can someone have alook at my site (looking at the source file) and tell me what the heck has happened? I would love it if someone can find the problem and email me the changes or suggestions.


  • Lucinda

    Hey I know this is off topic but I was wondering
    if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  • Christie

    Howdy! I could have sworn I’ve been to this site before but after checking through some of the post I realized it’s new to
    me. Anyways, I’m definitely happy I found it and I’ll be bookmarking and checking back

  • Proxy Twitter – Private Fast Proxiy, specifically deisgned for Twitter software, Turbocharge Twitter Promotion!

    I used to be suggested this web site by way of my cousin. I’m no longer sure whether this submit is written by way of him as nobody else realize such precise approximately my problem. You are wonderful! Thanks!