Tutorial: Display your Latest Tweet on your Website using SimplePie

Get our posts emailed to you with our monthly newsletter, subscribe here.

Okay so if you read my blog you will know I have already posted a tutorial showing you how to add your latest tweet to your website. Well this is part 2, and in part 2 we look at how to add your tweets to your blog/website in a much better way, using SimplePie.

SimplePie makes it so simple to pull in feeds from not only twitter, but any RSS feed, and it allows you to do this with many more options then my previous tutorial! Lets start by visiting https://simplepie.org and downloading the latest copy of SimplePie. Next, unpackage the zip and upload the file simplepie.inc to your server, now your ready to start coding!

Now either create or open your PHP page, then include the file at the top of the page using the following code…

[php] require_once(‘simplepie.inc’);
[/php]

Now you need to tell SimplePie the URL of your Twitter Feed, you can do this by adding the following line of code…

[php] $feed = new SimplePie(‘https://search.twitter.com/search.atom?q=from:USERNAME’);
[/php]

Make sure you change USERNAME to your own Twitter username. Next you need to add in another two lines (shown below). The first line is needed to make sure SimplePie is initiated, the second allows SimplePie to tell what kind of feed is been pulled (ATOM, RSS, etc).

[php] $feed->init();
$feed->handle_content_type();
[/php]

Thats the basic setup done and SimplePie should hopefully be pulling the feed to your website. Now we need to use this feed and display it correctly on the website. Firstly, here is the code in full that should be at the top of your page…

[php] require_once(‘simplepie.inc’);
$feed = new SimplePie(‘https://search.twitter.com/search.atom?q=from:USERNAME’);
$feed->init();
$feed->handle_content_type();
[/php]

Right lets get the Tweet displaying on the page! To do so, we are going to need to call the Tweet itself, the link to the Tweet and also the date the Tweet was Tweeted (alot of Tweets there I know lol). We do so by using a simple foreach loop, which I have listed below…

[php] <?php foreach ($feed->get_items(0, 1) as $item): ?>
<p><?php echo $item->get_description(); ?> <span><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_date(‘D, M j, Y’); ?></a></span></p>
<?php endforeach; ?>
[/php]

So lets explain the code! Earlier we set our feed URL to the value of $feed using SimplePie, now using the foreach loop we can use $feed->get_items(0, 1) as $item to call each tweet one at a time and pass it to the variable $item, which we can then do calls to get the information we need. The two numbers between the parentheses that you can see in the above block of code, can be changed to suit, I have explained what both are below:

  • First number – this is the number of the item you want to start at. Remember that arrays begin with 0, not 1, so for this example, the most recent tweet will be called first. Changing this item to 1 will skip the latest tweet.
  • Second Number – this is the number of items you want SimplePie to return. In this example I have it set to 1, which will only call one tweet.

Now we have our foreach loop setup and we know how to call each tweet, we need to do a few calls to get something displayed on the page. SimplePie has a huge list of functions that can be called, anything from calling Feed author right down to the Feed items date/time, etc, these can all be found here. For our Tweet we will require each items title, link and date. Using the SimplePie documentation, we can see that we need to call the functions within our foreach loop:

  • get_description()
  • get_permalink()
  • get_date()

To call a function we need to use the following…

[php] $item->FUNCTION_NAME();
[/php]

Now we can introduce some HTML to sort out how the page will look. In this example, I have used simple paragraph tags and in between these tags I have called the title and the date of the Tweet. I wrapped the date with span tags so that I can then use CSS to shrink the text size and give more focus on the Tweet text. Finally the date is linked to the Tweet itself over at Twitter.

Now save it all, upload your files and try it out! Fingers crossed your page should be displaying x number of Tweets from your profile. I have included the source code below which you can download, if you have any problems then please leave a comment.

Download Source Files (.zip) | Demo

Posted by Jason Bayless

34 Comments

  1. In case anyone is still struggling with the cache error, I fixed it by moving the cache folder to the site’s parent directory.

  2. Demo link is not working

    1. Thanks for bringing this to my attention, I am fixing this now.

  3. i created this on my wordpress site sidebar.. the tweets are showing but i get this error at the top of the tweets:

    ./cache/23cb175cf72055a35cb29fdd816da49b.spc is not writeable in /home/servername/public_html/site-name/wp-content/themes/theme-name/simplepie.inc on line 1769

    the cache folder is sitting next to “simplepie.inc” and “sidebar.php” and it’s permission is 777… i also deleted it and created it again and i still get the error :(

    any idea?

  4. Hi, great piece of coding. this is excellent.

    I have two questions for you, if that is ok?

    1) DId you manage to find a solution to changing the ‘post time’ to more twitter like, rather than showing the exact date it was posted. ‘posted 1 day ago’ etc?

    2) Is there a possiblity of having a ‘follow me’ link at the end? What snippet of code would I need to add.

    many thanks
    Gary

    p.s if you’d like to see mine in action –> http://www.garyrozanski.com

    :]

  5. @Matthew – You need to create a folder named “cache” inside the folder “twitter_test” and change the permission of “cache” to 777, and not change the permission of “twitter_test” to 777.

  6. you can also refer to this site on how to show the latest tweets using twitter API..

    as easy as 1 2 3

    thanks..

  7. I am using this at http://www.wadehammes.com and it has been working great up til that last tweet being shown. It hasn’t changed in over 3 hours – could this be a Twitter api thing?

  8. This code works great, except for that it doesn’t seem to grab any twitter posts if you haven’t written any for a couple days.

    e.g. not grabbing top three tweets if those tweets are over ~4 days old.

  9. Hey Matt, try this
    $tweet = html_entity_decode($tweet);
    instead of
    $tweet = str_replace(“<“, “”, $tweet);
    it will replace all the html sensitive characters by their display code :)

  10. Отличная статья. Краткость явно Ваша сестра

  11. Works great! The only problem I am having is that it does not seem to handle apostrophes very well. I wrote “don’t” in my tweet and it shows up as “'” on my blog. Other punctuation seems fine. Any ideas why this would happen?

  12. […] #5 – Display your Latest Tweet on your Website using SimplePie […]

  13. Tutorial: Display your Latest Tweet on your Website using SimplePie…

    Okay so if you read my blog you will know I have already posted a tutorial showing you how to add your latest tweet to your website. Well this is part 2, and in part 2 we look at how to add your tweets to your blog/website in a much better way, using S…

  14. @Matthew – have you tried deleting and recreating the cache folder?

  15. @Matt – Hi Matt, can you post me a link so I can see, if you are still having trouble that is :)

  16. @Dean – Try changing your feed url at the top to “http://search.twitter.com/search.atom?q=from:USERNAME&rpp=5” and see if this helps, rrp is requests per page so when you look at the link I just posted above in the browser it should display your latest 5 tweets.

  17. @Mark – I have seen this kinda of thing done in terms of making the date more Twitter like, I will try digg it out if you are still looking (sorry about slow reply)

  18. Stu,

    Thanks for taking the time to put this together.. twice!

    I am having the same problems as pete:
    Warning: ./cache/857b221004948d0eda23992871608943.spc is not writeable in /home/xxxx/public_html/twitter_test/simplepie.inc on line 1779

    I have changed the folder permissions of twitter_test to 0777. However once I change the folder permissions i get an 500 – Internal Server Error.

    Any thoughts would be greatly appreciated

  19. Hi Stu,

    I can plug and play this in 2 mins but the problem I am having now is my one doesn’t display the retweeted article but your one does.

    Any idea?

    1. @Louis – Can you provide me with a link to your website? cheers

  20. Matt Bivins 2009-11-23 at 21:24

    First of all, MANY THANKS for this easy and great way to make this increasingly common need for developers to work! SimplePie+This Code=Awesome.

    However, I was wondering whether anyone has run into a situation where the feed found at: http://search.twitter.com/search.atom?q=from:YOURUSERNAME doesn’t work? Is there a minimum of tweets necessary for Twitter to feed to this address? For clients that have more than a few tweets, this trick works great, but for a new client that has posted three times (@newappblogger), it doesn’t “spit” anything out.

    Anyone have a clue as to what to do? Can you “reset” Twitter?

    Thanks,
    M@

  21. Dean Greasley 2009-11-19 at 03:44

    for some reason only one tweet will appear although there is 5 that I’ve posted on twitter.

    Can anyone help ?

  22. Great! It works really well.

    Thanks!

  23. Ty for share :D i´ll do it someday in some website ;D for now, its saved :D

  24. @pete, you should make your cache folder writable. modify it to 777 as the property of that folder in your FTP software

    anyway, any ideas how “posted yesterday”, “posted a week ago”, “posted in less than 5 seconds” will appear instead of the actualy PHP time i.e. (Y-m-d’). I’ve seen others have this.. thanks…

  25. Hey Stu,

    I am seeing:

    Warning: ./cache/1b22cc348f944dfdb33bc10362007ab7.spc is not writeable in /home/content/d/w/r/dwrklfmduxkxww/html/simplepie.inc on line 1779

    Any ideas?

    (Looks great otherwise =) )

  26. Damn, this works so perfectly! So thanks Stu! I’d be subscribing here and check for new stuffs every day. Your great man! :)

  27. if it works, this would be a beter way to embed this, cause most implementation would fail when they cant connect to twitter and pull the data

  28. @constantx – good question! I will look into this, I am going to update the post shortly to add a section on caching the feed, so I guess if twitter was down the cached feed would display

  29. How does the script handle the call when Twitter is down?

  30. @Emma – Glad I could help, let me know if you get stuck at all :)

  31. I have been looking for something like this so I will be integrating this into one of my blogs as soon as I get a chance. I have downloaded the files. I thank you.

Comments are closed.