public function requestTweets($append = false) { $this->getRateLimitStatus(); $maxId = isset($_GET['maxTweetId']) ? $_GET['maxTweetId'] : -1; if (!isset($this->_tweets) || $append) { $username = $this->_username; $cache = Yii::app()->cache2; $cacheKey = $this->getCacheKey(); $pageSize = 5; $tweets = $cache->get($cacheKey); if ($append && !$tweets) { // another page of tweets has been requested but the newer tweets have been // invalidated. To avoid having to determine how many pages down the user is, // we simply refresh the feed. $append = false; $maxId = -1; } if (!$tweets || $append) { // fetch tweets and add to cache $tweetCount = 100; $credentials = $this->getTwitterCredentials(); $resourceName = '/statuses/user_timeline.json'; $remainingRequests = $this->remainingRequests($resourceName); if ($remainingRequests < 1) { // rate limit met throw new TwitterFeedWidgetException(Yii::t('app', 'Twitter feed could not be retrieved. Please try again later.')); } $url = 'https://api.twitter.com/1.1' . $resourceName; $getfield = '?screen_name=' . $username . '&count=' . $tweetCount; if ($append) { $maxId = $tweets[count($tweets) - 1]['id_str']; $getfield .= '&max_id=' . $maxId; } $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($credentials); $oldTweets = $tweets; $tweets = CJSON::decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest()); if (($statusCode = $twitter->getLastStatusCode()) != 200) { $this->throwApiException($tweets, $statusCode); } $this->remainingRequests($resourceName, $remainingRequests - 1); if ($append) { $tweets = array_merge($oldTweets, $tweets); } $cache->set($cacheKey, $tweets, 60 * 5); //AuxLib::debugLogR ('cache miss'); } else { //AuxLib::debugLogR ('cache hit'); } if ($maxId === -1) { // initial page load, just return the first page $this->_tweets = array_slice($tweets, 0, $pageSize); } else { // max id specified, return all tweets up one page beyond max id $tweetCount = count($tweets); $found = false; for ($i = 0; $i < $tweetCount; $i++) { $tweet = $tweets[$i]; if ($tweet['id_str'] == $maxId) { $found = true; break; } } if ($found && $i + $pageSize < $tweetCount) { $this->_tweets = array_slice($tweets, 0, $i + $pageSize + 1); } else { if (!$append) { // only request more tweets once return $this->requestTweets(true); } else { // giving up on searching for specified tweet, just display the first page $this->_tweets = array_slice($tweets, 0, $pageSize); } } } } return $this->_tweets; }