The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.
The Flash and PHP Bible has a forum for quick support.
An alternative way to using Twitter's JavaScript and Flash approaches.
We all know how annoying it is to not have any customization with the ways Twitter provides to show your latest Tweet. We all also know how annoying it is that your server lags out if the Twitter service is down when using the ways they provide. The obvious alternative method is to use the Twitter API to pull out your latest tweet, but that could cause more problems than it fixes, especially if you are requesting information from the server too much.
So, what's the alternative? RSS. Twitter generates a dynamic feed that allows parameters to be set to limit the results. (Documentation on Formats from Twitter.) For example, here is a feed showing results for my account: http://search.twitter.com/search.atom?q=from:RyanBarr. Notice how my username can be seen from the URL.
Problem here is we are only looking for the latest tweet, so we will use the rpp parameter Twitter has made for us to limit our results to a single tweet: our latest tweet. http://search.twitter.com/search.atom?q=from:RyanBarr&rpp=1.
Pretty boring, right? That's what I expected and exactly why I have developed the following script to make it easy for everyone to implement on the fly:
<?php
// Your twitter username.
$username = "TwitterUsername";
// Prefix - some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = "";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>
You will need to update the $username variable to hold your Twitter username. You will need to be running PHP on your hosting service and the file extension must be .php (unless you are using .htaccess to treat .html files as PHP). Other than that, wherever you place this code on your page is where the information is output. (Have basic PHP knowledge? Try moving this script to a header file and populating a variable that gets displayed later on.)
As this is part one of a four part series there are a few things to understand:
Please look forward to seeing the next three parts of this resource.
Follow Scriptplayground on Twitter (@scriptplay)
|
Jeremy Ricketts Sat Jan 31, 2009 10:00 pm
Just wanted to say thanks. Worked like a charm!
|
|
mkeefe Sun Feb 1, 2009 6:30 pm
@Jeremy - Awesome, glad you enjoyed it.
|
|
Gordy Wed Mar 4, 2009 8:47 pm
Thanks man, it works. Now if I only knew how to make it look good...
I'm such a noob at coding. |
|
hotel Thu Mar 5, 2009 12:37 pm
Nice script
|
|
Amy H. Fri Mar 6, 2009 6:11 am
I am looking for a way to import the recent posts from several users, can I do that with this?
|
|
Shane Sat Mar 14, 2009 9:12 am
I tried tinkering with your script, mainly to get it to display more than just the latest script. I plugged in the URL http://search.twitter.com/search.atom?q=shaneglass&rpp=5
but I still just the get single tweet. |
|
drm Thu Mar 26, 2009 12:20 pm
how can i display my last 2 tweets with this code?
|
|
mkeefe Fri Mar 27, 2009 8:44 pm
@drm - In order to limit the tweets to the last two, simply modify the rpp query parameter that is found in the URL.
Such as: http://search.twitter.com/search.atom?q=mkeefe&rpp=2. |
|
Aggersborg Sun Mar 29, 2009 2:43 am
Is there a way to only show the comment at and leave out the userID?
|
|
mkeefe Sun Mar 29, 2009 5:44 am
@Aggersborg - Yes, change the query parameter to ?from={USER} where {USER} is the twitter id. Such as:
http://search.twitter.com/search.atom?from=mkeefe&rpp=2 |
|
Karin Wed Apr 1, 2009 10:29 am
Hi,
Is there any way to include the time stamp to this gorgeous hack? Thanks! |
|
Willimac Mon May 18, 2009 10:02 am
I wanted to see if anyone else was having the same problem as me. This bit of code has been working great for months, but as of May 11th it has not updated itself. Did something change with Twitter's API?
I have another hack that uses my Twitter RSS feed to have the 20 latest tweets show up on a WP "page" to themselves. And the RSS feed is working fine. Thanks |
|
mkeefe Mon May 25, 2009 12:23 pm
@Willimac - I think Twitter changed their process. I will contact the author of the code and find out.
|
|
Laura Mon Jun 8, 2009 7:43 pm
I'm trying to use this, because it's perfect for what I need, but I'm getting errors:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /wp/wp-content/themes/custom/page_resources.php on line 45 Warning: file_get_contents(http://search.twitter.com/search.atom?q=from:farmadventure&rpp=1) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /wp/wp-content/themes/custom/page_resources.php on line 45 Can you suggest something? I've looked around and can't find anyone else with the same error... :( |
|
mkeefe Fri Jun 26, 2009 7:57 pm
@Laura - That looks like you are unable to load remote files with PHP. You will want to contact your host to get this limitation removed, if they will.
|
|
derp Fri Jul 17, 2009 9:04 am
changing the rpp number doesn't work.
I still only get one tweet. if I look at the rss feed, there will be more than one, but regardless, it will not display anything other than the latest tweet in my blog Do you know what might be wrong? |
|
mkeefe Sun Jul 19, 2009 8:33 am
@derp - I am not sure what may be wrong as it works perfectly for me, maybe Twitter is putting limitations in place I am not aware of.
Here is mine for reference: Last 5 tweets from @mkeefe |
|
ron Thu Aug 13, 2009 3:46 pm
hoe can i show this phpfile in flash as2.0?
|
|
Bill Fri Sep 11, 2009 9:12 am
This is exactly what I needed!!! Thank you so much!!!
|
|
moosyface Sat Sep 12, 2009 11:55 am
Extremely useful. Thanks a ton!
|
|
Vince Sun Sep 13, 2009 1:16 am
Thanks, this was what I was looking for. Very simple and easy to implement.
|
|
Mike Mon Oct 5, 2009 8:38 am
Someone asked about pulling a time stamp with the latest tweet but I didn't see an answer. Any suggestions on how I would do that? Thanks!
|
|
Matt Fri Oct 30, 2009 7:30 am
I have altered the code slightly to also give the time and date of the post. I have wrapped the function up in a class.
class twitter { public function tweet(){ // Your twitter username. $username = ""; // Prefix - some text you want displayed before your latest tweet. // (HTML is OK, but be sure to escape quotes with backslashes: for example href="link.html") $prefix = ""; // Suffix - some text you want display after your latest tweet. (Same rules as the prefix.) $suffix = ""; $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=3"; $twitterFeed = file_get_contents($feed); $stepOne = explode(" for($x=1;$x $parsedfeed = $stepTwo[0]; $parsedfeed = str_replace("<", "<", $parsedfeed); $parsedfeed = str_replace(">", ">", $parsedfeed); $parsedfeed = str_replace(""", "", $parsedfeed); $tweet[$x-1]['post'] = stripslashes($prefix) . $parsedfeed . stripslashes($suffix); } $stepOne = explode(" for($x=1;$x $parsedfeed = $stepTwo[0]; $splitParts = explode("T", $parsedfeed); $dateParts = explode("-", $splitParts[0]); $timeParts = explode(":", $splitParts[1]); $tweetDate = mktime($timeParts[0], $timeParts[1], $timeParts[1], $dateParts[1], $dateParts[2], $dateParts[0]); $tweet[$x-1]['time'] = date("dS M Y", $tweetDate) . " @ " . date("H:i:s", $tweetDate); } return $tweet; } } To access the data, you will need to create a variable to hold the return array (e.g. $tweetArr = $twitter->tweet(); ) and then access each part of the array to output the info (e.g. echo $tweetArr['post'] . $tweetArr['time']; ) Hope that helps. Matt |
|
Matt Fri Oct 30, 2009 11:35 am
Apologies, I didn't fully explain how to output the result you may need. Here is the full code to loop throught the array and output the details:
Create the file above and name it twitter.php In the file you would like to output place this at the top $twitter = new twitter(); ?> That will create an object called twitter which you can use to call the tweet function by doing this: $tweets = $twitter->tweet(); ?> This will create an multidimensional array of tweets. Then we need to loop through the tweets to output the code by doing this: foreach($tweets as $tweet){ echo $tweet['post'] . " "; echo "Posted on " . $tweet['time']; } ?> That should work for you. If you have any problems drop me an email at matt at madesignuk dot com. ;-) |
|
dinesh Wed Nov 11, 2009 3:44 am
Matt, your code doesnt work.
the function throws error. you have a line for($x=1;$x i think its not complete. However i got it done using curl , check out live on my website. |
|
dinesh Wed Nov 11, 2009 3:46 am
oh it blocked my url , no problem though , contact me at info creamylayouts com if you need my code.
|
|
josh Wed Dec 2, 2009 3:44 pm
I get errors with punctuation in the HTML code when the tweet appears on a page. "Oakland's" becomes "Oakland's" and anchor tags go from to
Any ideas? |
|
josh Wed Dec 2, 2009 3:46 pm
Let me try that again:
I get errors with punctuation in the HTML code when the tweet appears on a page. Examples: Oakland's -> Oakland's href="http://www.url.com" -> href="http://www.url.com"> |
|
Fabious Sun Dec 13, 2009 7:12 am
Hello! I've pasted your code to my blog (WORDPRESS) and it worded fine! Althought I'd like to edit the style to become similiar to the orthers headers like (meta and google reader shared items). So, that's my blog www.fcamatti.com. There you'll see what I'm talking about.
Thank you, Fábio |
|
fabianrios Thu Dec 24, 2009 7:59 am
i like the code, sometimes if the account you're using isn't posting often on twitter, it won't work, becouse, twitter apparently doesn't refresh this cases, how can i do to make the script refresh if there is a new post?
|
|
drankur Mon Dec 28, 2009 5:27 am
superb, this is the first script for last tweet on website that worked as mentioned, cheers to author
|
|
seb Mon Feb 8, 2010 12:22 pm
Nice work, this is the simplest tweet feed i've found - i can't believe it's so simple! cheers
|
|
Seb Mon Feb 8, 2010 12:25 pm
Also, rather than replacing <> marks from their html equiv, use $tweet = html_entity_decode($tweet);
|
|
Tracys Thu Feb 18, 2010 8:20 am
Thank you thank you for your script!
Question - I have the feed in my footer on the URL http://www.thegogreenattorney.com/attorneyblog/benefits/ but when the link back to twitter appears it doesn't work. Got any ideas? Really appreciate any help. |
|
David Sat Apr 10, 2010 1:41 am
If you're experiencing a problem with the punctuation
e.g. the apostrophe in "it's" is replaced with "it's" Right after this: $tweet = str_replace(">", ">", $tweet); Add this: $tweet = html_entity_decode($tweet); |
|
5-Squared Sat Apr 10, 2010 2:03 pm
The code was working fine but I ran into an issue and it is no longer working.
When changes my twitter username and it now has an underscore "_" in it. The tweet is no longer being pulled in. Has anyone else ran into this issue? Do you know a fix? Thanks a lot foe the help - great work on this script. |
|
Chantal Fri Jun 4, 2010 5:25 am
I have same problem with the underscore - and also I recently changed it from the original username . My problem is that it pulls Nothing. So it could be either one of those things throwing it off?
|
|
Florent Tue Jun 8, 2010 8:08 am
Sorry, i don't understand the solution for more than one tweet :(
|
|
mkeefe Wed Jun 30, 2010 7:25 am
Hey all,
As Twitter has changed how things work I recommend checking out the Updated Tutorial and see if that fixes the issues you may be having. Thanks, Matt |
|
DAV Wed Dec 1, 2010 6:27 pm
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=10";
I have change rrp even replace username only sow 1 twitt |
|
mkeefe Sun Dec 5, 2010 4:25 pm
@DAV - Given the numerous changes to Twitter I recommend reading the Updated Tutorial.
|
|
Noticias Android .NET Mon May 30, 2011 7:06 pm
Nice!
but ... you know how to remove the links of topics? (the links of #topic or @user and leave other links like url) That will be awesome. I leave you my site for you can see, i put in the center of the site just over the general post (have a big work in blue called twitter :P ) http://www.noticiasandroid.net Regardas! |
|
Charles Fri Jul 1, 2011 5:12 pm
Can you have multiple Twitter account in the same stream?
|
|
mkeefe Thu Jul 7, 2011 6:18 am
@Charles - You used to be able to, however new Twitter limitations no longer make it possible. At one time you could use comma separated options (user1,user2).
|
|
Jaap Wed Sep 21, 2011 7:42 am
Thanks for this code. It's just what I needed. With a bit of css styling this is great.
Thanks for sharing! |
|
xianvalencia Mon Nov 28, 2011 10:00 pm
how about:
$user = "xianvalencia"; $url = "http://search.twitter.com/search.atom?q=$user&rpp=1"; $xml = simplexml_load_file($url); now you have it on the array. print_r($xml); |
|
Tyler Mon Feb 13, 2012 4:09 pm
I am having prolems. I used this script and it works great with my twitter name. I interchanged my username with a friends and it doesnt pull anything. Any ideas?
|
©2004 - 2012 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN