Code snippet #1 - Get Latest Tweet

Ok so I’m following some guy’s recommendation (sorry, I really forgot his name as well as his blog address) to try to post something useful even when I don’t have anything to blog about. How about a series of code snippets? To some they are old but to the others they may help - for example to myself two months later that is.

Code Snippet #1 - Get Latest Tweet

Purpose

  • Get your latest tweet from Twitter to show up instead of an unconvincing “Follow Me” link -or-
  • Display your idol’s latest tweet to wow your readers

Requirements

  • A Twitter account. Don’t have one? Head here or here

The Code

<?php
// the function
/**
 * @desc Get latest tweet from a Twitter account
 * @param string The account's username
 * @return string The tweet
 */
function get_latest_tweet($username)
{
    $url = "http://search.twitter.com/search.atom?q=from:$username&rpp=1";
    $content = file_get_contents($url);
    $content = explode('<content type="html">', $content);
    $content = explode('</content>', $content[1]);
    return html_entity_decode($content[0]);
}
// now use it
$my_username = 'Phoenixheart';
echo get_latest_tweet($my_username);
?>

  • Is this only for WordPress sites? I changed $my_username and put the code in my website but it didn’t show up. Is there something else I need to do?

  • Hi John, this snippet can be used anywhere (with PHP supported of course). I tested it again and it works.

  • Hi phoenix, thanks for replying :)

    So, basically this is what I did.

    1) Change ‘Phoenixheart’ to my Twitter username.
    2) Put the entire code in a div.

    Now after I do this, a part of the code (below) shows up in the div and no tweet.

    This is what shows up:
    ‘, ); = explode(”, ); return html_entity_decode(); } // now use it = ‘davidchoimusic’; echo get_latest_tweet(); ?>


    Did I miss something?

  • From what I see, the code doesn’t get executed by PHP. Are you sure the code is in a php page, not a static html page?

  • Yeah I think you’re right. It’s a static html page. Can I copy that code into a php file and call it somehow?

  • Yes, you can :)

  • Awesome!

    mm…can you instruct me how? I’m a newbie to this stuff. :(

  • The easiest way is to rename your page into .php instead of .html

  • I want to put this code in my vBulletin header and I’m not sure how to find the html page. Also, I noticed that in my header, there are other bits of code that call for other php pages so wouldn’t I have to change everything then if I rename the page to php?

  • Nevermind! I figured it out by following these instructions: http://www.vbulletin.com/forum/showthread.php?t=173937&highlight=file+html

    I guess my only other question is, would it be difficult to exclude @ replies?

  • I have created a plugin that allows this with wordpress. you dont have to write a single line of php code (but you can if you want to).http://www.leitom.no/2009/10/last-tweet-wordpress-plugin/

  • Shouldn’t the “my_username” variable be “username” like the var that’s used in the function?

  • Chris, it’s unnecessary. $username is the parameter of the function, when $my_username is a global variable.

  • great code, short and sweet, I only wish it could skip tweets begining with @

  • Awesome snippet! I will use this in my theme widgets now on
    Thanks for sharing.

  • i have wordpress but with a cut and copy in the header didn’T show me the tweet
    i put the code in an existing <DIV>
    no error …what is wrong ?

  • it works for one of my accounts but not the other
     

  • Is there a way I can use this but echo back in plain text? not html, not clickable links?

  • Hi, I have tried this code but it always returns my first tweet not the latest. Can you help solve it?

You can follow any responses to this entry through the RSS 2.0 feed.