post icon

Code Snippet: A custom Twitter feed for your website

TwitterIf 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.

7 Comments

Leave a comment
  1. Sjoerd Adema
    6 April, 2011 at 8:03 pm #

    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!

  2. Sjoerd Adema
    6 April, 2011 at 11:29 pm #

    Ah crap, it get’s stripped, i’ll email it.

    • Hugh Lashbrooke
      7 April, 2011 at 12:12 am #

      Brilliant – thanks for that. Looks like your fix makes sense – I’ll definitely implement that.

  3. Charles Gross
    2 May, 2011 at 8:25 pm #

    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?

Leave a Reply