Report abuse

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
<?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();
}
?>