Listing Twitter Followers from Array Text to Excel Spreadsheet


One Friday morning I realized it was #FF Follow Friday, it came to my mind how do I list everyone in text of spreadsheet then just copy paste it so I can promote all of my followers on friday?

Everyone knows that when you click followers in Twitter website it will come out with list complete with all the followers details like username, nickname, description, location and picture of them. If I have few hundred of follower that can be a problem for me to copy every single name to tweet. So I googled 'Listing Twitter Followers' so I end up with results from Twitter API sites, and some forums, after reading I found a very nice example then modified it to make it simpler. Since it was php so it was easy to implement. Just have to download a php compiler, with internet connection this can be done without any server setup of any-kind.

So in the end I came up with this code in pure Php:
1:  <?php  
2:    $cursor = -1;  
3:  $account_from = 'account';  
4:  $result = array();  
5:  do  
6:  {  
7:    $json = file_get_contents('http://api.twitter.com/1/statuses/followers/' . $account_from .'.json?cursor=' . $cursor);  
8:    $accounts = json_decode($json);  
9:    foreach ($accounts->users as $account)  
10:    {  
11:                 $i++;  
12:                 $fudido = array(  
13:          'twitter_id' => $account->id_str,  
14:          'account' => $account->screen_name,  
15:          'description' => $account->description,  
16:        );  
17:        //print $i." ".$fudido['account']."\n";  
18:        array_push($result,$fudido);  
19:    }  
20:    $cursor = $accounts->next_cursor;  
21:  }  
22:  while ($cursor > 0);  
23:  ?>  


  1. $account_from = 'account'  , input your account or anyone's account there.
  2. After  while ($cursor > 0); on line 22, the next line can add print_r($result); to see result in list of arrays.
  3. But our objective is to export this into an Excel file. So printing an array is not useful.
  4. Basically Excel can import text files so we will use this to our advantage and make work more easy. As you can see below a text which data is separated by tab can be converted into tabular form into Excel.


Thus, all we need is to convert all those arrays we took from Twitter into data separated by Tab. So I googled it and found this on Art of Web on how to export Data to Excel and etcetera with Php. The original code was intended to work on a Server because it uses header so the browser will change the filename from php into excel formal. Since I did not install Apache server or whatever and solely running with Php compiler. I removed the part with header and added the File Write(fwrite) function so write a new file directly into the computer, so I have this:
1:  <?PHP  
2:   function cleanData(&$str)  
3:   {  
4:    $str = preg_replace("/\t/", "\\t", $str);  
5:    $str = preg_replace("/\r?\n/", "\\n", $str);  
6:    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';  
7:   }  
8:  $myFile = "testFile.xls";  
9:  $fh = fopen($myFile, 'w') or die("can't open file");  
10:  $flag = false;  
11:   foreach($result as $row) {  
12:    if(!$flag) {  
13:     fwrite($fh, implode("\t", array_keys($row)) . "\r\n");  
14:     $flag = true;  
15:    }  
16:    array_walk($row, 'cleanData');  
17:    fwrite($fh, implode("\t", array_values($row)) . "\r\n");  
18:   }  
19:   fclose($fh);  
20:   exit;  
21:  ?>  

This will directly convert the Array into text data separated by Tab, which looks like:


The only next thing you need to do is import this pseudo xls file in your Excel, and select only TAB as the separator. To learn more about Php Data Export from Array, read Art of Web .

The end result will be:
Thank you and enjoy your tweeting moments.

Comments

Popular posts from this blog

Contemplating Nothing

Perihal Flag Part 1(CMP dan SUB)