Skip to content

Instantly share code, notes, and snippets.

@mbabker
Created August 6, 2011 20:38
Show Gist options
  • Save mbabker/1129717 to your computer and use it in GitHub Desktop.
Save mbabker/1129717 to your computer and use it in GitHub Desktop.
A simple class to retreive the latest tweets for a user.
<?php
define('_JEXEC', 1);
require_once __DIR__ . '/libraries/import.legacy.php';
/**
* Simple class to retreive some tweets from Twitter and display using the Joomla! Platform CLI
*
* Note: This file must be placed at the root of your Joomla! Platform checkout
*
* @author Michael Babker
*/
class LatestTweets extends JApplicationCli
{
public function doExecute()
{
// Explain what is going on
$this->out('This application retreives the latest tweets from a user on Twitter.', true);
// Get the username
$this->out('What is the Twitter username?');
$username = $this->in();
// Get the number of tweets
$this->out('How many tweets would you like?');
$count = $this->in();
// Make sure the user has some hits on their account
$hits = $this->getLimit($username);
if ($hits == 0)
{
$this->out('This user has no hits available at this time.');
return;
}
// The URL to request the Twitter data from
$req = 'http://api.twitter.com/1/statuses/user_timeline.json?count=' . $count . '&screen_name=' . $username . '&include_rts=1';
// Get the data
$obj = $this->getJSON($req);
// Process the data
$twitter = $this->processItem($obj);
// Output the tweets
foreach ($twitter as $o)
{
$this->out($o->tweet->text . ' tweeted ' . $o->tweet->created, true);
}
}
/**
* Function to fetch a JSON feed
*
* @param string $req The URL of the feed to load
*
* @return array The fetched JSON query
*/
protected function getJSON($req)
{
// Create a new CURL resource
$ch = curl_init($req);
// Set options
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Grab URL and pass it to the browser and store it as $json
$json = curl_exec($ch);
// Close CURL resource
curl_close($ch);
// Decode the fetched JSON
$obj = json_decode($json, true);
return $obj;
}
/**
* Function to get the rate limit of a Twitter user
*
* @param string $username The Twitter username
*
* @return string The number of remaining hits on a user's rate limit
*/
protected function getLimit($username)
{
// Load the parameters
$req = 'http://api.twitter.com/1/account/rate_limit_status.json?screen_name=' . $username;
// Fetch the decoded JSON
$obj = $this->getJSON($req);
// Get the remaining hits count
if (isset ($obj['remaining_hits']))
{
$hits = $obj['remaining_hits'];
}
else
{
$hits = '';
}
return $hits;
}
/**
* Function to process the Twitter feed into a formatted object
*
* @param array $obj The data from Twitter
*
* @return object The output
*/
protected function processItem($obj)
{
// Initialize
$twitter = array();
$i = 0;
// Load the relative time language strings
JFactory::getLanguage()->load('jhtmldate', JPATH_PLATFORM . '/joomla/html', null, false, false);
// Process the feed
foreach ($obj as $o)
{
// Initialize a new object
$twitter[$i]->tweet = new stdClass();
// The data we're using
$twitter[$i]->tweet->user = $o['user']['screen_name'];
$twitter[$i]->tweet->text = $o['text'];
// Set the dates for the relative time string
$tweetTime = JFactory::getDate($o['created_at'], 'UTC');
$twitter[$i]->tweet->created = JHtml::_('date.relative', $tweetTime, null, JFactory::getDate('now', 'UTC'));
$i++;
}
return $twitter;
}
}
JApplicationCli::getInstance('LatestTweets')->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment