Spawning 32,678 processes took an average of 3.06 µs/process of CPU time and 4.01 µs/process of wall-clock time LOL.

Follow me

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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/

  • Hey i have the script working fine on http://www.mcgarritydesign.com/tweet.php but i want it to auto generate onto http://www.mcgarritydesign.com/twitter.html such as in a image which i will put on once it generates how would i do it i tried <class=”tweet.php”> and i tryied <action=”tweet.php”>

  • Sean, I don’t really understand what you mean, but class and action definitely have nothing to do here…

  • 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.

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