PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » PHP » Get Latest Twitter Status

Get Latest Twitter Status

<?php

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;
 }

//my user id kenrick1991
getTwitterStatus("kenrick1991");

?>

Subscribe to The Thread

  1. All I get is a ‘Could not Connect’ Error. Code doesn’t seem work.

    • The code works. There are a couple reasons you might get a “Could no Connect” that come to mind:

      1. Your installation of PHP does not support simple_xml
      2. Your server is not able to connect to an external site.

      The first scenario is probably more likely.

  2. can only receive the latter can not appear more

  3. Another reason for the code to not work properly is that the twitter account used to retrieve the status update may be ‘locked’ (private accounts).

    You should set it up to public status update ;)

  4. Martin

    Thanks for this snippets works perfect for me!

  5. I would advise against using the XML format, at least for now. It appears that retweets are not included in the XML and JSON feeds. That wouldn’t be so bad, except that it doesn’t just skip a retweet – it returns a blank tweet!

    To get around this I use the RSS format. That comes with its own unique challenges and a different XML structure but at least it returns those retweets.

    More info:

    http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline

  6. How would you change it to show the last X tweets instead of just 1?

  7. I implemented the above code but the links in my tweets are not active even though they are active on my twitter page. Have any thoughts?

  8. Walter
    <?php
    
    function getLastXTwitterStatus($userid,$x){
    $url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$x";
    
    $xml = simplexml_load_file($url) or die('could not connect');
    	echo '<ul>';
           foreach($xml->status as $status){
           $text = twitterify( $status->text );
    	   echo '<li>'.utf8_decode($text).'</li>';
           }
        echo '</ul>';
     }
    
     function twitterify($ret) {
      $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret);
      $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
      $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
      $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
    return $ret;
    }
    
    //my user id kenrick1991
    getLastXTwitterStatus('kenrick1991',5);
    
    ?>
    • I know you nearly lost your shit trying to publish this code, but it says everything you need to know in the remember section right by this comment form.

    • Thanks for the code, works perfect!

    • Asmi Rana

      i used code it works for me but i cannot get the date of tweets. Can you help me to get the tweet date.

  9. Not sure if this has been posted, I found a slight error with your foreach loop.

    if you set the ?count=5 (or something other than one) you will only echo the 5th most recent tweet you’ve made. If that is what you’re after, great, but if you want to show the 5 most recent tweets, you’ll need to put the “echo $text;” within the foreach:

    <?php 
    
    foreach ($xml->status as $status) {
      $text = $status->text;
      echo $text."<br />";
    }      
    
    //my user id kenrick1991
    getTwitterStatus("kenrick1991");
    
    ?>

    in the $url, change ?count=1 to ?count=5 or something other than one

  10. Well, it worked perfectly at first, then after refreshing the page, it gave a could not connect to server error. Wonder why it worked initially?

  11. awesome, thank you Chris.

  12. DocHollywood

    Both versions works great for me, now to just stylize it and put it on my homepage for my users. Thanks!

  13. hans chan

    i got it every thing just working fine :) thx Chris

  14. It works intermittently for me. It will be displaying everything fine and then, a moment later, it stops and I get the “Could not Connect” error.

    Any ideas?

  15. Alex Glover

    I was having issues getting it to work and I realized that

    $text = $status->text;

    would need to be

    $text .= $status->text;

    Otherwise the code will only print one.

  16. I made some modifications to the script. Hope this help someone! :)


    <?php
    function getTwitterStatus($userid){
    $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));
    $text = ereg_replace("[[:alpha:]]+://[^[:space:]]+[[:alnum:]/]", "\", $text);
    echo $text;
    }
    getTwitterStatus("nicholascamp");
    ?>

  17. Couldn’t get this to work in mine. I used this url and it worked:

    $url = “http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=$userid&count=1″;

  18. Asmi Rana

    i used @Walter code it works for me but i cannot get the date of tweets. Can anyone help me to get the tweet date.

  19. I’m working with a Drupal install, trying to forego using OAuth and the current iteration of the Twitter module, using this code instead. Unfortunately the “Could not connect” error is site crippling for me. It gives me a white screen and completely locks me out of accessing my site, until the error finally just (randomly) ceases and the code is functional again. Is there any way to stop this behavior, or a bit of code that might work “better” for my needs? Any help is appreciated. Thanks!

  20. I changed a little something in Walter’s code because Greek characters showed up like questionmarks for me.

    So I replaced utf8_decode($text) with html_entity_decode($text) and now my Greek chars are just fine!


    <?php

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

    $xml = simplexml_load_file($url) or die('could not connect');
    echo '<ul>';
    foreach($xml->status as $status){
    $text = twitterify( $status->text );
    echo '<li>'.utf8_decode($text).'</li>';
    }
    echo '</ul>';
    }

    function twitterify($ret) {
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
    $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
    $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
    return $ret;
    }

    //my user id kenrick1991
    getLastXTwitterStatus('kenrick1991',5);

    ?>

  21. I have been checking out different way’s to embed twitter in php and this is by far the best and easiest I’ve seen.

    Is there a way to embed hashtag’s like through twitter search instead of a user id? Would be great for events!

  22. How do I cache my tweet because my page seem to load slower than normal?

  23. Does anybody know how I can use PHP to grab the StatusesCount from a Twitter account and then return to it to a webpage as a plain text number to format with CSS? I am currently displaying the 5 most recent tweets and below have a link ‘view more tweets’ that links to the twitter account but I am keen to say ‘view more tweets, we have x available to read’. Where x is the count that gets returned through the PHP. Can anybody help, I thought it would be easy to find, but having searched for the past two hours I haven’t been able to track down how to do it. Thanks T

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~