How to import and display your latest tweets

Categories: Web design - Tags: ,

If you are an active Twitter user, chances are that you want to share your latest tweets on your website or blog. To do that it’s quite easy, just use the following PHP snippet.

<?php // import & display latest tweet
$username = "perishable";
$feed = "http://search.twitter.com/search.atom?q=from:".$username."&rpp=2";
// $prefix = '<h3>Perishable Press</h3>';
// $suffix = '<p><a href="http://twitter.com/'.$username.'">Follow me on Twitter</a>';
function parse_feed( $feed ) {
  $stepOne = explode( '<content type="html">', $feed );
  $stepTwo = explode( '</content>', $stepOne[1] );
  $tweet = $stepTwo[0];
  $tweet = str_replace( "&lt;", "<", $tweet );
  $tweet = str_replace( "&gt;", ">", $tweet );
  return $tweet;
}
$latest_tweet = file_get_contents( $feed );
// echo stripslashes( $prefix ) . parse_feed( $latest_tweet ) . stripslashes( $suffix );
echo parse_feed( $latest_tweet );
?>

mirkohumbert

Comments

Write a comment

You must be logged in to write a comment.

« »