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 » jQuery » Display Last Tweet

Display Last Tweet

$.getJSON("http://twitter.com/statuses/user_timeline/username.json?callback=?", function(data) {
     $("#twitter").html(data[0].text);
});

Make sure you change username.json to your actual username and #twitter to the actual selector you wish to update.

Subscribe to The Thread

  1. Love it!

  2. But how can I show the last several tweets?

  3. Add this
    callback=twitterCallback2&count=1
    after this callback=?

    Change the 1 to as many as you’d like.

  4. I tried to use the

    callback=twitterCallback2&count=1

    It doesn’t seem to work for some reason…

    $.getJSON("http://twitter.com/statuses/user_timeline/boombapgr.json?callback=twitterCallback2&count=3", function(data) {
    $("#tweets").html(data[0].text);
    });

    Connecting to Twitter…

  5. Yeah, similar sort of thing, can get the one on it’s own to work, but no more than that…

  6. For those having an issue with more than one item, it’s because you have to put it inside of an array if it’s more than one item.

    Something like:
    instead of:

    (data){
    
    });

    use

    (data){
     for (i = 0; i < item.length; i++) {
                var each = item[i];
    
     }
    }
  7. Will Twitter limit the number of jSON requests my server makes? In other words, if my site gets 1000 hits per hour should i be caching the tweet in a database? Or does twitter see it as the client making the request rather than my web server?

    • This site does OK without it… but… I probably should be caching. Caching something like this is just a good practice period. Regarding the limits, I’m not sure there is on stuff like just pulling from the timeline. I think it’s stuff like search that is limited. Could be wrong though.

  8. you can also refer to this site on how to display your latest tweets in twitter. with twitter API.

    http://www.ryscript.co.cc/web/how-to-display-latest-tweets-using-javascript-and-twitter-api/

    thanks..

  9. what if i dont want to get tweets from twitter i want to show it from my own mysql database any suggestions ?

  10. Nice simple code..
    I gonna use that one..
    Thanks

  11. It displays anchors as text on my page:

    http://www.eclipse-webdesign.nl/contact

    Any ideas?

  12. Eclipse

    works… thanks!

  13. If you want to display your tweets with working links/hashtags/usernamelinks you could use this:


    function parseTweet($text) {
    $pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
    '@([A-Za-z0-9_]+)';
    $tweet = preg_replace('/(^|\s)#(\w+)/', '\1#\2', $text);
    $tweet = preg_replace('/(^|\s)@(\w+)/', '\1@\2', $tweet);
    $tweet = preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is", "\\1[link]", $tweet);
    return $tweet;
    }

  14. This is awsome, I’ll try to implement this like now!!! :D

  15. This did not work :(

  16. @Daniel & others – Bob Kruithof’s code did not work for the following reasons:

    He did not replace the html brackets with their ascii counterparts (as it tells you to do below), therefore the links in the code have been displayed as actual links in your browser (as you can see they are rendered in blue & you can click on them…) and
    He hadn’t escaped some of the quotes in the last preg_replace.

    The following code works perfectly. Just copy & paste it inside the div on your page where you want your tweet to be displayed, and change ‘YOURNAMEHERE’ to your name.

    <?php

    function parseTweet($text) {
    $pattern_url = ‘~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i’;
    ‘@([A-Za-z0-9_]+)’;
    $tweet = preg_replace(‘/(^|\s)#(\w+)/’, ‘\1#<a
    href=”http://search.twitter.com/search?q=%23\2″ rel=”nofollow”>\2</a>’, $text);
    $tweet = preg_replace(‘/(^|\s)@(\w+)/’, ‘\1@<a
    href=”http://www.twitter.com/\2″ rel=”nofollow”>\2</a>’, $tweet);
    $tweet = preg_replace(“#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is”, “\\1<a
    href=\”\\2\” title=\”\\2\” rel=\”nofollow\”>[link]</a>”, $tweet);
    return $tweet;
    }

    $username=’YOURNAMEHERE’; // set user name
    $format=’json’; // set format
    $tweet=json_decode(file_get_contents(“http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}”)); // get tweets and decode them into a variable

    $theTweet = parseTweet($tweet[0]->text);

    echo ‘"’ . $theTweet . ‘"’;

    ?>

    • Thomas

      This above code does not work if copy and pasted. The pattern_url variable closes php ‘?>’ which I’m not sure is the intention – if it is than its never reopened which gives you the error. Not sure how to fix as I don’t know how to use patterns and preg functions. Anyone have any ideas?

    • had the same problem… just replace the all the ‘ and the ” — with ‘ and “…

      works for me…

  17. This is not working in IE at the moment? Any ideas?

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 ~