Getting your latest twitter status using PHP


Twitter is a social netwoking phenomenon. It attracts millions of visitors everyday and it can lead to a website’s popularity. Having your twitter status on your website would be beneficial; your visitors can see your status without actually going to Twitter, which is good for people who are too lazy to open links ;)

We’re loading the xml file from Twitter and then echoing the text using getTwitterStatus

function getTwitterStatus($userid){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=1";

$xml = simplexml_load_file($url) or die("could not connect");

       foreach($xml->status as $status){
       $text = $status->text;
       }
       echo $text;
 }

getTwitterStatus("insert.twitter.username.here");

/*Put your twitter ID above in the brackets*/

Hope this helps!

Arjun
http://www.avn8.com

Join testking ccda course to learn more about web design. Download the testking mcts tutorial and testking mcitp guide to learn about legal issues in web designing.

Related posts:

  1. Creating a simple CSS menu CSS can work wonders, normally making a good CSS menu...
  2. Create a Simple Javascript Clock When you think “time” for a website, you automatically think...

Written by Ben Bate

I'm Ben, a freelance designer from Plymouth, England specialising in UI design. I'm the director of iPhone app development company, PixelBit Apps Ltd and also run Pixel Clouds design blog.

13 Responses to “Getting your latest twitter status using PHP”

  1. @trudesign says:

    In your code block you are missing the first and last would make it hard to use lol…are there other parts in this that should be too?

  2. @trudesign says:

    You should also include more where and how you can add this. Also how to put it into a WP layout.

  3. Lam Nguyen says:

    You will kill your hosting CPU usage if you get over 5k visitors a day, dude! Try to put it in the cache to reduce loadtime or just using javascript :)

  4. Only what I need. Thanks!

  5. Codephase says:

    wow! thanks for sharing!

  6. wawa says:

    In getTwitterStatus (), did you try with that ?
    echo $xml->status->text;

  7. Codephase says:

    just a stupid question: if I want to show older tweets, I just alter the “count” attribute?

  8. Hey, this is nice!
    But I make some modifications in the function, and I think others should appreciate.
    Here are they:

    function getTwitterStatus($userid){
    $url = “http://twitter.com/statuses/user_timeline/$userid.xml?count=1″;
    $xml = simplexml_load_file($url) or die(“could not connect”); //no news above
    $text = utf8_decode($xml->status->text); //you need just this for your last tweet. utf8_decode is for latin characters
    $text = ereg_replace(“[[:alpha:]]+://[^[:space:]]+[[:alnum:]/]”, “\“, $text); //To make links inside anchors :D
    echo $text;
    }
    getTwitterStatus(“yourtwitterhere”);

    It’s only that, thank you very much for your code!

  9. I made*. Sorry about my english.

  10. I wrote the ereg_replace wrong
    here’s the right:
    $text = ereg_replace(“[[:alpha:]]+://[^[:space:]]+[[:alnum:]/]”, “\“, $text);

    Sorry about the mistake.

  11. No… the comment system seems to not accept the entire code… de ereg_replace still wrong.

  12. Hey, you can see that if your last tweet is a retweet, this code don’t work. To fix that, I made this do while estructure:

    $i = 1; do { $url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$i"; $i++; $xml = simplexml_load_file($url) or die("could not connect"); $text = utf8_decode($xml->status->text);
    } while (empty($text));

    I hope this help someone! :D

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>