Display Twitter RSS feed using PHP

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.

$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);
foreach($xml->channel->item as $twit){
if(is_array($twitterPosts) && count($twitterPosts)==$amountToShow){
$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'));

die('Can`t fetch the feed you requested');

a, a:link, a:visited, a:hover {
.twitter {
 display:table-cell; vertical-align:middle;
<!-- Insert your html here -->
<div class="twitter" id="jstweets">
 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 '';
 echo 'No Twitter posts have been made';//Error message
  • deseth

    I get this: Parse error: syntax error, unexpected T_ECHO in C:\xampp\htdocs\demo\ovhrss.php on line 48

    • http://awordpress.net Pavel

      Thanks for the feedback! There is a missing semi-colon at the end of line 46

  • http://shafawihamzah.com Shafawi Hamzah

    Your code has a small error. In line 48 it should be written like this echo “. By the way thanks for the code :)