Last active
January 3, 2016 05:39
-
-
Save PhilKershaw/8417782 to your computer and use it in GitHub Desktop.
Twitter status class - Provides the ability to fetch status' for a Twitter account using Zend Service (1.12)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Twitter status class | |
* | |
* Provides the ability to fetch status' for a Twitter account. | |
* | |
* @package Twitter | |
* @subpackage Twitter_Status | |
* @author Phil Kershaw | |
*/ | |
require_once 'Zend/Service/Twitter.php'; | |
require_once 'Zend/Service/Twitter/Exception.php'; | |
require_once 'Zend/Oauth/Token/Access.php'; | |
class Twitter_Status | |
{ | |
/** | |
* The auth token for connecting to the Twitter API | |
* | |
* @var string | |
*/ | |
private $_twitter = NULL; | |
/** | |
* The results from Twitter | |
* | |
* @var array | |
*/ | |
private $_result = NULL; | |
/** | |
* The oauth configuration details | |
* | |
* @var array | |
*/ | |
private $_config = NULL; | |
/** | |
* The path to the file where tweets are cached | |
* | |
* @var string | |
*/ | |
private $_cacheFilePath = NULL; | |
/** | |
* The type of feed to request | |
* | |
* @var string | |
*/ | |
private $_timeline = NULL; | |
/** | |
* Available status types | |
* | |
* @var array | |
*/ | |
private $_status_types = array( 'Home', 'Mentions', 'User', ); | |
/** | |
* Constructor | |
* @param array $config | |
* @param string $timeline Home|Mentions|User| | |
* @return void | |
*/ | |
public function __construct($config, $timeline) | |
{ | |
$this->_config = $config; | |
$this->_timeline = $timeline; | |
// set up cache | |
$this->_setCacheFile(); | |
} | |
/** | |
* Authenticate with Twitter to obtain an auth token using OAuth | |
* | |
* @return object WcLib_Twitter_Status | |
*/ | |
public function authenticate() | |
{ | |
$token = new Zend_Oauth_Token_Access(); | |
$token->setToken($this->_config['accessToken']) | |
->setTokenSecret($this->_config['accessTokenSecret']); | |
$this->_twitter = new Zend_Service_Twitter( | |
array( | |
'userName' => $this->_config['userName'], | |
'accessToken' => $token, | |
'oauthOptions' => array( | |
'consumerKey' => $this->_config['key'], | |
'consumerSecret' => $this->_config['secret'], | |
) | |
) | |
); | |
return $this; | |
} | |
/** | |
* Fetch the timeline specified in $this->_timeline | |
* | |
* @param boolean $clickableLinks | |
* @param integer $limit | |
* @param boolean $cache | |
* | |
* @throws Zend_Service_Twitter_Exception | |
* | |
* @return array | |
*/ | |
public function fetch($clickableLinks, $limit, $cache = TRUE) | |
{ | |
try { | |
if ((filesize($this->_cacheFilePath) <= 0) | |
|| (time() - filemtime($this->_cacheFilePath) > 3600)) { // if cache file is empty or the cache is stale | |
// Make sure the type of tweets requested is valid | |
if (!in_array($this->_timeline, $this->_status_types)) { | |
throw new Zend_Service_Twitter_Exception("Invalid timeline type. Options include: Home, Mentions or User. Please check the functions.php"); | |
} | |
// fetch tweets from twitter | |
$timeline = $this->_timeline; | |
$timeline .= 'Timeline'; | |
$this->_result = $this->_twitter->statuses->$timeline()->toValue(); | |
// Make URLs clickable | |
if ($clickableLinks) { | |
self::_applyUrlEntities($this->_result); | |
} | |
// cache our all tweets | |
if ($cache) { | |
$this->_cacheTweets(); | |
} | |
} else { | |
// fetch our cached tweets | |
$this->_result = unserialize( | |
file_get_contents($this->_cacheFilePath) | |
); | |
} | |
// apply the limit to the result set | |
if (NULL !== $limit && $limit < 20) { | |
$this->_result = array_slice($this->_result, 0, $limit); | |
} | |
} catch (Exception $e) { | |
error_log($e->getMessage()); | |
} | |
return $this->_result; | |
} | |
/** | |
* Sets the cache file up | |
* | |
* @return void | |
*/ | |
private function _setCacheFile() | |
{ | |
// Create cache directory | |
$path = __DIR__ . '/cache'; | |
if (!is_dir($path)) { | |
$oldumask = umask(0); | |
mkdir($path, 0775); | |
umask($oldumask); | |
} | |
// create cache file | |
$filename = preg_replace('/[^a-zA-Z0-9_]/', '', $this->_config['userName']) . '_' . $this->_timeline; | |
$this->_cacheFilePath = $path . '/' . $filename; | |
if (!file_exists($this->_cacheFilePath)) { | |
touch($this->_cacheFilePath); | |
} | |
} | |
/** | |
* Serialises and saves the Tweets to a file | |
* | |
* @return void | |
*/ | |
private function _cacheTweets() | |
{ | |
if (!file_put_contents($this->_cacheFilePath, serialize($this->_result))) { | |
throw new Zend_Service_Twitter_Exception("Failed to save to Tweets to cache. File path: {$this->_cacheFilePath}"); | |
} | |
} | |
/** | |
* Converts URLs into hyperlinks | |
* @param array $feed | |
* @return void | |
*/ | |
private static function _applyUrlEntities(&$feed) | |
{ | |
if ($feed !== NULL) { | |
foreach($feed as &$item) { | |
if ($item->entities->urls !== NULL) { | |
$urls = array_reverse($item->entities->urls); | |
$content = &$item->text; | |
foreach ($urls as $url) { | |
$content = substr_replace($content, '</a>', $url->indices[1], 0); | |
$content = substr_replace($content, '<a href="'.$url->url.'" target="_blank">', $url->indices[0], 0); | |
} | |
unset($content); | |
} | |
} | |
unset($item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment