Parse a Twitter Feed With PHP

By       
   

here.


Apache/2.4.18 (Ubuntu) Server at dcode.com.au Port 80
" data-via="andrewsirianni">Tweet

In this post, we will go though how to read Tweets from a Twitter Account into a PHP Array so that they can be used on a website, or in a localised program. To do this, we will be using the Twitter Timeline API and the PHP DOM Parser.

In reading this post, please note that while there may be opportunities to simplify the code, I have kept it as is to ensure that it is easy to follow.

To begin with, let's understand the process of what we are intending on doing. In short:

  1. First we want an RSS feed of our tweets; then
  2. We want to be able to read that feed into a PHP Array; then
  3. We want to add relevant links to content, add links to Twitter users and localise the timestamp of the tweet.

Step 1: Find the Twitter RSS Feed

The Twitter RSS Feed can be found as follows:

https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=andrewsirianni&count=5

You should change the andrewsirianni (my Twitter name) to your own. And change the 5 to replicate the number of items that you wish to read.

Step 2: Read the Feed into a PHP Array

Based on this, we can create a simplified function called get_tweets() to return a PHP Array of tweets for a specific Twitter user:


function get_tweets($username = 'andrewsirianni', $count = 5) {
  $url = "https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=$username&count=$count";

  $doc = new DOMDocument();
  $doc->load($url);

  $tweets = array();
  foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array (
      'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
      'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
      'localDate' => '',
      'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
      'source' => $node->getElementsByTagName('twitter:source')->item(0)->nodeValue
    );

    array_push($tweets, $itemRSS);
  }

  return $tweets;
}

This ensures that when we run the get_tweets() function, we will be returned with an array of the tweets. But this on its own does not give us the full functionality associated with the tweet. So we need to do a little more tinkering.

Step 3: Link to Content, Link to Twitter Users and Localise the Time

To add this functionality, we can create a number of additional functions that can be called as we parse the Twitter RSS feed.

The first of these is to add links to the text. So if there is a link in the Tweet, we will want to add a hyperlink to it. Using code originally found at http://css-tricks.com/snippets/php/find-urls-in-text-make-links, I have created the following function to Add Links to a tweet:


/* add links to text */
function add_links($text) {
  /* Original Source: http://css-tricks.com/snippets/php/find-urls-in-text-make-links */
  $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

  if(preg_match($reg_exUrl, $text, $url)) {
    return preg_replace($reg_exUrl, "<a href=\"{$url[0]}\">{$url[0]}</a> ", $text);
  } else {
    return $text;
  }
}

Now we need a function to add a link to Twitter users - basically we're searching the Tweet for anything with an "@" at the front of it and adding a link to the relevant Twitter account. This function is as follows:


/* add links to twitter users */
function link_twitter_users($input) {
  /* Original Source: http://stackoverflow.com/questions/4766158/php-to-replace-username-with-link-to-twitter-account */
  $input = preg_replace('/(?<=^|\s)@([a-z0-9_]+)/i', '<a href="http://www.twitter.com/$1">@$1</a>', $input);

  return $input;
}

Now the one last thing to do is localise the timestamp of the Tweet. The RSS feed presents the timestamp as being at UTC time. So we need a function to parse this date/time and convert it into something more meaningful:


/* localise time */
function localise_time($time) {
  $time = strtotime($time);
  $dateInLocal = date("g:ia, j F Y", $time);
  return $dateInLocal;
}

Now we need to put it all together.

I have modified the original get_tweets() to:

The final get_tweets() function is as follows:


function get_tweets($username = 'andrewsirianni', $count = 5) {
  $url = "https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=$username&count=$count";

  $doc = new DOMDocument();
  $doc->load($url);

  $tweets = array();
  foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array (
      'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
      'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
      'localDate' => '',
      'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
      'source' => $node->getElementsByTagName('twitter:source')->item(0)->nodeValue
    );

    $itemRSS['title'] = '@' . $itemRSS['title']; // add leading "@" to ensure the script links to OUR account
    $itemRSS['title'] = add_links($itemRSS['title']);
    $itemRSS['title'] = link_twitter_users($itemRSS['title']);

    $itemRSS['localDate'] = localise_time($itemRSS['pubDate']);

    array_push($tweets, $itemRSS);
  }

  return $tweets;
}

To use this function, call the function get_tweets('USERNAME', 'NUMBER') (changing the USERNAME and NUMBER to your required values) and the output will be an array of parsed Tweets!

Enjoy! And please share your comments!



comments powered by Disqus

About Me

I design & develop software that runs on the Internet. As a qualified analyst, accountant and real estate agent, I can deploy systems that improve performance.

Categories


Warning: DOMDocument::load(https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=andrewsirianni&count=5): failed to open stream: HTTP request failed! HTTP/1.0 410 Gone in /home/andrxvjf/public_etc/templates/rmargin/twitter.tpl.php on line 8

Warning: DOMDocument::load(): I/O warning : failed to load external entity "https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=andrewsirianni&count=5" in /home/andrxvjf/public_etc/templates/rmargin/twitter.tpl.php on line 8

Recent Tweets

Follow me: @andrewsirianni




Copyright ©2012 Andrew Sirianni, All Rights Reserved   LinkedIn Profile