<?php
/**
* @author Pete Robinson
*/
class Twitter
{
protected $baseUrl = 'http://twitter.com/statuses/'; // The common section of the twiiter url
protected $username = 'username'; // Your twitter username
protected $password = 'password'; // Your twitter password
public $statuses = array(); // an array to store the fetched data
// method to get the (3) most recent statuses.
public function apiStatuses($count = 3)
{
// get contents of the XML file holding my statuses
$xml = new SimpleXMLElement(file_get_contents($this->baseUrl . 'user_timeline/' . $this->username . '.xml?count=' . $count));
// loop through the xml object assigning each obj->text element to a variable in the $statuses array
foreach($xml->status as $stat) {
$this->statuses[] = $stat->text;
}
}
// method to fetch the statuses from the database, ordering by rank.
public function getStatuses()
{
return DB::select('*', 'tweets', '', 'rank ASC');
}
// method to update status. I'm not using this at the moment, although I do know it works.
public function updateStatus($status)
{
// initialise the cURL request to baseUrl/update.xml
$ch = curl_init($this->baseUrl . 'update.xml');
// assign class properties to variables
$user = $this->username;
$pass = $this->password;
// set username and password string
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
// set the status
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status=' . $status);
// execute the curl request
$output = curl_exec($ch);
// close cURL connection
curl_close($ch);
// return the output of the request. When/if I start using this properly, i will implement error checking and the like.
return $output;
}
}
class Cron extends Twitter
{
// construct the object
function __construct()
{
// use apiStatuses method defined in twitter class to assign 3 most recent statuses to an array
$this->apiStatuses();
// ensure that statuses have been retrived.
if($this->statuses) {
// set the fields for DB insert.
$fields = array('status', 'rank');
// delete all existing tweets in DB
DB::delete('tweets', "tweet_id > '0'");
$i = 1;
// loop through the $statuses array, assigning each one to $values['status'] and setting the rank;
foreach($this->statuses as $stat) {
$values['status'] = $stat;
$values['rank'] = $i;
// insert the tweet into the DB
DB::insert('tweets', $fields, $values);
// increment the rank.
$i++;
}
}
}
}
?>
<?php
require_once('/path-to-class-lib/twitter.class.php');
require_once('/path-to-class-lib/cron.class.php');
if($_GET['t'] == '1') {
new Cron();
}
?>