29
Dec/09
3

Snippet: Fetch Twitter Feed (With Retweets) in PHP

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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
}
?>

The output looks like this

Similar Posts:

 

Author: Steve

Steve is the owner of UlraMega Tech. He is a freelance Web designer and developer who specializes in PHP and AJAX development.
Comments (3) Trackbacks (0)
  1. Thanks for sharing this snippet! I’m sure this could be useful in many applications.

  2. Fatal error: Call to undefined function apc_fetch() in D:\wamp\www\UCPNEW\twtr.php on line 8

Leave a comment


No trackbacks yet.

Page optimized by WP Minify WordPress Plugin