Enquire

PHP- Show Your Latest Tweet On Your Website With PHP

PHP Scripts, Tips & Tricks

In the archives, there’s a post that shows you how to show your latest tweet on your website using jQuery. While that method was OK, I found it rather temperamental. I have a PHP solution, which seems to be more responsive. The code is nice and easy, and will allow you to have something like this on your website:

Latest Tweet

Note: the graphics/styling obviously won’t appear, just the Tweet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
function parse_feed($feed) {
 $stepOne = explode("<content type=\"html\">", $feed);
 $stepTwo = explode("</content>", $stepOne[1]);
 $tweet = $stepTwo[0];
 $tweet = html_entity_decode($tweet);
 return $tweet;
}
 
function showTwitter($username) {
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
 $twitterFeed = file_get_contents($feed);
 return stripslashes(parse_feed($twitterFeed));
}
?>

And to print the Tweet, you just need to pass your Twitter username into the following function:

1
<?php echo showTwitter("YourTwitterUsername");?>

That’s it.

Please let me know if you can either provide a better solution buyessaysweb.com, or improve on the code above.

Comments

  1. M spoke on 19/10/2012

    How do one set this up tho?

    I just pasted it all in a .php document and it didn’t work.

  2. M spoke on 19/10/2012

    Oh I got it to work now. You have to put the functions within php-tags.

  3. M spoke on 19/10/2012

    Any way to show the date it was posted?

Leave a reply

Your email address will not be published. Required fields are marked *

X