29Dec/096
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 } ?> |

December 29th, 2009 - 13:29
Thanks for sharing this snippet! I’m sure this could be useful in many applications.
January 18th, 2010 - 02:28
Fatal error: Call to undefined function apc_fetch() in D:\wamp\www\UCPNEW\twtr.php on line 8
January 18th, 2010 - 09:22
You need to install APC to use the APC caching functions.
http://www.php.net/manual/apc.installation.php
June 13th, 2010 - 11:14
There is a bug in this for 32-bit systems. The intval() function will overflow and always returns 2147483647 once Twitter message IDs have exceeded this number (which they have). Because of this, the elements in the $updates[] array will continually overwrite themselves because they are all using the same index.
It has been suggested on php.net that using the addition operator to convert the string can get around this, for example:
$num = ’100000000000 ‘ + 0;
I have not thoroughly tested this myself but it seems to work.
June 13th, 2010 - 13:44
If this doesn’t work for you (it seemed to give me problems on another machine), an alternative is to keep the ID as a string and pad the left side with zeroes using strpad(). Padding IDs to an equal length ensures that alphabetical sorting works as expected.
July 18th, 2010 - 10:09
Changing this line:
'/(#[a-z1-9_]+)/i');… to this:
'/#([a-z1-9_]+)/i');And this line:
'<a href="http://twitter.com/search?q=$1" rel="nofollow">$1</a>');… to this:
'<a href="http://twitter.com/search?q=%23$1" rel="nofollow">#$1</a>');… will increase compatibility with the Google Chrome browser.