← Back to blog home

Load latest twitter tweet in 1 line of PHP code

09 Nov

If all you're looking to do is load your latest tweet in the simplest way possible this is the function you need. Luckily Twitter formats all of the latest posts into simple RSS feeds which makes it SUPER easy to just go and get them. It honestly doesn't get any easier than this.

 

	function loadTweets($user){
		return simplexml_load_file("http://twitter.com/statuses/user_timeline/".$user.".xml");
	}

 

Usage

	// be sure to replace "USER_NAME" with your actual username
	$firstTweet=loadTweets("USER_NAME");
	$firstTweet=$firstTweet->status->text;
	echo($firstTweet);

It's that simple you now can put your latest tweet where ever you want. This code actually loads all your recent tweets with all of the information. So it will work if you wanted to get a bit fancier and include time stamps or other information.

Want an easy way to get the latest Facebook post too?

 
 

Tags: , , , ,

Leave a Reply

Notify me of future comments

(I don't spam or share your e-mail. Unsubscribing is as easy as clicking the "unsubscribe" link in the notifications)
 

 

 
  1. Ash Blue

    April 15, 2011 at 10:56 pm

    Realistic this and Twitter feed scripts do not have a fail safe against the outside website. Social media always goes down at some point so people using this should re-work this code in preparation for that. Other than that this is perfect for expanding on.

     
  2. Lenny

    May 7, 2011 at 12:17 pm

    Agreed this is totally a starting point. I usually cache them in to a database using a cron every 10 minutes or so and just pull form the database to display on the site.

     
  3. Jaap

    August 17, 2011 at 2:16 am

    This also does not cover the rate limits of Twitter. Loading a user’s timeline feed can only be done 150 times per hour.

    In this case the user feed should be stored on the server and not retrieved every time the page loads.

     
  4. Lenny

    September 21, 2011 at 12:53 pm

    Agreed… with or without the page limit you should cache the latest posts on your server. I usually just use a simple MySQL table to store them and use a cron to check it every few minutes. The added bonus there is if twitter or facebook go down your site can still show the latest updates that you have.