Enquiry Email Form

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.

  1. M
    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
    19/10/2012

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

  3. M
    19/10/2012

    Any way to show the date it was posted?

Learning & sharing

This is where we store some of our Web Development thoughts, tips and tricks for both our own uses and for others to learn from. Sharing is caring.

Please feel free to contribute to our blog posts; perhaps even teach us a few tricks of your own. We'd love to hear your thoughts.



X