Location: PHPKode > scripts > Twitter RSS > class.twitterrss.php
<?php

/*
include('class.twitterrss.php');
$url = "http://twitter.com/statuses/friends_timeline/78320369.rss";
$username = "XXXXX";
$password = "XXXXX";

$rss = new TwitterRss($url, $username, $password);
echo $rss->Load(12);// Load newest 12 posts
*/

class TwitterRss {
	var $url;
	var $username;
	var $password;

	function __construct($url, $username, $password) {
		try {
			if(empty($url)) throw new Exception("Twitter URL is required.");
			$this->url = $url;
			if(empty($username)) throw new Exception("Twitter user name is required.");
			$this->username = $username;
			if(empty($password)) throw new Exception("Twitter password is required.");
			$this->password = $password;
		}
		catch (Exception $e) {
			echo "Error: ".$e->getMessage();
		}
	}

	function GetRss() {
		// 1 - Pass through HTTP authorization
		$cookie_file_path = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER["SCRIPT_FILENAME"]).'cookiejar_'.time().'.txt'; // Please set your Cookie File path
		$handle = fopen($cookie_file_path, 'w');
		fclose($handle);

		$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->url);
		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode ("$this->username:$this->password")));
		//curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
		$result = curl_exec ($ch);
		curl_close ($ch);
		print $result;

		// 2 - Get RSS feed
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->url);
		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
		$result = curl_exec ($ch);
		curl_close ($ch);
		print $result;

		@unlink($cookie_file_path);
	}

	function Load($numRows=12) {

		$filename = 'cache/twitter.html';

		// Update cache file on every 1 hour
		if (file_exists($filename) && filemtime($filename) > strtotime('-1 hour')) return file_get_contents($filename);

		ob_start();
		$this->GetRss();
		$contents = ob_get_clean();

		// Generate HTML begin =======================
		$tpl = '';
		$i = 0;
		if(preg_match_all('/\<item\>(.*?)\<\/item\>/is', $contents, $items)) {
			if(count($items[1])) {
				$tpl .= '<ul id="twitter_update_list" class="twitList">';
				foreach ($items[1] as $item) {
					$title = $description = $pubDate = $link = '';
					if(preg_match('/\<title\>(.*?)\<\/title\>/is', $item, $matches)) $title = trim($matches[1]);
					if(preg_match('/\<description\>(.*?)\<\/description\>/is', $item, $matches)) $description = trim($matches[1]);
					if(preg_match('/\<pubDate\>(.*?)\<\/pubDate\>/is', $item, $matches)) $pubDate = trim($matches[1]);
					if(preg_match('/\<link\>(.*?)\<\/link\>/is', $item, $matches)) $link = trim($matches[1]);
					$tpl .= '<li>';
					$tpl .= '<div><a href="'.$link.'" target="_blank">'.$title.'</a></div>';
					$tpl .= '<div>'.$pubDate.'</div>';
					$tpl .= '</li>';
					$i++;
					if($numRows == $i) break;
				}
				$tpl .= '</ul>';
			}
		}
		// Generate HTML end =======================

		// Save file
		if (!$fp = fopen($filename, 'w')) {
			//echo "Cannot open file ($filename)";
			return;
		}
		if(false === fwrite($fp, $tpl)) {
			//echo "Cannot write to file ($filename)";
			return;
		}
		fclose($fp);

		return (file_exists($filename)) ? file_get_contents($filename) : 'No cached file!';
	}
}

?>
Return current item: Twitter RSS