Selasa, 20 September 2011

Melihat Follower dengan Twitter Api



Melihat Follower dengan Twitter Api
Twitter adalah sebuah fenomena menarik. Kecepatan beritanya melebihi kecepatan berita media-media. Hal tersebut mungkin karena sebagian pengguna twitter di indonesiamenggunakan twitter secara mobile, apa yang terjadi dan ada dipikiran langsung di sharing via twitter saat itu juga..
Twitter menurut saya sangat powerful untuk dijadikan alat untuk business intelegent… karena semua informasinya dapat diakses secara bebas. ketika kita me-launch sebuah product maka kita dapat segera mengetahui semua feedback mengenai product yang kita launch via twitter..
Untuk dapat memonitor product kita atau melakukan market research kita bisa melakukannya dengan melihat streamline dari masing2 pengguna kita. dengan melihat siapa saja follower kita maka kita bisa memfollow mereka (sistem berantai)
Langsung saja.. pertama-tama buatlah kelas twiiter yang berfungsi untuk memanggil twitter API. Untuk clientnya kita tetap menggunakan jQuery
Mungkin bagi anda yang senang untuk melihat code bisa melanjutkan :P

Class Twiitter

Kelas ini akan memanggil 3 api yaitu friends, follower, dan show
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
class Twitter {
 private $screen_name;
 private $api = array('friends'   => 'http://twitter.com/statuses/friends.json',
      'followers' => 'http://twitter.com/statuses/followers.json',
      'show'   => 'http://twitter.com/users/show/'
    );
 
 public function __construct($screen_name){
  $this->screen_name  =  $screen_name;
 }
 
 public function countUsers($type){
  $total = 0;
  $callstr = $this->api['show'].$this->screen_name.'.json'; 
 
  $ch = curl_init($callstr);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $apiresponse = curl_exec($ch);
  curl_close($ch);
 
  if ($apiresponse) {
   $json = json_decode($apiresponse);
   if (($json != null)&&(!$json->error)){
    if($type=='Friends') 
     $total   = $json->friends_count;
    else if($type=='Followers')    
     $total   = $json->followers_count;
   }   
  }
  return $total;
 }
 
 public function getUsers($type){
  $total = $this->countUsers($type);
 
  $images = array();
  $result = 1;
  if($type=='Friends') $callstr = $this->api['friends'].'?screen_name='.$this->screen_name; 
  if($type=='Followers') $callstr = $this->api['followers'].'?screen_name='.$this->screen_name; 
  $ch = curl_init($callstr);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $apiresponse = curl_exec($ch);
  curl_close($ch);
  $res = $div1 = $div2 = $div3 = $div4 = '';
 
  $i=1;
  if ($apiresponse) {
   $json = json_decode($apiresponse);
   if ($json != null){
    if($json->error) $result=0;
    else{ 
     foreach ($json as $u){
      $images[] = $u->profile_image_url;
 
      if($i>25&&$i<=50){
       if($i==26)
        $div2='<div style="display:none;" id="2"><a class="_img_profile" href="http://www.twitter.com/'.$u->screen_name.'"><img width="35px" heigth="35px" src="'.$u->profile_image_url.'"></img><input class="_name" type="hidden" value="'.$u->screen_name.'"></input><input class="_nmb_followers" type="hidden" value="'.$u->followers_count.'"></input><input class="_nmb_friends" type="hidden" value="'.$u->friends_count.'"></input></a>';
       else
        $div2.='<a class="_img_profile" href="http://www.twitter.com/'.$u->screen_name.'"><img width="35px" heigth="35px" src="'.$u->profile_image_url.'"></img><input class="_name" type="hidden" value="'.$u->screen_name.'"></input><input class="_nmb_followers" type="hidden" value="'.$u->followers_count.'"></input><input class="_nmb_friends" type="hidden" value="'.$u->friends_count.'"></input></a>';
      }
      else if($i>50&&$i<=75){
       if($i==51)
        $div3='<div style="display:none;" id="3"><a class="_img_profile" href="http://www.twitter.com/'.$u->screen_name.'"><img width="35px" heigth="35px" src="'.$u->profile_image_url.'"></img><input class="_name" type="hidden" value="'.$u->screen_name.'"></input><input class="_nmb_followers" type="hidden" value="'.$u->followers_count.'"></input><input class="_nmb_friends" type="hidden" value="'.$u->friends_count.'"></input></a>';
       else
        $div3.='<a class="_img_profile" href="http://www.twitter.com/'.$u->screen_name.'"><img width="35px" heigth="35px" src="'.$u->profile_image_url.'"></img><input class="_name" type="hidden" value="'.$u->screen_name.'"></input><input class="_nmb_followers" type="hidden" value="'.$u->followers_count.'"></input><input class="_nmb_friends" type="hidden" value="'.$u->friends_count.'"></input></a>';
      }
      else if($i>75&&$i<=100){
       if($i==76)
        $div4='<div style="display:none;" id="4"><a class="_img_profile" href="http://www.twitter.com/'.$u->screen_name.'"><img width="35px" heigth="35px" src="'.$u->profile_image_url.'"></img><input class="_name" type="hidden" value="'.$u->screen_name.'"></input><input class="_nmb_followers" type="hidden" value="'.$u->followers_count.'"></input><input class="_nmb_friends" type="hidden" value="'.$u->friends_count.'"></input></a>';
       else
        $div4.='<a class="_img_profile" href="http://www.twitter.com/'.$u->screen_name.'"><img width="35px" heigth="35px" src="'.$u->profile_image_url.'"></img><input class="_name" type="hidden" value="'.$u->screen_name.'"></input><input class="_nmb_followers" type="hidden" value="'.$u->followers_count.'"></input><input class="_nmb_friends" type="hidden" value="'.$u->friends_count.'"></input></a>';
      }
      else{
       if($i==1)
        $div1='<div id="1"><a class="_img_profile" href="http://www.twitter.com/'.$u->screen_name.'"><img width="35px" heigth="35px" src="'.$u->profile_image_url.'"></img><input class="_name" type="hidden" value="'.$u->screen_name.'"></input><input class="_nmb_followers" type="hidden" value="'.$u->followers_count.'"></input><input class="_nmb_friends" type="hidden" value="'.$u->friends_count.'"></input></a>';
       else
        $div1.='<a class="_img_profile" href="http://www.twitter.com/'.$u->screen_name.'"><img width="35px" heigth="35px" src="'.$u->profile_image_url.'"></img><input class="_name" type="hidden" value="'.$u->screen_name.'"></input><input class="_nmb_followers" type="hidden" value="'.$u->followers_count.'"></input><input class="_nmb_friends" type="hidden" value="'.$u->friends_count.'"></input></a>';
      }
      ++$i;
     }
    }   
   } 
 
   $n_divs = 0;
   if($div1!=''){
    $div1.='</div>';
    $res.=$div1;
    ++$n_divs;
   } 
   if($div2!=''){
    $div2.='</div>';
    $res.=$div2;
    ++$n_divs;
   }
   if($div3!=''){
    $div3.='</div>';
    $res.=$div3;
    ++$n_divs;
   }
   if($div4!=''){
    $div4.='</div>';
    $res.=$div4;
    ++$n_divs;
   }
   if($result == 1){
       if($type=='Friends')
     $link = "http://www.twitter.com/".$this->screen_name."/following";
    else
     $link = "http://www.twitter.com/".$this->screen_name."/followers";
    $res.='<div class="jf-more" style="display:none;" id="'.($n_divs+1).'"><a href="'.$link.'">See all on Twitter</a></div>';
   } 
  }
  else
   $result = -1;
 
  $json = array("result" => $result,"images" => $images,"res" => $res,"total" => $total); 
  $encoded = json_encode($json);
  echo $encoded;
  unset($encoded);
 }
}

sebagian code berasal dari http://tympanus.net/codrops/2009/12/02/twitter-api-and-jquery-showcase-display-your-followers-or-friends/ dan qtips

0 comments:

Poskan Komentar