This blog has moved here: | FOLLOW ME ON TWITTER @woork
Thursday, June 25, 2009

Super simple way to work with Twitter API (PHP + CSS)

In this post I want to illustrate a super simple way to work with Twitter API and PHP. In particular, this tutorial explains how to get public updates from Twitter public timeline and display them in a web page with a custom style using CSS. In order to get Twitter updates I used Twitterlibphp (a PHP implementation of the Twitter API) that allows you to take advantage of it from within your PHP applications.

Using this simple method you can obtain awesome results like this:

You can download the source code at the following link and reuse it for free in your web projects (you need PHP and APACHE):

1. A little introduction
Twitterlibphp returns Twitter timeline in XML format and with this structure (take a look at the Twitter API home page for a full list of available nodes.):


So, if you want to display the user image, user status, and status date in your timeline you have to choose the following nodes:

But how you can display them? It's very simple! Take a look at the following code!

2. PHP code
Create a new file twitter_status.php and copy and paste the following code:

<div class="twitter_container">
// require the twitter library
require "twitter.lib.php";

// your twitter username and password
$username = "your_username";
$password = "your_password";

// initialize the twitter class
$twitter = new Twitter($username, $password);

// fetch public timeline in xml format
$xml = $twitter->getPublicTimeline();

$twitter_status = new SimpleXMLElement($xml);
foreach($twitter_status->status as $status){
foreach($status->user as $user){
echo '<img src="'.$user->profile_image_url.'" class="twitter_image">';
echo '<a href="'.$user->name.'">'.$user->name.'</a>: ';
echo $status->text;
echo '<br/>';
echo '<div class="twitter_posted_at">Posted at:'.$status->created_at.'</div>';
echo '</div>';

How you can see, the previous code is very simple to understand. The line:

$xml = $twitter->getPublicTimeline();

get the 20 most recent public statuses posted. You can also use other functions such as:

getFriendsTimeline(): returns the 20 most recent statuses posted by the authenticating user and that user's friends.

- getUserTimeline(): returns the 20 most recent statuses posted from the authenticating user.

getReplies(): returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.

Take a look at twitter.lib.php for the full list of available functions.

If you want to display different information, for example the source of the update (status->source) use this code:


...or if you want to display the number of followers of the current user (follower_count) use this code:


3. CSS Code
Now you can customize the style of your timeline using CSS code. I used the following classes but you can customize the look how you prefer:


margin: 0 auto;

.twitter_container a{



border-bottom:solid 1px #DEDEDE;


border:solid 2px #DEDEDE;




That's all! Download the source code, open twitter_status.php, change $username and $password with your Twitter username and password and upload the file in your test server.
If you have some suggestion, please add a comment!

Simple PHP Twitter Search ready to use in your web projectsSend messages from a PHP page using Twitter APITwitter API: How to create a stream of messages Monitter-like with PHP and jQuery

blog comments powered by Disqus
Spencer said...

Very nice...

mario said...

Hey thanks mate for this good tutorial... :))

LastWebdesigner said...

Great article Antonio.

Hirvesh said...

Tutorial Antonio! Real nice.

ChristianoErwen said...

Why give me an invitation to error in twitter.lib to the 510 line?

Yoosuf said...

its cool

Webexpert said...

Hi Antonio, thanks a lot! I was looking for that! It works fine! :)))

Image Hosting said...

Thank you

nilicule said...

Small comment: Apache is not a requirement, any webserver that supports PHP will do :)

drewmclellan said...

It's generally a bad idea to try and interact with the Twitter API using the same process your user is connected to. i.e. fetching tweets as the page loads is never going to end well. If Twitter is running slow, your page is going to run slow too.

It's far better to use another process to fetch the tweets (such as a PHP script run periodically by cron), stash them in a local cache, and then have you page display data from the cache.

If Twitter is down, then your site is still fast to load and will still display the most up-to-date tweets that are available.

Anthony said...

hey, nice tutorial mate, just a quick question. How do you limit your results to say, show only 5 latest tweets? instead of showing all 20?

Dave said...

AWESOME!!! Thanks man!

José Pacheco said...

amazing job..i´ll delicious in my acc

JoeLongstreet said...

Nice work!

Mohan Desai said...

Good work man,

Jordy said...

Thank you! We only talked today about integrating our twitter account to our website.

Dinesh Verma said...

Hey buddy your are the best....

Hendriono said...

Awesome friend... Thanks for your tuts...

Rainer Bendig said...

It should be a closing div ;)

paul said...

i agree with drewmclellan! but still a good idea though thanks for sharing!

David Fitzgerald said...

Antonio, can you tell me how to add the "Share this post" links like you have at the end of each of your blog posts?

If it's not a secret of course! ;-) Thank you in advance!

Anonymous said...

Grande articolo, molto utile.
Grazie mille.


belhana said...


Evans said...

This is a very cool tutorial - I will be trying it out later in the week when I get home to my trusted computer :)

Thanks for posting it.

Daniel K said...

Wow, nice tutorial!
If it is possible, could you make a tutorial showing how to post and delete new tweets with this API and PHP?

Flash Buddy said...

I'm missing the part that creates those wonderful alternating row colors. No even/odd in the code I downloaded. I do appreciate the ease of use. I've been checking out

So far it has been easy to work with. Don't know if I'll get those alternating rows that I covet.

Trey said...

Super tutorials.

NgocHau said...

Can you help me?
I used your code -> error
Fatal error: Call to undefined function curl_init() in D:\phpbb source\xampplite\htdocs\twitter_woork\twitter.lib.php on line 510

Web server
# Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
# MySQL client version: 5.1.30

Hau Nguyen

zen24 said...

can you please answer to this question:

Parse error: parse error, unexpected T_CLASS in /home/hosting_users/ok50/www/twitter/status/twitter.lib.php on line 39

have a goodday.

  • Twitter Follow woork on Twitter
  • RSS Feed Subscribe to stay up to date
  • Podcast Coming soon...
  • 0 delicious lovers save
Share your links. Do you want to suggest any interesting link about web design or tech news? Submit your link.
Submit a News