Herself's Webtools

Scripts, HowTos, Templates, Plugins, Widgets, Tips and Useful Information

Use PHP to add your Twitter feed to any page

with 2 comments

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 “;

}

Written by timestocome

August 18th, 2008 at 5:00 am

2 Responses to 'Use PHP to add your Twitter feed to any page'

Subscribe to comments with RSS

  1. You might be interested to know that === is what you use to test the result of strpos. It knows the difference between null and zero.

    gingerbeardman

    15 Jul 09 at 3:37 am

  2. Thank you! That’ll cut out several operations.

    ljmacphee

    15 Jul 09 at 9:13 pm

Leave a Reply

You must be logged in to post a comment.