Let's see step by step explanation of our tweetsigniter class, a twitter codeigniter library. Initially, we have declared all our necessary global variables, like username, password, path, template, timezone etc.
# username field
public $username;
# password field
public $password;
# maximum no. of values
public $maxItems;
# path value
public $path;
# timezone value
public $timezone;
public $configs = array();
# template file name
public $template;
Next comes the constructor method. it defines our template variable. we will replace the template values.
public function __construct()
{
$this->template = "
{text}
";
}
we set the necessary values with this method like username, password, maxItem shows the maximum no. of items to be displayed, and timezone will be your timezone.
public function setInfo($username, $password, $maxItems = '5', $timezone = '+6')
{
$this->username = $username;
$this->password = $password;
$this->timezone = $timezone;
$this->maxItems = $maxItems;
$this->config();
}
We set necessary configuraion Information with these function.
public function config()
{
$this->configs['template'] = $this->template;
$this->configs['maxitems'] = $this->maxItems;
}
The following function shows all your tweets. you can apply css style within the html elements. the cut and resume functions process tweeter responses.
public function easyfeeder()
{
$content=$this->twitterXml('http://twitter.com/statuses/user_timeline.xml');
$items=explode("",$content);
array_shift($items);
$retstr="";
return $retstr;
}
Following is the cut and resume methods that we used in the above method.
public function cut($start,$end,$word)
{
$word=substr($word,strpos($word,$start)+strlen($start));
$word=substr($word,0,strpos($word,$end));
return $word;
}
public function resume($text,$limit=7)
{
$words=@explode(" ",$text);
$words=@array_splice($words,0,$limit);
$retstr=@implode(" ",$words);
return $retstr."... ";
}
The following method will show all you tweeter followers.
public function twitterFollowers()
{
$this->template = "

";
$content=$this->twitterXml('http://twitter.com/statuses/followers.xml');
$items=explode("
",$content);
array_shift($items);
$retstr="";
return $retstr;
}
This method will show all your tweeter friends.
public function twitterFriends()
{
$this->template = "
";
$content=$this->twitterXml('http://twitter.com/statuses/friends.xml');
$items=explode("",$content);
array_shift($items);
$retstr="";
return $retstr;
}
let's see how we have connected with tweeter. we used curl to connect with tweeter. the following method done the trick for us.
function twitterXml($url)
{
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERPWD, "$this->username:$this->password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
return $buffer;
}
Links:
See the book OpenCart 1.4 Template Design Cookbook.
See the book Joomla Mobile Development Beginners Guide