Parsing Twitter feeds with SimpleXML

Parsing Twitter feeds with SimpleXML is quite a simple task. First of all, you need the URL of your Twitter RSS/Atom feed. Then you can use SimpleXML as follows:

$tweets = simplexml_load_file('http://twitter.com/statuses/user_timeline/120345723.rss');
        
    
    foreach($tweets->channel->item as $tweet) {
    
        $raw_title = $tweet->title;
        $title = str_replace('gabromanato:', '', $raw_title);
        $title = preg_replace('/http.+/', '', $title);
        
        $raw_date = $tweet->pubDate;
        $date = str_replace('+0000', '', $raw_date);
        
        $link = $tweet->link;
        
        echo '<li><a href="' . $link . '">' . $title . '</a>' . "\n" . '<div class="pubdate">' . $date . '</div>' . "\n" . "</li>\n";
    
    
    }

I've only removed some unnecessary strings using str_replace() and preg_replace(). You can see the final result here.

Leave a Reply

Note: Only a member of this blog may post a comment.