Most of the bloggers(and developers), have at least once needed to display a twitter feed on their pages. There are a variety of ways of doing that, but today I`ll write about including a twitter feed in your blog using PHP. The code is pretty much straight-forward. The only thing you have to adjust is the amount of tweets you want shown and the username, which feed you`ll be displaying.
<?php $twitterUsername = "earthquake_jp"; $amountToShow = 5; $twitterRssFeedUrl = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$twitterUsername.'&count='.$amountToShow; $twitterPosts = false; $xml = @simplexml_load_file($twitterRssFeedUrl); if(is_object($xml)){ foreach($xml->channel->item as $twit){ if(is_array($twitterPosts) && count($twitterPosts)==$amountToShow){ break; } $d['title'] = stripslashes(htmlentities($twit->title,ENT_QUOTES,'UTF-8')); $description = stripslashes(htmlentities($twit->description,ENT_QUOTES,'UTF-8')); if(strtolower(substr($description,0,strlen($twitterUsername))) == strtolower($twitterUsername)){ $description = substr($description,strlen($twitterUsername)+1); } $d['description'] = $description; $d['pubdate'] = strtotime($twit->pubDate); $d['guid'] = stripslashes(htmlentities($twit->guid,ENT_QUOTES,'UTF-8')); $d['link'] = stripslashes(htmlentities($twit->link,ENT_QUOTES,'UTF-8')); $twitterPosts[]=$d; } }else{ die('Can`t fetch the feed you requested'); } ?> <style> a, a:link, a:visited, a:hover { color:#000; } .twitter { display:table-cell; vertical-align:middle; float:left; width:250px; } </style> <!-- Insert your html here --> <div class="twitter" id="jstweets"> <?php if(is_array($twitterPosts)){ echo ''; foreach($twitterPosts as $post){ $data = $post['description']; echo '<a href="{$post['link']}">'.$data."<br >Updated on: ".date('l jS of F Y h:i:s A',$post['pubdate']).'</a><br ><br >'; } echo ''; }else{ echo 'No Twitter posts have been made';//Error message } ?> </div>