July 31, 2009

Display Your Latest Tweet With PHP

I get asked, literally some times every so often, the best way to integrate your latest tweet into Wordpress. There are a few ways of doing this, but the way I’ve come up with is, I think, pretty nifty.

The first way is using Twitter’s profile widget creator, which displays tweets in a little box which you can change the colours and dimensions of. It’s actually not bad, but it’s a bit generic. It looks the same, despite colour differences, on everyone’s website, and it probably wouldn’t work with a site design similar to mine.

The next solution is using one of Twitter’s other widgets, which are hidden a little on the Twitter site (you need to go into Applications on the Twitter goodies page, not Widgets — it’s linked above for your convenience, though). You can choose between a Flash or HTML widget. The Flash version is similar to the previous widget, in that it looks pretty much the same everywhere, despite colour options. The HTML one is much, much better though. It allows for simple, easy CSS styling. I liked this method so much that I used it on the previous version of nickheer.com

The problem with the HTML widget is that, for whatever reason, it doesn’t seem to work smoothly 100% of the time. I think it’s a Javascript/Wordpress/Safari 4 issue, but it doesn’t matter. In addition, if you want to style it in-depth, it can be a royal pain.


However, I recently found a post on the Smashing Magazine site which described a method for displaying the latest tweet via PHP. This intrigued me, and I implemented it into this revision of the website, and immediately I ran into a huge problem.

This PHP method seems to get easily confused by symbols (<, > and & all seem to be problematic) and links. Bad news for me, since I seem to tweet links quite often. Note that this also applies to @replies, because the username is linked. After much trial-and-error, I think I’ve cracked the perfect way to show your latest tweet(s).

Update: e-sushi posted a much better version of this in the comments. Thanks for the new code.

Click here for the UPDATED code.

The beauty of this code is that it replaces common symbols with their appropriate equivalents, and it parses links perfectly. The bulk of the code is from the Smashing Magazine article, but all of the string replacement code is by me. Please don’t forget to put your Twitter username on line 2.


 

Comments

  1. I´ve tried this out and can sadly report this method just does not work very well. Using Twitter search to get your latest tweet has a few problems:

    1. It does not always return a result (sometimes seems to have to do with how old the latest tweet is)
    2. It fails to return certain tweets containing special characters etc.

    This pretty much never happens when getting the rss feed for a specific user (to the best of my knowledge). So I´m looking to find a php solution for parsing the rss instead although after a bit of googling on the subject it looks as if I´ll have to learn som php myself.

    • If you have a live version of it running somewhere, please feel free to email me at nick@nickheer.com. It runs fine here, on my site, but I’d really like to troubleshoot what’s going on to make it work universally. Thanks for letting me know of the problem.

  2. I’m going to roll this into a new theme I’m developing, so I’ll let you know how it works. Thanks for posting it.

    I encountered the same special character issue with the original script, and i was just about to roll up my sleeves on fixing it when I discovered your post!

  3. You should really use PHP functions when they do the right thing.
    All that replace stuff actually messes up in utf-8, breaking the text up badly and rendering it into unvalid, broken html.

    Therefore, this code is more simple and more correct:

    <?php
    $username = "your_url_name_at_twitter";
    $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";

    function parse_feed($feed) {
    $stepOne = explode("”, $feed);
    $stepTwo = explode(“”, $stepOne[1]);
    $tweet = $stepTwo[0];
    $tweet = htmlspecialchars_decode($tweet,ENT_QUOTES);
    return $tweet;
    }

    $twitterFeed = file_get_contents($feed);
    echo(‘"’.parse_feed($twitterFeed).’"’);
    ?>

  4. Bah, wordpress breaking code in comments… grrr.
    Ok, used pastie.org so grab the correct code over there at

    http://pastie.org/974423

  5. e-sushi, that codes great. nice and clean, exactly what i was looking for.

  6. Pastie.org isn’t available for some reason, is there another way to grab the code?

  7. [...] have found the solution on NickHeer.com ! Go to this page to get the code to display your latest tweets very simple with PHP or grab it [...]

  8. You can also get the Twitter feed as a JSON object for PHP5+, which makes it waaaaay easier to parse, you can return it as an array:

    function GetTwitterFeed()
    {
    $username = "myname";
    $feed = "http://search.twitter.com/search.json?q=from:" . $username . "&rpp=1";
    $twitterFeed = file_get_contents( $feed );
    return = json_decode ( $twitterFeed );
    }

 

Leave a Reply