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 the server 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.
|
Jeremy Ricketts Sun Feb 1, 2009 3:00 am
Just wanted to say thanks. Worked like a charm!
|
|
mkeefe Sun Feb 1, 2009 11:30 pm
@Jeremy - Awesome, glad you enjoyed it.
|
|
Gordy Thu Mar 5, 2009 1:47 am
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 5:37 pm
Nice script
|
|
Amy H. Fri Mar 6, 2009 11: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 1:12 pm
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 4:20 pm
how can i display my last 2 tweets with this code?
|
|
mkeefe Sat Mar 28, 2009 12:44 am
@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 6:43 am
Is there a way to only show the comment at and leave out the userID?
|
|
mkeefe Sun Mar 29, 2009 9: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 2:29 pm
Hi,
Is there any way to include the time stamp to this gorgeous hack? Thanks! |
|
Willimac Mon May 18, 2009 2:02 pm
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 4: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 11: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 11: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 1:04 pm
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 12:33 pm
@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 7:46 pm
hoe can i show this phpfile in flash as2.0?
|
|
Bill Fri Sep 11, 2009 1:12 pm
This is exactly what I needed!!! Thank you so much!!!
|
|
moosyface Sat Sep 12, 2009 3:55 pm
Extremely useful. Thanks a ton!
|
|
Vince Sun Sep 13, 2009 5:16 am
Thanks, this was what I was looking for. Very simple and easy to implement.
|
|
Mike Mon Oct 5, 2009 12:38 pm
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 11: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 3:35 pm
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 8: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 8: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 8: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 8: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 12:12 pm
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 12:59 pm
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 10: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 5: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 5:25 pm
Also, rather than replacing <> marks from their html equiv, use $tweet = html_entity_decode($tweet);
|
|
Tracys Thu Feb 18, 2010 1:20 pm
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. |
©2004 - 2010 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN