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
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);
?>
What others are reading
Loading…
John
13 Jun, 2009
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?
phoenix.heart
13 Jun, 2009
Hi John, this snippet can be used anywhere (with PHP supported of course). I tested it again and it works.
John
13 Jun, 2009
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?
phoenix.heart
13 Jun, 2009
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?
John
13 Jun, 2009
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?
phoenix.heart
13 Jun, 2009
Yes, you can
John
13 Jun, 2009
Awesome!
mm…can you instruct me how? I’m a newbie to this stuff.
phoenix.heart
13 Jun, 2009
The easiest way is to rename your page into .php instead of .html
John
13 Jun, 2009
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?
John
14 Jun, 2009
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?
Tommy
16 Oct, 2009
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/
Chris Schmitz
4 Feb, 2010
Shouldn’t the “my_username” variable be “username” like the var that’s used in the function?
phoenix.heart
4 Feb, 2010
Chris, it’s unnecessary. $username is the parameter of the function, when $my_username is a global variable.
Wardell
15 May, 2010
great code, short and sweet, I only wish it could skip tweets begining with @
Matt Say
22 Jun, 2010
Awesome snippet! I will use this in my theme widgets now on
Thanks for sharing.
nico
24 Jan, 2011
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 ?
chris
22 Mar, 2011
it works for one of my accounts but not the other
Nice
27 Jan, 2012
Is there a way I can use this but echo back in plain text? not html, not clickable links?
Ahmad Mestarihi
12 Sep, 2012
Hi, I have tried this code but it always returns my first tweet not the latest. Can you help solve it?