Wednesday, July 7, 2010

tweetsigniter Documentation

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="

    Latest News

      "; foreach($items as $cnt=>$item) { if($this->configs['maxitems']>0 and $cnt+1>$this->configs['maxitems']) break; $created_at=$this->cut("","",$item); $text=$this->cut("","",$item); $itemid=$this->cut("","",$item); $screenname=$this->cut("","",$item); $created_day = substr($created_at, 0, 3); $created_monthday = substr($created_at, 4, 6); $created_timehour = substr($created_at, 11, 2); $created_timehour = $created_timehour + $this->timezone; $created_timeminutes = substr($created_at, 14, 2); $created_year = substr($created_at, 26, 4); if ($created_timehour == 12) { $ampm = "pm"; } else if (($created_timehour > 12) AND ($created_timehour < 24)) { $ampm = "pm"; $created_timehour = $created_timehour - 12; } else if ($created_timehour == 24) { $ampm = "am"; $created_timehour = 12; } else { $ampm = "am"; } $created_at = $created_day . ", " . $created_monthday . ", " . $created_year . " - " . $created_timehour . ":" . $created_timeminutes . $ampm; $itemlink = "http://twitter.com/" . $screenname . "/statuses/" . $itemid; $tpl=$this->configs['template']; $tpl=str_replace("{created_at}",$created_at,$tpl); $tpl=str_replace("{text}",$text,$tpl); $tpl=str_replace("{itemlink}",$itemlink,$tpl); $retstr.=$tpl; } $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="

    Latest News

      "; foreach($items as $cnt=>$item) { if($this->configs['maxitems']>0 and $cnt+1>$this->configs['maxitems']) break; $created_at=$this->cut("","",$item); $text=$this->cut("","",$item); $itemid=$this->cut("","",$item); $screenname=$this->cut("","",$item); $itemlink = "http://twitter.com/" . $screenname; $tpl=$this->template; $tpl=str_replace("{text}",$text,$tpl); $tpl=str_replace("{itemlink}",$itemlink,$tpl); $retstr.=$tpl; } $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="

    Latest News

      "; foreach($items as $cnt=>$item) { if($this->configs['maxitems']>0 and $cnt+1>$this->configs['maxitems']) break; $created_at=$this->cut("","",$item); $text=$this->cut("","",$item); $itemid=$this->cut("","",$item); $screenname=$this->cut("","",$item); $itemlink = "http://twitter.com/" . $screenname; $tpl=$this->template; $tpl=str_replace("{text}",$text,$tpl); $tpl=str_replace("{itemlink}",$itemlink,$tpl); $retstr.=$tpl; } $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




    List of my works:

    Opencart Extensions:

    1. Product Based Quantity Wise Shipping: Find it here.
    2. OpenSSLCOMMERZ: integrate SSLCOMMERZ with opencart: Find it here.
    3. Fine Search v.1.0 - Improves Opencart search feature to find relevant: Find it here.
    4. Opensweetcaptcha - An easy way to generate attractive captcha for your system!: Find it here.
    5. Custom Field Product - add unlimited custom fields to the product form: Find it here.
    6. Formcaptcha - add captcha on the register page: Find it here.

    My Books:

    1. OpenCart 1.4 Template Design Cookbook.
    2. Joomla Mobile Development Beginners Guide

    4 comments:

    Anonymous said...

    Hi,
    Do you plan on updating the code to use oauth?

    Anonymous said...

    No funciona

    Nirmal said...

    I think this tutorial is incomplete

    Anonymous said...

    TechNew.In

    All in one tech website !

    http://www.technew.in/

    :)