This script or function allows you to read and display twitter feeds on your website, in html format instead of the java widgets on Twiter.com. This helps with seo etc
The function uses the xml feed provided by twitter and searches through it collecting the data, you just need the id and username of the feed you want to view/display.
These are provided by twitter.com, to get this info go to twitter and on the right hand side you will see a link 'View RSS feed of XX' (XX is the username of the person you want)
click on this and in the url take a note of the number at the end.
Using Jonathon Ross as an example you will see his feed url is http://twitter.com/statuses/user_timeline/17753033.rss so his id will be
17753033
//function to get the latest tweets from xml file provided by twitter.com
function getTweets($rss_ref, $username, $number_to_show=5){
//setup return array just in case an error occurs a result will still be returned
$tweets=false;
//load contents of xml file into variable
$data=@file_get_contents("http://twitter.com/statuses/user_timeline/" . $rss_ref . ".rss");
//if the $data var is not empty we have retrieved some tweets
if (!empty($data)){
//scan file contents for the different xml nodes, and capture the data between them
preg_match_all('!
preg_match_all('!
preg_match_all('!
preg_match_all('!(.*?)!i', $data, $tweet_link);
//loop through the array containing the tweets starting at 1 (0 contains the username)
for($cnt=1; $cnt<=$number_to_show; $cnt++){
//store in array
//remove username from the title
$tweets[$cnt]['Title']=strpos($tweet_title[1][$cnt], $username . ': ') ? $tweet_title[1][$cnt] : str_replace($username . ': ', '', $tweet_title[1][$cnt]);
$tweets[$cnt]['Description']=$tweet_description[1][$cnt];
$tweets[$cnt]['Date']=$tweet_date[1][$cnt];
$tweets[$cnt]['Link']=$tweet_link[1][$cnt];
}
}
//return result
return $tweets;
}
Usage:
To use the function call it like this getTweets(rss_ref, username, how many you want to show)
So if we use wossy again I will call it like this:
$tweets_array=getTweets('17753033', 'wossy', 3);
print_r($tweets);
This should produce something similiar to:
Tue, 02 Feb 2010 14:01:46 +0000 - Wossy: Hi everyone! Still feelng lousy as are the WHOLE family!! But I am getting booted and suited to head off to do the talk show. Seeyalater. - http://twitter.com/Wossy/statuses/8630225031
Tue, 02 Feb 2010 11:35:40 +0000 - Wossy: @ShaunsRockabill Yes I do love him a little bit. I didn't say he was ugly btw, just better looking in real life...gotta love Misfits! - http://twitter.com/Wossy/statuses/8543191498
Mon, 01 Feb 2010 14:54:10 +0000 - Wossy: @mouthwaite did you take the tamiflu ? I am gradually feeling better after 4 days of the course... - http://twitter.com/Wossy/statuses/8538979681
This code was created at a beginner level for beginners, so if you have any comments or improvements, please get in touch and let me know.
(i think there is a way of using preg_match_all to collect all the ate in one go, using names? if you know how please let me know).)
Warning: include(template_bottom.php) [function.include]: failed to open stream: No such file or directory in /home/epxhxnp/public_html/twitter_feeds.php on line 64
Warning: include(template_bottom.php) [function.include]: failed to open stream: No such file or directory in /home/epxhxnp/public_html/twitter_feeds.php on line 64
Warning: include() [function.include]: Failed opening 'template_bottom.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/epxhxnp/public_html/twitter_feeds.php on line 64