Use PHP to add your Twitter feed to any page

Posted by ljmacphee on August 18, 2008 under hack your template, how to, php |

While I was building my wonder of me portal page for TimesToCome I thought it’d be neat to add in my twitter stream.

Here is some very simple PHP code to do so. Now I was lazy and I only check for one link in the tweet. 99% of the time that is sufficient. The only thing you need to change is to change the user name from timestocome to your username. Any other changes are optional.

$username = “timestocome”;
$tweets = fetch_rss(’http://twitter.com/statuses/user_timeline/’.$username.’.rss’);

foreach ( $tweets->items as $tweet ){

print “<hr>”;

$twit = ” “.substr(strstr($tweet['description'],’: ‘), 2, strlen($tweet['description'])).” “;
$link = $tweet['link'];

// hack around php null == 0 ugh!
$xtwit = “x” . $twit;
$mark = strpos( $xtwit, “http://” );

if ( $mark > 0 ){ // we have a link in our tweet perhaps more than one

$l = substr ( $xtwit, $mark, strlen($xtwit) );
$l = substr ( $l, 0, strpos( $l, ” ” ));
$full_link = “<a href=\”$l\”>$l</a>”;
$xtwit = str_replace ( $l, $full_link, $xtwit );
$xtwit = substr ( $xtwit, 1, strlen($xtwit));

print “<br><p>$xtwit <a href=\”$link\”>#</a></p>”;

}else{

print “<br><p>$twit <a href=\”$link\”>#</a></p>”;

}
$time = $tweet['pubdate'];
$time = substr( $time, 0, strlen($time)-6);
print  ” $time “;

}

Add A Comment

You must be logged in to post a comment.