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’);
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’);
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();
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’);
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; ?>
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:
To call a function we need to use the following…[php] $item->FUNCTION_NAME();
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.