How to Use:PHP Code:
<?php
class Twitter_Feed_Reader
{
var $feed = Array();
function __construct($user_id = false)
{
if ($user_id)
$this->load($user_id);
}
function get($parent, $name, $item = 0)
{
return $parent->getElementsByTagName($name)->item($item);
}
function load($user_id)
{
$twitter = new DOMDocument();
$twitter->loadXML(file_get_contents('http://twitter.com/statuses/user_timeline/' . $user_id . '.rss'));
$rss = $this->get($twitter, 'rss');
$channel = $this->get($rss, 'channel');
$username = explode(' / ', $this->value($channel, 'title')); // split Twitter & Username
array_shift($username); // remove Twitter
$username = implode(' / ', $username); // in case Username contains slashes
$link = $this->value($channel, 'link');
$atom_link = $this->value($channel, 'link', 1, 'href');
$items = $channel->getElementsByTagName('item');
$this->feed[$user_id] = Array();
foreach ($items as $item)
{
$this->feed[$user_id][strtotime($this->value($item, 'pubDate'))] = Array(
'description' => substr($this->value($item, 'description'), strlen($username) + 2),
'link' => $this->value($item, 'link')
);
}
}
function output($user_id, $template)
{
if (!array_key_exists($user_id, $this->feed))
$this->load($user_id);
$midnight = time() - gmdate('G') * 60 * 60 - gmdate('i') * 60 - gmdate('s');
foreach ($this->feed[$user_id] as $time => $data)
{
$date = false;
if (gmdate('d m y', $time) == gmdate('d m y', $midnight))
$date = '<strong>Today</strong> at ' . gmdate('g:ia', $time);
else if (gmdate('d m y', $time) == gmdate('d m y', $midnight - 86400))
$date = '<strong>Yesterday</strong> at ' . gmdate('g:ia', $time);
else
$date = gmdate('d M. Y, g:ia', $time);
echo str_replace('{date}', $date,
str_replace('{link}', $data['link'],
str_replace('{description}',
preg_replace('/\@(.+?)\b/', '@<a href="http://twitter.com/$1" rel="nofollow" title="View $1\'s Twitter Profile">$1</a>',
preg_replace('/(https?\:\/\/)(.+?)(\s|$)/', '<a href="$1$2" rel="nofollow" title="$2">[link]</a>',
$data['description']
)
),
$template
)));
}
}
function value($parent, $name, $item = 0, $attribute = false)
{
return ($attribute ? $this->get($parent, $name, $item)->getAttribute($attribute) : $this->get($parent, $name, $item)->firstChild->nodeValue);
}
}
?>
$feed_reader = new Twitter_Feed_Reader();
$feed_reader->output($numerical_user_id, $html_template);
Example:
$feed_reader = new Twitter_Feed_Reader();
$feed_reader->output(75376101, "<q>{description}</q>\n<cite><a href=\"{link}\">{date}</a></cite><br />\n");
Outputs:Code:<q>The server is back online.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4893105057">15 Oct. 2009, 4:59pm</a></cite><br /> <q>Server is down for an unknown period of time. Nameservers are offline. See you when they return.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4828704115">13 Oct. 2009, 5:45am</a></cite><br /> <q>A wild Imakuni? appears! See what the fuss is about: <a href="http://forums.offlinegamers.com/imakuni/" rel="nofollow" title="forums.offlinegamers.com/imakuni/">[link]</a></q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4755707691">10 Oct. 2009, 6:27am</a></cite><br /> <q>Soon to hold a contest for top poster and/or top referrer. Giving away expensive accounts. Get in on the action! <a href="http://bit.ly/107Bl9" rel="nofollow" title="bit.ly/107Bl9">[link]</a></q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4639080884">05 Oct. 2009, 10:10pm</a></cite><br /> <q>Graphics & Programming Forum Opened: <a href="http://bit.ly/HztF2" rel="nofollow" title="bit.ly/HztF2">[link]</a></q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4612427470">04 Oct. 2009, 9:47pm</a></cite><br /> <q>Installed a banking/money system. Get a head start on earning OGG!</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4587784628">03 Oct. 2009, 9:19pm</a></cite><br /> <q>Haven't finished tweaking settings, but *registration is live*. Get a head start in the new OG community: <a href="http://forums.offlinegamers.com" rel="nofollow" title="forums.offlinegamers.com">[link]</a></q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4570861109">03 Oct. 2009, 3:17am</a></cite><br /> <q>Installing the forums. Should be ready in a few days. Tell your friends to get ready and to follow us on Twitter. :-)</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4558544227">02 Oct. 2009, 5:37pm</a></cite><br /> <q>Just underwent a huge coding overhaul to improve performance speeds.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4517543114">01 Oct. 2009, 4:46am</a></cite><br /> <q>Postponing forum installation due to cPanel issues. Will work on it in-depth first thing tomorrow.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4459675564">29 Sep. 2009, 2:09am</a></cite><br /> <q>Found a server! We should be officially opening soon.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4434721052">28 Sep. 2009, 4:07am</a></cite><br /> <q>@<a href="http://twitter.com/KaienLG" rel="nofollow" title="View KaienLG's Twitter Profile">KaienLG</a>: We're just waiting on a server that can support us. I'd make a custom user system, but that would only postpone release.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4420650660">27 Sep. 2009, 5:07pm</a></cite><br /> <q>Updated the OfflineGamers Twitter profile with a custom-made design.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4411264799">27 Sep. 2009, 5:34am</a></cite><br /> <q>Altering the colors of and adding random effects to the Gaia Online images in order to avoid any copyright issues.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4367957707">25 Sep. 2009, 1:19pm</a></cite><br /> <q>Coding the OG template and making it look similar to Gaia Online, except better.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4314925373">23 Sep. 2009, 1:15pm</a></cite><br /> <q>Both AIM and YIM support accounts have been registered under the name OfflineGamers.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4164491819">22 Sep. 2009, 3:56am</a></cite><br /> <q>Just finished the basics of the Twitter feed reader. Now to program it into the soon-to-be OfflineGamers.com.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4128318032">20 Sep. 2009, 6:24pm</a></cite><br /> <q>OfflineGamers.com is now in our possession; waiting for the server to be up in the next few weeks.</q> <cite><a href="http://twitter.com/OfflineGamers/statuses/4108807298">19 Sep. 2009, 7:53pm</a></cite><br />
Results 1 to 1 of 1
Thread: [PHP] Twitter Feed Reader
- 19 Nov. 2009 07:41am #1
[PHP] Twitter Feed Reader