How to Create a Twitter Feed on Your Web Site

Twitter has quickly become one of the most popular social networking sites online. It is currently ranked among the top twenty Web sites in the world and has over a million users. I have only recently hopped on the bandwagon and so far I have been able to connect with many interesting people and discover tons of great online resources. I added a Twitter feed into my footer once I got things going and I have been refining the code ever since. I finally have it at a point where I think all of the bugs are ironed out and it is ready to share.
The Twitter API is pretty easy to use and I decided to work with the Search API Method. I found that sometimes Twitter doesn’t respond due to high traffic on the site so instead of just using the API to return an Atom Feed, I decided to use is to return a Json file.
First, let’s setup our username and the number of Tweets we want to display.

$username = "your-user-name";
$num = 5;
$feed = "http://search.twitter.com/search.json?q=from:" . $username . "&rpp=" . $num;

Next we need to copy the Json file to our server, just in case the Search API doesn’t respond during our next attempt to grap our Tweets.

$newfile = dirname(__FILE__)."/twitternew.json";
$file = dirname(__FILE__)."/twitter.json";
copy($feed, $newfile);
$oldcontent = @file_get_contents($file);
$newcontent = @file_get_contents($newfile);
if($oldcontent != $newcontent) {
copy($newfile, $file);
}
$tweets = @file_get_contents($file);
$tweets = json_decode($tweets);

This will also check to see if any new Tweets have been added before it attempts to copy over the new Json file.
To finish it all off, we need to display our Tweets. This examples outputs them into an unordered list.

echo "
    "; for($x=0;$x<$num;$x++) { $str = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","\\0", $tweets->results[$x]->text); $pattern = '/[#|@][^\s]*/'; preg_match_all($pattern, $str, $matches); foreach($matches[0] as $keyword) { $keyword = str_replace(")","",$keyword); $link = str_replace("#","%23",$keyword); $link = str_replace("@","",$keyword); if(strstr($keyword,"@")) { $search = "$keyword"; } else { $link = urlencode($link); $search = "$keyword"; } $str = str_replace($keyword, $search, $str); } echo "
  • ".$str."
  • \n"; } echo "
";

Let’s put it all together and wrap it in a PHP tag.

";
for($x=0;$x<$num;$x++) {
$str = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","\\0", $tweets->results[$x]->text);
$pattern = '/[#|@][^\s]*/';
preg_match_all($pattern, $str, $matches);
foreach($matches[0] as $keyword) {
$keyword = str_replace(")","",$keyword);
$link = str_replace("#","%23",$keyword);
$link = str_replace("@","",$keyword);
if(strstr($keyword,"@")) {
$search = "$keyword";
} else {
$link = urlencode($link);
$search = "$keyword";
}
$str = str_replace($keyword, $search, $str);
}
echo "
  • ".$str."
  • \n"; } echo ""; ?>

    To see this script in action, just check out my footer.
    Twitter Bird image provided by Pasquale D’Silva and Function.

    Share this:

    Email
    Facebook
    Twitter
    Pinterest
    Pocket

    Premium Themes for WordPress

    Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

    Use this WordPress website builder to build powerful websites in no time for your or your clients.

    WordPress Hosting

    WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

    Bluehost – providing quality web hosting solutions since 1996.

    About the author

    Picture of Luke Perrie

    Luke Perrie