how to retrieve and display your latest tweet using PHP

Twitter 5 by jasonh1234 on deviantART

Retrieving your latest tweet from twitter is a very simple process. First, you need to request your latest tweet from twitter using its API and then parse the xml response and select your tweet. It’s much simpler than it sounds!

You can request your latest tweet from twitter just by using a simple URL: By replacing YourUserName with your twitter username, you’ll get an xml file containing your latest tweet (you can request more tweets by changing the “count=1” with the appropriate number of tweets) along with some information about your profile.

Using the following PHP code, you’ll get the content of the xml response to the $buffer variable:

Now that you have the xml content, you just need to parse it so you can retrieve your tweet. Using SimpleXML, which is a PHP extension that allows users to easily manipulate/use XML data, the content is converted to a set of nodes. The $status variable holds the “status” node and then the $tweet variable gets the child-node named text, which is your tweet. Finally, the tweet is displayed using the echo command.

Simple, right?

You can also use an alternative way: how to store and retrieve your latest tweet from an XML file

Creation by ~jasonh1234

  • Linkan

    I´ll try this out. I have been using the the other more popular method which uses twitter search instead of the rss. To make a long story short that method does not work very well for several reasons. Hoping this will be a winner. Thanks!

  • Silas Latzke

    appreciated lots, I must comment that your blog is fantastic!

  • PM


    After 150 hits per hour the page is broken if we access the twitter page. Do you know any other ways to display tweets without breaking the page?


  • Stathis

    @PM: If you mean that twitter doesn’t allow you to retrieve your tweets more than 150 times per hour, then simply retrieve them using a cron job every X minutes, save them to an xml file at your server and for every user request parse this file instead of performing a new query using the twitter’s API.

    If you mean something else please provide an example. :-)

  • Vinod

    This was really helpful. Thanks.

  • Umre

    I found your website while I was looking for solution about retrieving latest twitter messages. I assume that post was written in early twitter api so I wonder if there is any solution except this one ? is that your only solution that you deal ?

    Thank you

    Btw I respect your snippet and I will use it