Easily Add Twitter Status to any page with PHP
Tuesday, February 16th, 2010Quick post– I found a lot of answers to this problem but none that really worked for me…
This will output your latest twitter status, converting any URLs or email addresses to clickable links:
function get_status($twitterid) {
$xml = @simplexml_load_file('http://twitter.com/statuses/user_timeline/'.$twitterid.'.xml?count=1');
if($xml->status) {
foreach($xml->status as $status){
$text = $status->text;
$text = html_entity_decode($text);
$text = " ".$text;
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1" target=_blank>\\1</a>', $text);
$text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1" target=_blank>\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
'<a href="mailto:\\1" target=_blank>\\1</a>', $text);
$text .= "<h4>Posted from $status->source</h4>";
}} else {
return 'Trouble in Twittersville, API not loading, sorry!';
}
return $text;
}
Then, to call it, just use this function:
get_status('YOUR_TWITTER_ID');
with your numerical Twitter ID in the obvious spot. You can find your Twitter ID by looking at the URL for your Twitter RSS feed, which will be:
http://twitter.com/statuses/user_timeline/YOUR_TWITTER_ID.rss
So, in accordance with my plan to switch client communication from phone to email, I have decided that, instead of a Cell Phone, I will carry a