Moving on from my last post about displaying data from RSS on your page using php this post will show you how to display your latest Twitter Posts on a XHTML page without using any third party software packages. I used the same code as the previous post however this time I had modified the way it displayed, to see an example of this in action see the ShaneDJ Homepage The code I have used is below.
<?php
$rss_url = "http://twitter.com/statuses/user_timeline/15298261.rss";
if (!$rss_data = @file_get_contents($rss_url)) {
echo "<p>Ooooops</p><p>Looks like Shane's Twitter feed isn't working at the moment.</p>";
} else {
$rss_xml = SimpleXML_Load_String($rss_data);
$channel_title = $rss_xml->channel->title;
$channel_link = $rss_xml->channel->link;
foreach ($rss_xml->channel->item as $item) {
$item_title = $item->title;
$item_link = $item->link;
$item_description = $item->description;
$tweet = explode(": ", $item_title);
$tweetmessage = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" rel=\"nofollow\">$1</a>", $tweet[1]);
$tweetmessage = preg_replace("/(@[^\s]+)/", "<span class=\"twit_at\">$1</span>", $tweetmessage);
echo "<li><b>Shane tweeted</b> - " . $tweetmessage . " - ";
echo "<a href=\"" . $item_link . "\" rel=\"nofollow\">View in Twitter</a></li>\n";
}
}
?>
First things first you need to change your $rss_url to the location of your RSS twitter feed. Then If you look at the following lines.
$tweet = explode(": ", $item_title);
This line will split the $item_title this is needed so that we can split the title into two parts. Part 1 is the username and part 2 is the actual message.
$tweetmessage = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" rel=\"nofollow\">$1</a>", $tweet[1]);
$tweetmessage = preg_replace("/(@[^\s]+)/", "<span class=\"twit_at\">$1</span>", $tweetmessage);
The next set of lines change the format of certain elements, the first one uses regex to find and replace the URL’s and make them actively clickable. Note the use of nofollow there too. The second line formats other users by looking for their @username names and changing the style of it.
echo "<li><b>Shane tweeted</b> - " . $tweetmessage . " - "; echo "<a href=\"" . $item_link . "\" rel=\"nofollow\">View in Twitter</a></li>\n";
The last 2 lines display all of the data in the way that we have declared it. At the end of the message we have a View in Twitter link. This will take you to the actual post. Good for people that want to reply to your message.
Once again to see this in use check out my homepage.
















Nice script Shane. Challenge: take it a little further and cache the XML periodically to save the need for frequent requests.
This suggestion will truly make this script worth while. Only a suggestion
Thank you for the script. You made my day))
But script has a small bug.
Wolverine_Ru: winter session is closed now (it’s works)
Wolverine_Ru: RT @radio_t: Радио-Т 171 http://bit.ly/6B7kVK #radiot (not working, becausу string contains two characters “:”)
AFTER
$tweet = explode(": ", $item_title);ADD
if (strstr($item_title, ' RT ')) $tweet[1] .= ": " .$tweet[2]."";Fuck) Wolverine_Ru: bug: bug: bug: bug: bug: not working
DELETE (not correct check) if (strstr($item_title, ‘ RT ‘)) $tweet[1] .= “: ” .$tweet[2].”";
ADD for ($i=2; $i<=substr_count($item_title, ": "); $i++) $tweet[1] .= ": " .$tweet[$i]."";
Final
Returning to the problem. More elegant solution for print actual message
$item_title = substr($item_title, strpos($item_title, ‘: ‘) + 2);