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

TWITTER HAS CHANGED ITS API METHOD THIS TUTORIAL WILL NO LONGER WORK.

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 = "http://twitter.com/statuses/friends_timeline.xml?count=5";

$tw = curl_init();

curl_setopt($tw, CURLOPT_URL, $tweets);

curl_setopt($tw, CURLOPT_USERPWD, $login);

curl_setopt($tw, CURLOPT_RETURNTRANSFER, TRUE);

$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=\"http://www.twitter.com/ashleyford\">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=\"http://www.twitter.com/\\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=\"http://www.twitter.com/", $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>";}

curl_close($tw);

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: http://twitter.com/statuses/friends_timeline.xml?count=5 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.

UPDATE:

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.

http://twitter.com/statuses/public_timeline.xml

Users Timeline.

http://twitter.com/statuses/user_timeline.xml

Shows a users @replies

http://twitter.com/statuses/replies.xml

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

http://twitter.com/statuses/followers.xml

demodownload



More tutorials from Papermashup
Comments
80 discussions around Using the Twitter API
Older Comments
  1. Cheri says:

    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.

    Thanks
    Cheri

  2. Lucinda says:

    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.

  3. Christie says:

    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
    often!

  4. 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!

Older Comments




Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.