Use PHP to add your Twitter feed to any page
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 “;}
6 Responses to 'Use PHP to add your Twitter feed to any page'
Leave a Reply
You must be logged in to post a comment.
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
Thank you! That’ll cut out several operations.
ljmacphee
15 Jul 09 at 9:13 pm
Hi,
Not sure if you still use this, but I found this via Google. I’ve updated the script somewhat:
channel->item;
for($a=0;$a<numOfTweets;$a++) {
$tweet = $tweets{$a};
echo "”.utf8_decode(preg_replace(
array( “@https?\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?@i”,
“@#([a-zA-Z0-9]+)@i”,
“@\@([a-zA-Z0-9]+)@i”),
array( “$0“,
“$0“,
“$0“),
substr(stristr($tweet->description,’: ‘), 2)));
echo ” link.”\”>#“;
// echo $tweet->pubDate; // if you want to show the date/time
}
?>
ollybenson
17 Oct 10 at 3:36 pm
Here’s my updated code:
channel->item;
for($a=0;$a<numOfTweets;$a++) {
$tweet = $tweets{$a};
echo "”.utf8_decode(preg_replace(
array( “@https?\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?@i”,
“@#([a-zA-Z0-9]+)@i”,
“@\@([a-zA-Z0-9]+)@i”),
array( “$0“,
“$0“,
“$0“),
substr(stristr($tweet->description,’: ‘), 2)));
echo ” link.”\”>#“;
// echo $tweet->pubDate; // if you want to show the date/time
}
?>
ollybenson
17 Oct 10 at 3:39 pm
No, sorry, doesn’t want to paste properly into the comments box.
ollybenson
17 Oct 10 at 3:40 pm
Thank you. I was buried in code all day I just came up for air.
WP is finicky about scripts in comments because of all the cross-site scripting hacks I’m impressed you got it to take.
I stopped using it because if twitter.com went down it makes the website load too slowly to be useful. PHP has a long built in timeout.
Do you know any way to shorten that timeout?
timestocome
18 Oct 10 at 7:48 pm