Get Twitter RSS Feeds with CakePHP
Using CakePHP model get your RSS twitter feeds with caching and limits.
Installation:
- Save this script below into your models directory
- Change your Twitter ID and Twitter Name
- Add to your $uses controller variable,
public $uses = array('Twitter')
- Add to your controller method,
$this->set('twits',$this->Twitter->find());
- Then add to your view
Download source
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
<?php
/**
* Get Twitter Updates
*
* $twits = $this->Twitter->find(array('cache'=>true,'limit'=>8));
*
* @author Darren Moore, zeeneo@gmail.com
* @link http://www.zeen.co.uk
*/
class Twitter extends AppModel
{
/**
* Your Twitter ID
*
* @var integer
* @access public
*/
public $twitterId = 14210082;
/**
* Remove your name from posts
* Set to false to not remove your name, otherwise set to your name
*
* @var mixed
* @access public
*/
public $twitterName = 'zeeneo';
/**
* Show replies to people
*
* @var boolean
* @access public
*/
public $showReplies = false;
/**
* Twitter RSS URL
*
* @var string
* @access public
*/
public $rssUrl = 'http://twitter.com/statuses/user_timeline/:twitterId.rss';
/**
* Turn off table usage
*
* @var string
* @access public
*/
public $useTable = false;
/**
* Duration of cache
*
* @var string
* @access public
*/
public $cacheDuration = '+30 mins';
/**
* Find Twitters
*
* @param array $options Options when getting twits, as followed:
* - cache: Force caching on or off
* - limit: Limit number of records returned
* @access public
* @return array
*/
public function find($options = array())
{
//Get twits
if((isset($options['cache']) && $options['cache'] == false) || ($twits = Cache::read('Twitter.lines')) == false)
{
$twits = $this->_getTwits();
Cache::set(array('duration' => $this->cacheDuration));
Cache::write('Twitter.lines',$twits);
}
//Set to limit
if(isset($options['limit']) && count($twits) > $options['limit'])
{
$twits = array_slice($twits, 0, $options['limit']);
}
return $twits;
}
/**
* Get Twitter Lines
*
* @access private
* @return array
*/
private function _getTwits()
{
//Get feed
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,String::insert($this->rssUrl,array('twitterId'=>$this->twitterId)));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$feed = curl_exec($ch);
curl_close($ch);
if(!$feed) { return false; }
$xml = new SimpleXmlElement($feed);
foreach($xml->channel->item as $item)
{
//
$title = (string)$item->title;
//Skip if it's a reply
if(!$this->showReplies && preg_match('/^'.$this->twitterName.': @/',$title))
continue;
//Remove name
if($this->twitterName)
$title = trim(preg_replace('/^'.$this->twitterName.':/','',$title));
$out[] = array(
'title' => $title,
'description' => (string)$item->description,
'pubDate' => strtotime($item->pubDate),
'link' => (string)$item->link
);
}
return $out;
}
}
?>
|