How to fetch Twitter feed (including retweets) in php

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to fetch Twitter feed (including retweets) in php

Post by Neo » Sun Feb 28, 2010 11:04 pm

Here is a PHP that fetches a Twitter stream for a specified user. It also combines retweets with the regular updates. This puts them all in an array to make it easy to output them however you want.

This snippet uses APC to cache the data, but you can use any caching method such as memcached or database. It should be fairly easy to edit in your own caching.

Code: Select all

<?php
$tw_user = ''; // Twitter username
$tw_pass = ''; // Twitter password
$tw_key = 'twitter'; // Unique key to cache tweets
$tw_count = 5; // Number of tweets to get
$tw_timeout = 300; // How long to cache tweets in seconds
 
$updates = apc_fetch($tw_key);
if(!$updates) {
   $timelines = array();
   $tw_auth = $tw_user . ':' . $tw_pass;
   $timeline_url = 'http://twitter.com/statuses/user_timeline.xml?count=' . $tw_count . '&screen_name=' . $tw_user;
   $retweet_url = 'https://api.twitter.com/1/statuses/retweeted_by_me.xml?count=' . $tw_count;
 
   // Get regular tweets
   $curl = curl_init();
   curl_setopt($curl, CURLOPT_URL, $timeline_url);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   $timelines[] = curl_exec($curl);
   curl_close($curl);
 
   // Get retweets
   $curl = curl_init();
   curl_setopt($curl, CURLOPT_URL, $retweet_url);
   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
   curl_setopt($curl, CURLOPT_USERPWD, $tw_auth);
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   $timelines[] = curl_exec($curl);
   curl_close($curl);
 
   $updates = array();
 
   // Set up replace patterns
   $patterns = array('#(http\://[^\s]*)#i',
               '/@([a-z1-9_]+)/i',
               '/(#[a-z1-9_]+)/i');
   $replace = array('<a href="$1" target="_blank">$1</a>',
               '@<a href="http://twitter.com/$1" target=_blank">$1</a>',
               '<a href="http://twitter.com/search?q=$1" target=_blank">$1</a>');
   foreach($timelines as $timeline) {
      $xmlDoc = DOMDocument::loadXML($timeline);
      $statuses = $xmlDoc->getElementsByTagName('status');
      if($statuses->length > 0) {
         foreach($statuses as $status) {
            $id = intval($status->getElementsByTagName('id')->item(0)->nodeValue);
            $text = $status->getElementsByTagName('text')->item(0)->nodeValue;
            $date = strtotime($status->getElementsByTagName('created_at')->item(0)->nodeValue);
            $text = preg_replace($patterns, $replace, $text); // Parse links
            $updates[$id] = array($text, $date);
         }
      }
   }
 
   krsort($updates); // Sort tweets by id
   $updates = array_slice($updates, 0, $tw_count); // Limit tweet count
 
   apc_store($tw_key, $updates, $tw_timeout); // Cache data
}
?>
Post Reply

Return to “PHP & MySQL”