If you’re using a CMS you can simply find a handy plugin that displays your Twitter feed on your site, but if you’re working in a non-CMS framework then it’s more tricky. Here’s a handy piece of code you can use to get your twitter feed as well as any tweets that mention your name. I have also provided a simple function that uses regex to turn any URLs, hash tags or @mentions in your tweets into clickable links.
Here is the code to get the tweets you need – I have not stated what each of the lines do as the variable names are fairly self-explanatory, so if you would like a deeper understanding of it then I would recommend having a look directly at the XML that is processed here.
function getTweets($username = false, $count = 5) {
//Fetch data and process response
$request = "http://search.twitter.com/search.atom?q=$username";
$response = file_get_contents($request);
$response = str_replace("twitter:", "", $response);
$xml = simplexml_load_string($response);
//Loop through specified amount of entries
for($i=0;$i<$count;$i++)
{
$id = $xml->entry[$i]->id;
$id_parts = explode(":",$id);
$tweet_id = array_pop($id_parts);
$account_url = $xml->entry[$i]->author->uri;
$image_url = getXMLattribute($xml->entry[$i]->link[1],"href");
$name = trim($xml->entry[$i]->author->name, ")");
$name_parts = explode("(", $name);
$real_name = trim(array_pop($name_parts));
$screen_name = trim(array_pop($name_parts));
$published_time = strtotime(trim(str_replace(array("T","Z")," ",$xml->entry[$i]->published)));
$tweet_url = getXMLattribute($xml->entry[$i]->link[0],"href");
$tweet = $xml->entry[$i]->content;
$tweet = str_replace(array("<b>", "</b>"), "", $tweet);
/* Now just create your HTML output */
}
}
Once you have your tweets you need to make the relevant parts clickable. Unfortunately due to the regex code used in this function I am unable to post the text of the function to do this in the blog post, so instead I have placed it in this text file (opens in a new tab). You simply need to run the tweet content through that function and it will make the URLs, hash tags and @mentions clickable.
UPDATE: You can see these functions in action here: http://www.brothersstreep.com/ – in this case I actually used the Twitter handles of all the members of the band, so it includes tweets from (and mentioning) a few different Twitter users.







Hey,
i’m using these functions and i like them quite a lot. I had to fix the getTweet() though because the getXMLattribute() on the links didn’t work for me.
Instead i used: $xml->entry[$i]->link[0]["href"]
I dumped all the variables into an array with an additional $username check so i’d end up with just the Tweets instead of both Tweets and Replies.
I also added some more regex to the twitify() because it didnt put a target=”_blank” on some of the links.
Combined with a previously written xtimeAgo() function which adds the Twitter timeformat (4 hours ago) to a timestamp, this becomes a really strong package to customize a Twitterfeed on a website.
Thanks a lot for this one!
Great – I’m glad you could find it useful. I also have a handy timeAgo() function that I use to display the post time for tweets. Could you maybe post your fixes to twitify() here? It would help me to improve the post a bit.
You can actually see it all in action here: http://www.brothersstreep.com/ (this example actually includes the Twitter handles of each of the members of the band, not just one user).
I saw your example, it looks pretty nice and powerfull. I actually embedded it the same as you did, with multiple feeds depending on the page.
My fix to twitify() is 1 regex, it’s a preg_replace() with the following:
$pattern = “/]+)>/i”;
$replace = “”
$subject = str_replace(‘target=”_blank”‘,”,$str);
The str_replace() is to make sure i don’t apply 2 targets to a link.
Ah crap, it get’s stripped, i’ll email it.
Brilliant – thanks for that. Looks like your fix makes sense – I’ll definitely implement that.
I am looking to create a custom feed that will capture tweets that contain certain key words and allow me to select tweets of specific twitter users in my feed. How would I do this?
You can use the same code I’ve got here, except instead of using a Twitter name as the keyword, replace it with whatever keyword you like. You can use Twitter usernames or random words to search on with the same result.
If you have multiple words you would like to search on then the query string will look something like this:
http://search.twitter.com/search.atom?q=keyword1%20OR%20keyword2%20OR%20keyword3 etc…